Basis (the relationship between inherited members) JAVA

1, the relationship between the succession of a member variable

  • Different variable names

    • Subclass own variables

  • Variable with the same name

    • Appearing parent subset of variables in the same name but with a sub lecture example, in the development of this situation it is not

    • Subclass inherits the parent class is to use members of the parent class, so if you define a member variable of the same name does not make sense


 

class Demo4_Extends {

    public static void main(String[] args) {

        Son s = new Son();

        s.print();

    }

}



class Father {

    int num1 = 10;

    int num2 = 30;

}





class Son extends Father {

    int num2 = 20;

    // int num1 = 10;  --> 从父类中继承下来





    public void print() {

        System.out.println(this.num1);                //this既可以调用本类的,也可以调用父类的(本类没有的情况下)

        System.out.println(this.num2);                //就近原则,子类有就不用父类的了

        System.out.println(super.num2);

        

    }

}

2, the relationship between the constructor inheritance

class Demo5_Extends {

    public static void main(String[] args) {

        Son s = new Son();

    }

}



class Father extends Object {

    /*public Father() {

        super();

        System.out.println("Father 的构造方法");

    }*/





    public Father(String name, int age){

    

    }

}





class Son extends Father {

    public Son() {

        super();                            //这是一条语句,如果不写,系统会默认加上,用来访问父类中的空参构造

        System.out.println("Son 的构造方法");

    }

}

 

[1] All subclasses default constructor will access the parent class constructor parameter hollow

  • Because a subclass inherits the data in the parent class, you may also use the data in the parent class. So, before the subclass initialization, be sure to complete the initialization of the parent class data.

  • Each of the first statement is a default constructor: super () Object Class topmost parent class.

 

3 Notes inheritance constructor

  • Case:

class Demo6_Extends {

    public static void main(String[] args) {

        Son s1 = new Son();

        System.out.println(s1.getName() + "..." + s1.getAge());

        System.out.println("--------------------");

        Son s2 = new Son("张三",23);

        System.out.println(s2.getName() + "..." + s2.getAge());

    }

}

/*

* A:案例演示

    * 父类没有无参构造方法,子类怎么办?

    * super解决

    * this解决

* B:注意事项

    * super(…)或者this(….)必须出现在构造方法的第一条语句上

*/

class Father {

    private String name;            //姓名

    private int age;                //年龄





    public Father() {                //空参构造

        System.out.println("Father 空参构造");

    }





    public Father(String name,int age) {    //有参构造

        this.name = name;

        this.age = age;

        System.out.println("Father 有参构造");

    }





    public void setName(String name) {    //设置姓名

        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() {                        //空参构造

        this("王五",25);                //本类中的构造方法

        //super("李四",24);                //调用父类中的构造方法

        

        System.out.println("Son 空参构造");

    }





    public Son(String name,int age) {    //有参构造

        super(name,age);

        System.out.println("Son 有参构造");

    }

}

 

  • 注意事项
    
    super(…)或者this(….)必须出现在构造方法的第一条语句上
    
        1,创建son();先走super父类。Father有参构造。
    
        2,son 有参构造。
    
        3,son 无参构造。
    
        4,输出名字。
    
    

     

 

4, members inherited method relations

  • Method with a different name

  • The method of rewriting the same name. If you want to call the parent class members in the sub-class object. super. The parent class method.

class Demo7_Extends {

    public static void main(String[] args) {

        Son s = new Son();

        s.print();

        s.method();

    }

}



class Father {

    public void print() {

        System.out.println("Fu print");

    }

}



class Son extends Father {

    public void method() {

        System.out.println("Zi Method");

    }



    public void print() {

        super.print();                            //super可以调用父类的成员方法

        System.out.println("Zi print");

    }

}

 

5, the process is called class


class Test2_Extends {

    public static void main(String[] args) {

        Zi z = new Zi();

    }

    /*

    1,jvm调用了main方法,main进栈

    2,遇到Zi z = new Zi();会先将Fu.class和Zi.class分别加载进内存,再创建对象,当Fu.class加载进内存

    父类的静态代码块会随着Fu.class一起加载,当Zi.class加载进内存,子类的静态代码块会随着Zi.class一起加载

    第一个输出,静态代码块Fu,第二个输出静态代码块Zi

    3,走Zi类的构造方法,因为java中是分层初始化的,先初始化父类,再初始化子类,所以先走的父类构造,但是在执行

    父类构造时,发现父类有构造代码块,构造代码块是优先于构造方法执行的所以

    第三个输出构造代码块Fu,第四个输出构造方法Fu

    4,Fu类初始化结束,子类初始化,第五个输出的是构造代码块Zi,构造方法Zi

    */

}

class Fu {

    static {

        System.out.println("静态代码块Fu");

    }





    {

        System.out.println("构造代码块Fu");

    }





    public Fu() {

        System.out.println("构造方法Fu");

    }

}





class Zi extends Fu {

    static {

        System.out.println("静态代码块Zi");

    }





    {

        System.out.println("构造代码块Zi");

    }





    public Zi() {

        super();

        System.out.println("构造方法Zi");

    }

}



 

 

 

 

 

Guess you like

Origin blog.csdn.net/Cricket_7/article/details/92061082