首页 > 数据库技术 > 详细

【转载】Accessing a Derived Class Using a Base Class Variable

时间:2014-03-12 02:45:34      阅读:436      评论:0      收藏:0      [点我收藏+]

You can use a variable whose type is a base class to reference instances of a derived class.

However, using the variable whose type is the base class, you can use it to access only members of the base class and not members of the derived class.

In the example below, we have two instances of the Terrier class, which derives from Dog. One instance is referenced by a variable of type Terrier. Using this variable, you have access to all members of the Terrier class. On the other hand, the variable whose type is Dog can only reference members of the Dog class, even though the reference points to an instance of a Terrier.

1 Terrier bubba = new Terrier("Bubba", 2, "Happy");
2 bubba.Growl();         // Can call Terrier.Growl
3 
4 Dog jack = new Terrier("Jack", 17, "Surly");
5 jack.Growl();             // ERROR: Can‘t call Growl method

【转载】Accessing a Derived Class Using a Base Class Variable,布布扣,bubuko.com

【转载】Accessing a Derived Class Using a Base Class Variable

原文:http://www.cnblogs.com/yuthreestone/p/3594702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!