package yang.polymorphic;
class Father{
public String str1;
public int num1;
Father(){
System.out.println("父类无参构造方法");
}
Father(String str){
System.out.println("父类有参构造方法1");
this.str1 = str;
}
}
class Son extends Father{
public String str2;
public int num2;
Son(){
//你不显示调用父类的构造方法时,会默认加上super()
System.out.println("子类无参构造方法");
}
Son(String str){
System.out.println("子类有参构造方法1");
this.str2 = str;
}
Son(String str,int num){
super(str);
System.out.println("子类有参构造方法2");
this.num2 = num;
}
}
public class Test {
public static void main(String[] args) {
Son son = new Son("dd",22);
System.out.println("1."+son.str1+"--"+son.str2+"--"+son.num1+"--"+son.num2);
System.out.println("-------------------------------");
Son son2 = new Son("yy");
System.out.println("2."+son2.str1+"--"+son2.str2+"--"+son2.num1+"--"+son2.num2);
}
}
package yang.polymorphic;
class Father{
public String str1;
public int num1;
Father(){
System.out.println("父类无参构造方法");
}
Father(String str){
System.out.println("父类有参构造方法1");
this.str1 = str;
}
}
class Son extends Father{
public String str2;
public int num2;
Son(){
//你不显示调用父类的构造方法时,会默认加上super()
System.out.println("子类无参构造方法");
}
Son(String str){
System.out.println("子类有参构造方法1");
this.str2 = str;
}
Son(String str,int num){
super(str);
System.out.println("子类有参构造方法2");
this.num2 = num;
}
}
public class Test {
public static void main(String[] args) {
Son son = new Son("dd",22);
System.out.println("1."+son.str1+"--"+son.str2+"--"+son.num1+"--"+son.num2);
System.out.println("-------------------------------");
Son son2 = new Son("yy");
System.out.println("2."+son2.str1+"--"+son2.str2+"--"+son2.num1+"--"+son2.num2);
}
}