JAVA 多态

多态是编程语言给不同的底层数据类型做相同的接口展示的一种能力。一个多态类型上的操作可以应用到其他类型的值上面

package peoplePack;

public class JavaApp {
    public static void main(String[] args) {
        Young y = new Young();
        y.testInstanceMethod();
        
        People p = new Young();
        p.testInstanceMethod();
        
        People.testClassMethod();
        Young.testClassMethod();
    }
}

class People {
    public static void testClassMethod(){
        System.out.println("Static Method in People");
    }
    
    public void testInstanceMethod(){
        System.out.println("Instance Method in People");
    }
}

class Young extends People{
    public static void testClassMethod(){
        System.out.println("The static method in Young");
    }
    
    public void testInstanceMethod(){
        System.out.println("The instance method in Young");
    }
}

 

Output:

The instance method in Young
The instance method in Young
Static Method in People
The static method in Young

 

Reference

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。