C++与Java比较之向上转型

前几天看Mars老师的视频教程,里面有这样一句“一个引用能调用那些成员属性(变量和函数),取决于引用的类型;调用的方法取决于引用所指向的对象。(向上转型)”

今天在使用C++的upcast时感觉不对啊?网上搜了下原来Java和C++的向上转型的机制不同。

C++向上转型使用静态邦定,所以在无法识别类类型引用或指针调用对应类的成员函数方法

c++示例:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Base  
  2. {  
  3. public:  
  4.     virtual void display()   //虚方法,可以被子类重写
  5.     {  
  6.         cout<<"Base display()"<<endl;   
  7.     }  
  8.     void say()  
  9.     {    
  10.         cout<<"Base say()"<<endl;    
  11.     }  
  12.   
  13.     void exec()  
  14.     {   
  15.         display();   
  16.         say();   
  17.     }  
  18.   
  19.     void f1(string a)    
  20.     {   
  21.         cout<<"Base f1(string)"<<endl;   
  22.     }  
  23.   
  24.     void f1(int a)   
  25.     {   
  26.         cout<<"Base f1(int)"<<endl;   
  27.     }   //overload  
  28. };  
  29.   
  30. class DeriveA:public Base  
  31. {  
  32. public:  
  33.     void display()   
  34.     {   
  35.         cout<<"DeriveA display()"<<endl;   
  36.     }   //override  
  37.   
  38.     void f1(int a,int b)   
  39.     {   
  40.         cout<<"DeriveA f1(int,int)"<<endl;   
  41.     }   //redefining  
  42.   
  43.     void say()   
  44.     {   
  45.         cout<<"DeriveA say()"<<endl;   
  46.     }   //redefining  
  47. };  
  48.   
  49. int main()  
  50. {  
  51.     DeriveA a;  
  52.     Base *b = &a;  
  53.     b->say();   
  54.     a.say();  
  55.     b->exec();  //display():version of DeriveA call(polymorphism)   say():version of Base called(allways )  
  56.       
  57.     a.exec();  //same result as last statement  
  58.          
  59.     return 0;  
  60. }   

运行结果:

Base say()
DeriveA say()
DeriveA display()
Base say()
DeriveA display()
Base say()

Java示例:

JAVA中采用动态邦定技术,所以在程序中自动识别,调用的方法取决于引用所指向的对象。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Instrument  
  2. {  
  3.     public void play()  
  4.     {  
  5.         System.out.println("Instrument calling ");  
  6.     }  
  7.   
  8. }  
  9.   
  10. class Piano extends Instrument  
  11. {  
  12.     public void play()  
  13.     {  
  14.         System.out.println("Piano calling");  
  15.     }  
  16.     public void description()  
  17.     {  
  18.         System.out.println("a Piano is a kind of Instrument");  
  19.     }  
  20. }  
  21.   
  22. public class Upcast  
  23. {  
  24.     public static void main(String args[])  
  25.     {  
  26.         Instrument i = new Piano();   
  27.         i.play();  
  28.   
  29.         //error  
  30.         //i.description();  
  31.         ((Piano)i).description();  
  32.     }  
  33. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值