class Person
{
private String name;
private int age;
Person()
{
System.out.println("A"+"name="+name+",,age="+age);
speak();
}
Person(String n)
{
name=n;
System.out.println("A"+"name="+name+",,age="+age);
speak();
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
Person(String n,int c)
{
name=n;
age=c;
System.out.println("C"+"name="+name+",,age="+age);
speak();
}
void speak()
{
System.out.println("cay...............");
}
}
class PersonDemo1
{
public static void main(String[] args)
{
//Person p=new Person();
Person p1=new Person("大黄");
System.out.println(p1.getName());
//Person p2=new Person("阿凯",28);
//p.speak();
//p.speak();
}
}
注:构造函数未写name=n 前,你的执行结果就会变成Aname=null,,age=0 cay...............null 因为你默认使的是第二个有参构造函数,
但是,没有接收这个名字。所以,name就会为null
当你接收name之后,初始化name 完成,结果变成了,Aname=大黄,,age=0 cay...............大黄 |