[Java] inheritance-related issues

Parent-child inheritance relationship among classes, member variables are duplicate names

/*
在父子类的继承关系当中,如果成员变量重名,则创建子类对象时,访问有两种方式:

直接通过子类对象访问成员变量:
    等号左边(new对象赋值语句的左边的引用名)是谁,就优先用谁,没有则向上找。
间接通过成员方法访问成员变量:
    该方法属于谁(new的对象是谁),就优先用谁,没有则向上找。
 */
public class Demo01ExtendsField {

    public static void main(String[] args) {
        Fu fu = new Fu(); // 创建父类对象
        System.out.println(fu.numFu); // 只能使用父类的东西,没有任何子类内容
        System.out.println("===========");

        Zi zi = new Zi();

        System.out.println(zi.numFu); // 10
        System.out.println(zi.numZi); // 20
        System.out.println("===========");

        // 等号左边是谁,就优先用谁,这里zi这个对象是Zi类,所以是Zi类中的num变量
        System.out.println(zi.num); // 优先子类,200
//        System.out.println(zi.abc); // 到处都没有,编译报错!
        System.out.println("===========");

        // 这个方法是子类的,优先用子类的,没有再向上找
        zi.methodZi(); // 200
        // 这个方法是在父类当中定义的,
        zi.methodFu(); // 100
    }
}

How to inherit variables related call

Local variables within the method: direct write names of local variables
of this class member variables: this member variable name.
Member variables of the parent class:. Super member variable name

Access order inheritance neutron class and the parent class

/*
在父子类的继承关系当中,创建子类对象,访问成员方法的规则:
    创建的对象是谁,就优先用谁,如果没有则向上找。

注意事项:
无论是成员方法还是成员变量,如果没有都是向上找父类,绝对不会向下找子类的。

重写(Override)
概念:在继承关系当中,方法的名称一样,参数列表也一样。

重写(Override):方法的名称一样,参数列表【也一样】。覆盖、覆写。
重载(Overload):方法的名称一样,参数列表【不一样】。

方法的覆盖重写特点:创建的是子类对象,则优先用子类方法。
 */
public class Demo01ExtendsMethod {

    public static void main(String[] args) {
        Zi zi = new Zi();

        zi.methodFu();
        zi.methodZi();

        // 创建的是new了子类对象,所以优先用子类方法
        zi.method();
    }
}

Methods overwritten Notes

Note rewritten method covering:

  1. We must ensure that the same method between father and son class name, parameter list is the same.
    @Override: written before the method for detecting a valid right is not overwritten.
    This comment does not even write, as long as meet the requirements, but also the right approach overwritten.

  2. The return value must subclass method [less] parent class method returns the value range.
    Small extension Note: java.lang.Object class is the highest of all classes of common parent (ancestor class), java.lang.String is a subclass of Object.

  3. Subclasses must permission method or greater] [parent process permission modifier.
    Small extension Note: public> protected> (default) > private
    Remarks: (default) is not a keyword default, but nothing to write, leave it blank.

Inheritance, the access characteristics of the parent-child class constructor

  1. Constructor of a subclass which has a default implicit "super ()" call, so be sure is to call the constructor of the parent class, subclass constructor after execution.
  2. Subclass constructor can be called the father of the overloaded constructors of the super keyword.
  3. parent class constructor calls super must be the first statement of the subclass constructor. Not a sub-class constructor called many times super structure.
    Summary:
    The subclass must call the parent class constructor, do not write the gift super (); written with the write call specified super, super can only have one, it must also be the first.
public class Fu {

    public Fu() {
        System.out.println("父类无参构造");
    }

    public Fu(int num) {
        System.out.println("父类有参构造!");
    }
}
public class Zi extends Fu {

    public Zi() {
        super(); // 在调用父类无参构造方法
//        super(20); // 在调用父类重载的构造方法
        System.out.println("子类构造方法!");
    }

    public void method() {
//        super(); // 错误写法!只有子类构造方法,才能调用父类构造方法。
    }
}

three uses the super keyword

  1. A member of the subclass, the access member variables of the parent class.
  2. Subclass members, the visit members of the parent class.
  3. In the constructor of a subclass, the access structure of the parent class.

this three uses keyword

super keyword is used to access the parent class content, but this keyword is used to access this type of content. There are also three usage:

  1. In the class member method, access the member variables of this class.
  2. In the method of the present class member, another member of this class access method.
  3. In the present method, the constructor, another configuration access to the class method.
    In the third use them to pay attention to:
    A. the this (...) call must be the first statement in the constructor, the only one.
    B. super and this call two configurations, not both.

this memory map and the super keyword

Here Insert Picture Description

Java only single inheritance, but to support multi-level inheritance

Here Insert Picture Description

Published 218 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/u011035397/article/details/104975327