Variables same name and the same name appearing Java method and class neutron parent class problem explanation

Variables appear to explain the same name and the same name as methodological issues, following Cheng Shizi class and the parent class

1. member variable of the same name

public class Test2 {
    public static void main (String[] args) {
        ZI zi = new ZI();
        FU fu = new FU();
        System.out.println(zi.a);
        System.out.println(fu.a);
    }
}
class FU{
    int a=10;
}
class ZI extends FU{
    int a=20;
}
/*
输出结果为20  10
*/
public class Test2 {
    public static void main (String[] args) {
        ZI zi = new ZI();
        zi.show();
    }
}
class FU{
    int a=10;
    public void show(){
        System.out.println(a);
    }
}
class ZI extends FU{
    int a=20;
}
/*
输出结果为10
*/
public class Test2 {
    public static void main (String[] args) {
        ZI zi = new ZI();
        zi.show();
    }
}
class FU{
    int a=10;

}
class ZI extends FU{
    int a=20;
    public void show(){
        System.out.println(a);
    }
}
/*
输出结果为20
*/

Display can be seen from the above code, when a variable appears following the Cheng Shizai members of classes and parent classes, are independent variables of the same name; if direct access is only based on the type of object to; call if the method according to which is defined in the class, the parent classes were calling the parent class, sub-class calls subclasses.

2. When members of the methods of the same name

public class Test2 {
    public static void main (String[] args) {
        ZI zi = new ZI();
        zi.show();
        zi.A();
        FU fu = new FU();
        fu.show();
        fu.A();
        FU fu1=new ZI();
        fu1.show();
        fu1.A();
    }
}
class FU{
    public void A(){
        System.out.println("A方法");
    }
   public void show(){
       System.out.println("FU SHOW");
   }
}
class ZI extends FU{
    public void show(){
        System.out.println("ZI SHOW");

    }
}
/*
输出结果为
ZI SHOW
A方法
FU SHOW
A方法
ZI SHOW
A方法
*/

NOTE: FU parent class object, ZI is the subclass object, FU1 subclasses of the parent class object instantiated (upwards achieve transformation)

Code implements the above functions can be obtained: the parent object and child objects of this class object class can call; subclass but no parent class inheritance conventional method will be realized; upward transition in the realization, the parent class and subclass the method of the method of the same name will be overwritten subclasses override (override method), method of the same name will not be inherited subclass, when the test class method calls, the method will override the whichever method call.

to sum up

Inheritance can only cover method (method overrides), but can not cover the member variables, when a variable of the same name, between the independent variables, when direct access to variables, acquired according to the type of object in front of the variable, according to the time at which the calling object class call.
Once the method is rewritten, the rewritten method are subject when called again for execution.
Parent class object instantiated subclasses (i.e. polymorphic transition or upward) is based on inheritance and subclasses of the parent class method override achieved. No inheritance and rewriting method, polymorphism is out of the question.

Published 10 original articles · won praise 0 · Views 122

Guess you like

Origin blog.csdn.net/DONG__CHI/article/details/103585129