已有56人关注
类的问题
发表在C#图书答疑 2009-11-04
是否精华
版块置顶:
public class Animal 

public string word=""; 
public virtual void Introduce() 

word="I'm a Animal"; 


public class cat:Animal 


public new  void Introduce() 

word="I'm a cat"; 



Animal theAnimal=new cat(); 
theAnimal.Introduce(); 
MessageBox.show(theAnimal.word); 
结果为I'm a Animal 
而cat theAnimal=new cat();结果为I'm a cat 
new关键字不是用来区别派生类和基类同名方法选择问题,来隐藏基类方法吗?为什么第一个的结果还为I'm a Animal
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2009-11-06
沙发
读者朋友:
    您好,public new void Introduce()这里面的这个new关键字是实例化一个新的同名方法,但并不覆盖基类中的方法,Animal theAnimal=new cat(); 这条语句是使用派生类来实例化基类的对象,所以它调用的还是基类中的方法;而cat theAnimal=new cat();这条语句实例化的就是派生类的对象,它调用的也就是派生类中的方法。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照