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. Ansible自动化运维工具

    ansible软件介绍 python语言是运维人员必会的语言!  ansible是一个基于Python开发的自动化运维工具!(saltstack)  其功能实现基于SSH远程连接服务!  ans ...

  2. Python Numpy Array

    Numpy 是Python中数据科学中的核心组件,它给我们提供了多维度高性能数组对象. Arrays Numpy.array   dtype 变量 dtype变量,用来存放数据类型, 创建数组时可以同 ...

  3. 转载 Some indexes or index [sub]partitions of table VAS.TAB_PUB_CALLLOG have been marked unusable

    http://www.xifenfei.com/2011/12/some-indexes-or-index-subpartitions-of-table-vas-tab_pub_calllog-hav ...

  4. IIS 配置asp.net 环境

    打开Internet信息服务管理器,在左侧点击“应用程序池”,下面将以asp.net v4.0 程序池为例 在中间的“ASP.NET V4.0”上右键“高级选项”,展开“进程模型”,找“标识”选项,然 ...

  5. java 静态变量生命周期(类生命周期)(转)

    Static: 加载:java虚拟机在加载类的过程中为静态变量分配内存. 类变量:static变量在内存中只有一个,存放在方法区,属于类变量,被所有实例所共享 销毁:类被卸载时,静态变量被销毁,并释放 ...

  6. 关于Mysql数据库的注意点

    1.注意属性为String的数据在JDBC操作语句中要加单引号 例子: conn = DriverManager.getConnection("jdbc:mysql://localhost: ...

  7. HDU 4323——Magic Number——————【dp求编辑距离】2012——MUT——3

    Magic Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  8. 开发安全的 API 所需要核对的清单

    开发安全的 API 所需要核对的清单 以下是当你在设计, 测试以及发布你的 API 的时候所需要核对的重要安全措施. 身份认证 不要使用 Basic Auth 使用标准的认证协议 (如 JWT, OA ...

  9. Day6 下(

    T1 模拟,80? #include<iostream> #include<cstring> #include<queue> #include<algorit ...

  10. Xtrareport二之固定数据绑定

    已经了解了XtraReport的初步用法,现在在进一步了解数据绑定 我们还是先不整高深的,先来个写死的,让我们的数据库可以通过报表呈现先 1. 准备 还在上节基础上,选中设计器report的page  ...