Small talk compile-time and run-time type type

Java reference variable has two types, one is the compile-time type, there is a run-time type.

Compile-time type is the type used by declaring the variable is determined, the runtime object type is determined by the type of the variable points

if not both types, there will be multi-state, and therefore will be assigned to the subclass objects between the parent class reference variable, called the upward transition, rather than type conversion. The Animal a = new Bird (), wherein a reference variable compile time type Animal, Bird runtime type is, do not coincide, and therefore the polymorphic appears

that when using a call to the object reference, there is such a rule, the object when the type of call attributes compile and runtime type of process

we see an example to verify directly the phrase

class the Person {
    public String name;

    public the Person () {
        name = "Person";
    }

    public void Show () {
        the System.out .println ( "Person Show");
    }
}

class Woman the extends the Person {
    public String name;

    public Woman () {
        name = "Woman";
    }

    public void Show () {
        System.out.println ( "
    }
}

public class TestDemo {

    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);
        person.show();

        System.out.println();

        Person person1 = new Woman();
        System.out.println(person1.name);
        person1.show();

        System.out.println();

        Woman woman = new Woman();
        System.out.println(woman.name);
        woman.show();
    }

}


结果:

person
person show

person
woman show

woman
woman show

    1
    2
    3
    . 4
    . 5
    . 6
    . 7
    . 8

can be seen from the results, the first statement cited property called person belongs to the class Person, the person calls a reference method or the Person class; so after

three reference

https://blog.csdn.net/ snow_7 / Article This article was / the Details / 51,579,278
https://blog.csdn.net/qq_29513537/article/details/60765552
https://blog.csdn.net/qq_23419401/article/details/52064871#java

Guess you like

Origin www.cnblogs.com/jenny2019/p/11674027.html
Recommended