publicclass Father {
Stringname;
intage;
public Father(){ //构造方法的重载
}
public Father(Stringname){
this.name = name;
System.out.println("name");
}
public Father(intage){
this.age = age;
System.out.println("age");
}
public Father(Stringname,intage){
this.name = name;
this.age = age;
System.out.println("name,age");
}
}
package com.lijie.lianxi6;
importorg.omg.CORBA.PUBLIC_MEMBER;
publicclass Sonextends Father {
//String name;//因为继承了Father的属性和方法,所以可以省略,起到了简化代码的作用
//int age; //要访问这些属性和方法就直接去父类那里访问
public Son(){
System.out.println("son空参数的构造方法....");
}
public Son(Stringname){
super(name);//第1种调用:单独用时,放在首位,this也一样.
//this.name = name;第2种调用
//super.name = name;第3种调用
System.out.println("son name空参数的构造方法....");
}
public Son(Stringname ,intage){
super(name , age);
this.name = name;
this.age = age;
System.out.println("son name age参数的构造方法....");
}
publicstaticvoid main(String[]args) {
//PUBLIC_MEMBER,不要在main方法里写public
//Son son = new Son();
System.out.println("------------------");
Son son2 =new Son("xiao" , 10);
System.out.println(son2.name);//去到父类那里访问
System.out.println(son2.age);
}
}