复制代码
- //:RTTI.java
- //Downcasting&Run-TimeType
- //Identification(RTTI)
- importjava.util.*;
- classUseful{
- publicvoidf(){}
- publicvoidg(){}
- }
- classMoreUsefulextendsUseful{
- publicvoidf(){}
- publicvoidg(){}
- publicvoidu(){}
- publicvoidv(){}
- publicvoidw(){}
- }
- publicclassRTTI{
- publicstaticvoidmain(String[]args){
- Useful[]x={
- newUseful(),
- newMoreUseful()
- };
- x[0].f();
- x[1].g();
- //Compile-time:methodnotfoundinUseful:
- //!x[1].u();
- ((MoreUseful)x[1]).u();//Downcast/RTTI
- ((MoreUseful)x[0]).u();//Exceptionthrown
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |