欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

父类对象引用子类对象规则

程序员文章站 2024-03-21 19:07:46
...
public class People {
    public void eat(){
        System.out.println("吃饭");
    }
}
public class Ray extends People {
    public void beCool(){
        System.out.println("cool");
    }
    public void eat(){
        System.out.println("ray吃饭");
    }
}
public class Main {
    public static void main(String[] args) {
        Ray ray = new Ray();
        People[] people = new People[3];
        people[0].eat();
    }
}

父类对象引用子类对象规则