Hi
I have a problem about default value of data member!
+++++++ cut here++++++
class CComputer
{
private double cpu=3.0;
private double memory=1.0;
public void set(double c)
{
if(c<0)
System.out.println("Input cpu error, cpu is default value");
else
this.cpu=c;
}
public void set_memory(double m)
{
if(m<0)
System.out.println("Input memory error, memory is default
value");
else
{
memory=m;
show_memory();
}
}
public void set(double c,double m)
{
set(c);
set_memory(m);
}
void show_cpu()
{
System.out.println("cpu="+this.cpu);
}
void show_memory()
{
System.out.println("memory="+this.memory);
}
void show_all()
{
this.show_cpu();
this.show_memory();
}
}
public class bbb
{
public static void main(String args[])
{
CComputer c1=new CComputer();
c1.set(-3.5,1.5);
c1.show_all();
c1.set(-3.5,-2.0);
c1.show_all();
c1.set(-3.5);
c1.show_all();
}
}
+++++++
The output of "c1.set(-3.5,-2.0);" shows memory=1.5.
The output of " c1.set(-3.5);" also shows memory=1.5;
Why? I thought memory should be default value, i.e. 1.0.
Thank you in advance.
Mike
Daniel Pitts - 29 May 2007 05:11 GMT
> Hi
>
[quoted text clipped - 68 lines]
>
> Mike
You're default value only gets assigned during the construction of
your object.
your "new CComputer()" sets it to 1.0, You're first call to set(-3.5,
1.5) sets that value to 1.5, and it isn't ever reset.
Daniel Pitts - 29 May 2007 05:12 GMT
> Hi
>
[quoted text clipped - 68 lines]
>
> Mike
You're default value only gets assigned during the construction of
your object.
your "new CComputer()" sets it to 1.0, You're first call to set(-3.5,
1.5) sets that value to 1.5, and it isn't ever reset.