【摘要】:所有的类都是直接或者间接继承自Object。同样,所有的子类也都隐含的“等于”其父类。那么,由子类造型至父类的操作就是不必要的了。Object o = dog; //not necessary.Nigel Warren, Philip Bishop: "Java inPractice - Design Styles and Idiomsfor Effective Java". Addison-Wesley, 1999. pp.22-23
所有的类都是直接或者间接继承自Object。同样,所有的子类也都隐含的“等于”其父类。那么,由子类造型至父类的操作就是不必要的了。
例子:
class UNC {
String _id = "UNC";
}
class Dog extends UNC {
void method () {
Dog dog = new Dog ();
UNC animal = (UNC)dog; // notnecessary.
Object o = (Object)dog; //not necessary.
}
}
更正:
class Dog extends UNC {
voidmethod () {
Dog dog = new Dog();
UNC animal = dog;
Object o = dog;
}
}
参考资料:
Nigel Warren, Philip Bishop: "Java inPractice - Design Styles and Idioms
for Effective Java". Addison-Wesley, 1999. pp.22-23
免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。