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.

 Terrier bubba = new Terrier("Bubba", , "Happy");
bubba.Growl(); // Can call Terrier.Growl Dog jack = new Terrier("Jack", , "Surly");
jack.Growl(); // ERROR: Can't call Growl method

原文地址:#335 - Accessing a Derived Class Using a Base Class Variable

【转载】#335 - Accessing a Derived Class Using a Base Class Variable的更多相关文章

  1. (转载)1248 - Every derived table must have its own alias

    (转载)http://hi.baidu.com/lylegend13/item/a79f17eb51f5dff7e0a5d43b 1. select count(distinct CName) fro ...

  2. 如何实现 Copying derived entities using only base class pointer

    #include <iostream> struct CloneableBase { ; }; template<class Derived> struct Cloneable ...

  3. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  4. 【转载】#330 - Derived Classes Do Not Inherit Constructors

    A derived class inherits all of the members of a base class except for its constructors. You must de ...

  5. [转载] google mock cookbook

    原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...

  6. C++ 强制类型转换(转载)

    转载自:http://www.weixueyuan.net/view/6329.html 在C++语言中新增了四个关键字static_cast.const_cast.reinterpret_cast和 ...

  7. 【转载】C++中的基类与派生类

    转自:http://www.cnblogs.com/sujz/articles/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的 ...

  8. [C++] OOP - Base and Derived Classes

    There is a base class at the root of the hierarchy, from which the other class inherit, directly or ...

  9. 【转载】#344 - Hidden Base Class Member Is Invoked Based on Declared Type of Object

    When you use the new modifier to hide a base class method, it will still be called by objects whose ...

随机推荐

  1. Python-Anaconda练习candy算子用于边缘提取,再用hough变换检测直线边缘

    img: 待检测的图像. threshold: 阈值,可先项,默认为10 line_length: 检测的最短线条长度,默认为50 line_gap: 线条间的最大间隙.增大这个值可以合并破碎的线条. ...

  2. 汉诺塔问题hdu 2065——找规律

    这类题目就是纸上模拟,找规律. 问题描述:在一块铜板上有三根杆,目的是将最左边杆上的盘全部移到右边的杆上,条件是不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允 ...

  3. java——平衡二叉树 AVLTree、AVLMap、AVLSet

    平衡二叉树:对于任意一个节点,左子树和右子树的高度差不能超过1 package Date_pacage; import java.util.ArrayList; public class AVLTre ...

  4. vue 的watch用法

    参考转自https://www.imooc.com/article/details/id/28187 类型:{ [key: string]: string | Function | Object | ...

  5. java替换word2003

    map.put("year", year);            map.put("yearMonthDay", yearMonthDay);         ...

  6. 使用taobao的npm镜像源,同时安装nrm镜像工具

    身在天朝.你面临的一个问题就是网络 安装好Nodejs 控制台 执行 npm config set registry https://registry.npm.taobao.org //配置指向源 然 ...

  7. 百度BAE数据库连接问题

    今天第一次使用百度的开发平台BAE,按照入门文档上的操作一步步来,进行的很顺利,可是我在上传了一个cms系统后,进行安装时,卡在了数据库连接这个地方,弄了一下午,终于有了结果,在这里记录起来,希望能帮 ...

  8. GitKraken使用教程-基础部分(8)

    9.  远程(Remote)仓库 1) 添加远程仓库 一般在本地新建仓库后,需要添加一个远程仓库用于push/pull代码.鼠标移至GitKraken左侧区域的REMOTE栏,点击 该栏右边出现的 按 ...

  9. C++ map的基本操作和用法

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  10. Cordova 常用的插件汇总

    1.分享到微信:cordova-plugin-wechat 地址:https://github.com/xu-li/cordova-plugin-wechat 安装: cordova plugin a ...