集册 Java实例教程 演示早期绑定的结果。

演示早期绑定的结果。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

532
演示早期绑定的结果。

class MyBase {
/* 
*来 自
 时代Java
*/

    // An instance variable 

    public String str = "EarlyBindingSuper";


    // A static variable 

    public static int count = 100;


    public static void print() {

        System.out.println("Inside EarlyBindingSuper.print()");

    }

}


class MyClass extends MyBase{

    // An instance variable 

    public String str = "EarlyBindingSub";


    // A static variable /*n o w    j a v a  . c o m 提供*/

    public static int count = 200;


    public static void print() {

        System.out.println("Inside EarlyBindingSub.print()");

    }

}



public class Main {

  public static void main(String[] args) {

    MyBase ebSuper = new MyBase();

    MyClass ebSub = new MyClass();


    // Will access EarlyBindingSuper.str 

    System.out.println(ebSuper.str);


    // Will access EarlyBindingSuper.count 

    System.out.println(ebSuper.count);


    // Will access EarlyBindingSuper.print() 

    ebSuper.print();


    // Will access EarlyBindingSub.str 

    System.out.println(ebSub.str);


    // Will access EarlyBindingSub.count 

    System.out.println(ebSub.count);


    // Will access EarlyBindingSub.print() 

    ebSub.print();


    // Will access EarlyBindingSuper.str 

    System.out.println(((MyBase)ebSub).str);


    // Will access EarlyBindingSuper.count 

    System.out.println(((MyBase)ebSub).count);


    // Will access EarlyBindingSuper.print() 

    ((MyBase)ebSub).print();


    // Assign the ebSub to ebSuper 

    ebSuper = ebSub; // Upcasting 


展开阅读全文