super(...)访问父类的有参构造方法
class eddemo{
public static void main(String[] args)
{
son s=new son();
System.out.println("主方法"+s.getname()+s.getage());
son s1=new son("sisi",23);
}
}
class father
{
private String name;
private int age;
/*public father () //将父类的空参构造注释掉
{
System.out.println("父类的空参构造");
}*/
public father(String name,int age)
{
this.name=name;
this.age=age;
System.out.println("父类的有参构造");
System.out.println(this.name+this.age);//输出父类的name和age
}
public void setname(String name)//setxxx和getxxx方法
{
this.name=name;
}
public String getname()
{
return name;
}
public void setage(int age)
{
this.age =age;
}
public int getage()
{
return age;
}
}
class son extends father
{
public son ()
{
super ("zhangsan",23);//调用父类的有参构造方法,对父类进行初始化,这样的话父类与子类
System.out.println("子类的无参构造");//的初始化值不就一样了么?并且系统内存到底是产生了几 //个对象,怎么运作的?
}
public son (String name,int age)
{
super (name,age);
System.out.println("子类的有参构造");
}
}