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;
}
}
java多态向上转型和向下转型
最新推荐文章于 2024-05-21 18:54:19 发布