Java object-oriented-inheritance (2) how to access member methods and member variables

table of Contents

Preface

1. Access member variables

1.1 Rules

1.2 Code implementation

1.3 How to distinguish local variables, member variables of this class and member variables of the parent class when accessing in a class?

2. Access member method

2.1 Rules

2.2 Code implementation


Preface

The last article introduced the definition of inheritance, class diagram and code implementation. Today, I will continue to take you to learn how to access member variables and member methods in inherited classes.

1. Access member variables

1.1 Rules

Two cases (duplicate and non-duplicate member variables)

When the member variable does not have the same name: [1] If you create a parent object, you can only use the parent class, without any subclass. [2] If you create a subclass object, that is, a subclass of new, then the member variables and member methods of the subclass's parent class can be used, and the subclass does not have methods to find the parent class.

In the inheritance relationship of parent and child, if the member variable has the same name, there are two ways to access when creating the child object:

[3] Direct access to member variables through subclass objects: Whoever is on the left of the equal sign will give priority to whoever is used, and if not, look up (parent class).

[4] Indirect access to member variables through member methods: Whoever belongs to the method will be used first, and if not, look up.

1.2 Code implementation

//父类
public class Fu {
    int numFu=10;
    int num=100;

    public void methodFu(){
        //用的是本类当中的num
        System.out.println(num);
    }
}
//子类
public class Zi extends Fu{
    int numZi=20;
    int num=200;

    public void methodZi(){
        //因为本类当中有num,所以这里用的是本类的num
        System.out.println(num);
    }
}
//使用
public class Demo01ExtendsField {
    public static void main(String[] args) {
        //【1】如果创建的父类对象,那么只能使用父类的东西,没有任何子类的东西。
        Fu fu=new Fu();     //创建父类对象
        System.out.println(fu.numFu);   //10
        System.out.println(fu.num);     //100

        //【2】如果创建的是子类对象,也就是说new的子类,那么子类父类的成员变量和成员方法都可以使用.
        Zi zi=new Zi();
        System.out.println(zi.numZi);       //20
        System.out.println(zi.numFu);       //10
        System.out.println("*****************************");

        //【3】重名:直接通过子类对象访问成员变量:等号左边是谁,就优先用谁,没有则向上(父类)找。
        System.out.println(zi.num);     //优先子类,200

        //【4】重名:间接通过成员方法访问成员变量:    该方法属于谁,就优先用谁,没有则向上找。
        zi.methodZi();      //200 ,这个方法是子类的,优先用子类的,没有再向上(父类)
        zi.methodFu();      //100 ,这个方法实在父类当中定义的,属于父类,优先使用父类
    }
}

1.3 How to distinguish local variables, member variables of this class and member variables of the parent class when accessing in a class?

format:

Local variable: directly write the member variable name

Member variables of this class: this.Member variable name

The member variable of the parent class: super. member variable name

//父类
public class Fu {
    int num=10;
}
//子类
public class Zi extends Fu {
    int num=20;
    public void method(){
        int num=30;
        System.out.println(num);        //30,局部变量
        System.out.println(this.num);   //20,本类的成员变量
        System.out.println(super.num);  //30,父类的成员变量
    }
}

2. Access member method

2.1 Rules

In the inheritance relationship of the parent and child classes, the rules for creating subclass objects and accessing member methods: Whoever is the created object will be used first, if not, look up (the parent class). If the subclass overrides the method of the parent class, the subclass object is created, and the subclass method is preferred.

2.2 Code implementation

public class Fu {
    public void methodFu(){
        System.out.println("父类方法执行!");
    }

    public void method(){
        System.out.println("父类重名方法执行!");
    }
}

public class Zi extends Fu {
    public void methodZi(){
        System.out.println("子类方法执行!");
    }

    public void method(){
        System.out.println("子类重名方法执行!");
    }

}

public class Demo01ExtendsMethod {
    public static void main(String[] args) {
        Fu fu=new Fu();
        fu.methodFu();  //父类方法执行!

        Zi zi=new Zi();
        zi.methodZi();   //子类方法执行!new子类,优先用子类
        zi.methodFu();  //父类方法执行!子类当中没有,则向父类找

        //创建的是new子类对象,所以优先使用子类方法
        zi.method();        //子类重名方法执行!
    }
}

 

Guess you like

Origin blog.csdn.net/wtt15100/article/details/108117150