博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】#335 - Accessing a Derived Class Using a Base Class Variable
阅读量:7282 次
发布时间:2019-06-30

本文共 896 字,大约阅读时间需要 2 分钟。

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.Growl3 4 Dog jack = new Terrier("Jack", 17, "Surly");5 jack.Growl();             // ERROR: Can't call Growl method

 原文地址:

转载于:https://www.cnblogs.com/yuthreestone/p/3594702.html

你可能感兴趣的文章
keepalived基础配置详解(一)
查看>>
LeetCode----204. Count Primes(Java)
查看>>
9大内置对象
查看>>
转载-vim配置收藏
查看>>
winform开发中的隐藏与展开实现原理
查看>>
C# 可变参数params的一个应用
查看>>
mysql 拉丁1 转换成 utf8
查看>>
获取url的hash值
查看>>
关于在CentOS下MRTG的正确安装方法
查看>>
linux 系统上开启uRPF
查看>>
表空间实际操作遇到的一个问题
查看>>
关于main函数的参数列表 String[] agrs
查看>>
HDU1287 破译密码
查看>>
GCC编译C时中文支持问题
查看>>
关于团队作业美工的设想
查看>>
图片浏览器的种类
查看>>
OpenCart中基于重量的配送模块是如何工作的?
查看>>
bash配置文件开机启动
查看>>
CodeForces 42A Guilty — to the kitchen!
查看>>
UnitedStack:不作Openstack发行版作服务
查看>>