java多态向上转型和向下转型

来源:互联网 发布:js手机上下滑动事件 编辑:程序博客网 时间:2024/06/10 07:04
interface Animal {    int a = 1;    int eat();}public class Test implements Animal{    public static void main(String[] args) {        //向上转型        Animal animal = new Test();        //向上转型后可以再转回来(如何不是向上转型的animal往下转编译能通过,        //但是调用子特有的方法的会报错,因为他里面只有父的方法)        Test test = (Test)animal;    }    public int eat() {        return 0;    }}
原创粉丝点击