子类与父类构造方法执行顺序
2013-03-10 22:36
369 查看
父类:
public class Father {
String s="this is father";
public Father(){
System.out.println(s);
}
public Father(String str){
s=str;
System.out.println(s);
}
}
1、子类有或无参数构造方法,都会调用父类无参构造方法。
public class Son extends Father{
String s="this is son";
public Son(){
System.out.println(s);
}
public Son(String str){
//this();
s=str;
System.out.println(s);
}
public Son(String str1,String str2){
//super(str1+" "+str2);
s=str1;
System.out.println(s);
}
}
public class ExtendCase {
public static void main(String args[]){
Father f1=new Father();
Father f2=new Father("Hello father ");
Son s1=new Son();//调用父类无参构造方法,然后调用子类无参构造方法;
Son s2=new Son("Hello son ");//调用父类无参构造方法,然后调用子类含参构造方法。
Son s3=new Son("Hello son","Hello father");//
}
}
结果:
this is father
Hello father
this is father
this is son
this is father
Hello son
this is father
Hello son
2,子类调用父类有参构造方法必须实现super。调用了父类有参数的构造方法不会再自动调用无参数的构造方法。
public class Son extends Father{
String s="this is son";
public Son(){
System.out.println(s);
}
public Son(String str){
this();
s=str;
System.out.println(s);
}
public Son(String str1,String str2){
super(str1+" "+str2);
s=str1;
System.out.println(s);
}
}
public class ExtendCase {
public static void main(String args[]){
Father f1=new Father();
Father f2=new Father("Hello father ");
Son s1=new Son();//调用父类无参构造方法,然后调用子类无参构造方法;
Son s2=new Son("Hello son ");//调用父类无参构造方法,然后调用子类含参构造方法。
Son s3=new Son("Hello son","Hello father");//
}
}
结果:
this is father
Hello father
this is father
this is son
this is father
this is son
Hello son
Hello son Hello father
Hello son
public class Father {
String s="this is father";
public Father(){
System.out.println(s);
}
public Father(String str){
s=str;
System.out.println(s);
}
}
1、子类有或无参数构造方法,都会调用父类无参构造方法。
public class Son extends Father{
String s="this is son";
public Son(){
System.out.println(s);
}
public Son(String str){
//this();
s=str;
System.out.println(s);
}
public Son(String str1,String str2){
//super(str1+" "+str2);
s=str1;
System.out.println(s);
}
}
public class ExtendCase {
public static void main(String args[]){
Father f1=new Father();
Father f2=new Father("Hello father ");
Son s1=new Son();//调用父类无参构造方法,然后调用子类无参构造方法;
Son s2=new Son("Hello son ");//调用父类无参构造方法,然后调用子类含参构造方法。
Son s3=new Son("Hello son","Hello father");//
}
}
结果:
this is father
Hello father
this is father
this is son
this is father
Hello son
this is father
Hello son
2,子类调用父类有参构造方法必须实现super。调用了父类有参数的构造方法不会再自动调用无参数的构造方法。
public class Son extends Father{
String s="this is son";
public Son(){
System.out.println(s);
}
public Son(String str){
this();
s=str;
System.out.println(s);
}
public Son(String str1,String str2){
super(str1+" "+str2);
s=str1;
System.out.println(s);
}
}
public class ExtendCase {
public static void main(String args[]){
Father f1=new Father();
Father f2=new Father("Hello father ");
Son s1=new Son();//调用父类无参构造方法,然后调用子类无参构造方法;
Son s2=new Son("Hello son ");//调用父类无参构造方法,然后调用子类含参构造方法。
Son s3=new Son("Hello son","Hello father");//
}
}
结果:
this is father
Hello father
this is father
this is son
this is father
this is son
Hello son
Hello son Hello father
Hello son
相关文章推荐
- java父类子类中静态块非静态块构造方法的执行顺序和继承
- Java子类继承父类,构造方法的执行顺序问题
- java中父类的静态代码块,非静态代码块,构造方法,子类的静态代码块,构造方法等先后执行顺序
- 父类与子类的静态代码块、代码块、构造方法执行顺序
- 静态语句块,非静态语句块,构造方法在子类和父类的执行顺序
- JAVA父类子类静态代码块、静态变量、构造方法的执行顺序
- Java继承中父类子类构造方法、静态代码块、非静态代码块的执行顺序
- JAVA中静态代码块、构造方法、Super()父类与子类之间执行顺序
- 父类子类静态代码块,构造方法的执行顺序
- 子类和父类的 static块 和 构造方法 构造块 的执行顺序
- 父类子类构造方法、静态代码块、非静态代码块的执行顺序
- 【JAVA】探究JAVA父类子类静态代码块、静态变量、构造方法的执行顺序
- java中 静态成员、实例成员、构造方法在子类和父类中的执行顺序
- 关于java中子类,父类中,静态代码块: staic{},动态代码块:{},构造方法,类属性,对象属性等执行顺序做个总结:
- 子类继承父类中的static模块、构造方法执行顺序及java多态性
- java父类子类静态块、块、构造方法的执行顺序
- 父类和子类静态代码块、非静态代码块、构造函数(方法)执行的先后顺序问题
- java父类,子类静态块和构造方法的执行顺序
- 尝试创建一个父类和子类,分别创建一个构造方法,然后向父类和子类添加成员变量和方法,并总结构造子类对象时的顺序。
- Java子类继承父类,静态代码块、普通代码块、构造代码块的执行顺序