`
talin2010
  • 浏览: 520761 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

17.5.3 Virtual methods

阅读更多
When an instance method declaration includes a virtual modifier, that method
is said to be a virtual method.
When no virtual modifier is present, the method is said to be a non-virtual
method.
The implementation of a non-virtual method is invariant: The implementation
is the same whether the method is
invoked on an instance of the class in which it is declared or an instance
of a derived class. In contrast, the
implementation of a virtual method can be superseded by derived classes.
The process of superseding the
implementation of an inherited virtual method is known as overriding that
method (§17.5.4).
In a virtual method invocation, the run-time type of the instance for which
that invocation takes place determines
the actual method implementation to invoke. In a non-virtual method
invocation, the compile-time type of the
instance is the determining factor. In precise terms, when a method named N
is invoked with an argument list A
on an instance with a compile-time type C and a run-time type R (where R is
either C or a class derived from C),
the invocation is processed as follows:
? First, overload resolution is applied to C, N, and A, to select a
specific method M from the set of methods
declared in and inherited by C. This is described in §14.5.5.1.
? Then, if M is a non-virtual method, M is invoked.
? Otherwise, M is a virtual method, and the most derived implementation of
M with respect to R is invoked.
For every virtual method declared in or inherited by a class, there exists
a most derived implementation of the
method with respect to that class. The most derived implementation of a
virtual method M with respect to a class R
is determined as follows:
? If R contains the introducing virtual declaration of M, then this is the
most derived implementation of M.
? Otherwise, if R contains an override of M, then this is the most derived
implementation of M.
? Otherwise, the most derived implementation of M is the same as that of
the direct base class of R.
[Example: The following example illustrates the differences between virtual
and non-virtual methods:
C# LANGUAGE SPECIFICATION
234
using System;
class A
{
public void F() { Console.WriteLine("A.F"); }
public virtual void G() { Console.WriteLine("A.G"); }
}
class B: A
{
new public void F() { Console.WriteLine("B.F"); }
public override void G() { Console.WriteLine("B.G"); }
}
class Test
{
static void Main() {
B b = new B();
A a = b;
a.F();
b.F();
a.G();
b.G();
}
}
In the example, A introduces a non-virtual method F and a virtual method G.
The class B introduces a new nonvirtual
method F, thus hiding the inherited F, and also overrides the inherited
method G. The example produces
the output:
A.F
B.F
B.G
B.G
Notice that the statement a.G() invokes B.G, not A.G. This is because the
run-time type of the instance (which
is B), not the compile-time type of the instance (which is A), determines
the actual method implementation to
invoke. end example]
Because methods are allowed to hide inherited methods, it is possible for a
class to contain several virtual
methods with the same signature. This does not present an ambiguity
problem, since all but the most derived
method are hidden. [Example: In the example
using System;
class A
{
public virtual void F() { Console.WriteLine("A.F"); }
}
class B: A
{
public override void F() { Console.WriteLine("B.F"); }
}
class C: B
{
new public virtual void F() { Console.WriteLine("C.F"); }
}
class D: C
{
public override void F() { Console.WriteLine("D.F"); }
}
Chapter 17 Classes
235
class Test
{
static void Main() {
D d = new D();
A a = d;
B b = d;
C c = d;
a.F();
b.F();
c.F();
d.F();
}
}
the C and D classes contain two virtual methods with the same signature:
The one introduced by A and the one
introduced by C. The method introduced by C hides the method inherited from
A. Thus, the override declaration in
D overrides the method introduced by C, and it is not possible for D to
override the method introduced by A. The
example produces the output:
B.F
B.F
D.F
D.F
Note that it is possible to invoke the hidden virtual method by accessing
an instance of D through a less derived
type in which the method is not hidden. end example]
分享到:
评论

相关推荐

    Python库 | sht-sensor-17.5.3.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:sht-sensor-17.5.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    17.5.3(K-Means、PCA、异常检测)1

    K-Means聚类算法是机器学习领域中的一个基础方法,尤其在非监督学习中被广泛使用。它的目标是将数据集分成多个类别,使得同一类别的样本间距离尽可能小,而不同类别间的样本距离尽可能大。K-Means的核心步骤包括: ...

    CoreliGrafxOriginsprov17.5.3.3中文安装免费版(附注册机序列号)

    Corel iGrafx Origins是一款由corel公司推出的企业级办公软件,该软件的主要作用就是用于各类业务流程的分析操作,包含了FlowCharter、IDEF0、Process等实用工具,可以用于各类流程、BPMN基本协作图、BPMN多通道协作...

    郵件伺服器 MDaemon 12.5.3

    比特旺公司出品的邮件服务器软件,使用本补丁请先安装MDaemon12.5.3英文原版,安装原版后替换原版中文件即可随意注册。 附序号产生器 , 本补丁用于测试,测试后请24小时内删除。如果正式使用,请购买正版软件。

    LPC1700系列微控制器用户手册

    17.5.3 从机模式 17.5.4 异常状况 17.6 管脚描述 17.7 寄存器描述 17.7.1 SPI控制寄存器 17.7.2 SPI状态寄存器 17.7.3 SPI数据寄存器 17.7.4 SPI时钟计数器寄存器 17.7.5 SPI测试控制寄存器 17.7.6 SPI测试状态...

    python3.6.5参考手册 chm

    PEP 486: Make the Python Launcher aware of virtual environments PEP 488: Elimination of PYO files PEP 489: Multi-phase extension module initialization Other Language Changes New Modules typing ...

    IMAGE and VIDEO COMPRESSION for MULTIMEDIA ENGINEERING Fundamentals, Algorithms, and Standards.part1.rar

    Stochastic Methods 14.2.2 Spatial Domain Methods vs. Frequency Domain Methods 14.2.3 Region-Based Approaches vs. Gradient-Based Approaches 14.2.4 Forward vs. Backward Motion Estimation ...

    数据挖掘原理与SPSS Clementine应用-数据挖掘模型评价

    17.1基于损失函数的标准 17.1.1混淆矩阵 17.1.2准确率及误差的度量 17.1.3两个评价模型成本的可视化工具 17.1.4评估分类器的准确率 17.2基于统计检验的准则 17.2.1统计模型之间的距离 ...17.5.3遗传算法 17.6小结

    spring chm文档

    17.5.3. 注册bean映射 17.5.4. 注册自己的处理方法 17.5.5. 使用XFire来暴露Web服务 17.6. 对远程接口不提供自动探测 17.7. 在选择这些技术时的一些考虑 18. Enterprise Java Bean(EJB)集成 18.1. 简介 18.2....

    Linux程序设计 第4版.haozip01

    17.5.3 qinputdialog 618 17.5.4 使用qmake简化makefile文件的编写 620 17.6 kde的菜单和工具栏 620 17.7 使用kde/qt编写cd数据库应用程序 624 17.7.1 主窗口 624 17.7.2 addcddialog 627 17.7.3 logondialog ...

    Linux程序设计 第4版.haozip02

    17.5.3 qinputdialog 618 17.5.4 使用qmake简化makefile文件的编写 620 17.6 kde的菜单和工具栏 620 17.7 使用kde/qt编写cd数据库应用程序 624 17.7.1 主窗口 624 17.7.2 addcddialog 627 17.7.3 logondialog ...

    Spring中文帮助文档

    2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 ...2.2.5. 在classpath中自动搜索组件 ...2.3.3. 对bean命名pointcut( ...

    Spring API

    前言 1. 简介 1.1. 概览 1.1.1. 使用场景 2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)...

    LINUX与UNIX_Shell编程指南(下)

    17.5.3 模式匹配 158 17.6 小结 159 第18章 控制流结构 160 18.1 退出状态 160 18.2 控制结构 160 18.2.1 流控制 161 18.2.2 循环 161 18.3 if then else语句 161 18.3.1 简单的if语句 162 18.3.2 变量值测试 162 ...

    21天学通Java-由浅入深

    图文并茂很适合初学着学习 下面是课程的目录: 第一篇 基础篇 第1章 Java简介(精彩视频:33分钟) 21 1.1 Java的平台简介 21 1.2 安装工具包 22 1.2.1 下载JDK 22 1.2.2 安装JDK 24 1.2.3 查看与设置环境变量 25 ...

    Spring.3.x企业应用开发实战(完整版).part2

    17.5.3 编写DAO测试基类 17.5.4 编写BoardDao测试用例 17.6 服务层开发 17.6.1 UserService的开发 17.6.2 ForumService的开发 17.6.3 服务类Bean的装配 17.7 对服务层进行测试 17.7.1 编写Service测试基类 17.7.2 ...

    Spring3.x企业应用开发实战(完整版) part1

    17.5.3 编写DAO测试基类 17.5.4 编写BoardDao测试用例 17.6 服务层开发 17.6.1 UserService的开发 17.6.2 ForumService的开发 17.6.3 服务类Bean的装配 17.7 对服务层进行测试 17.7.1 编写Service测试基类 17.7.2 ...

    UNIX 高级教程系统技术内幕

    目 录 第1 章 简介(1) 1.1 简介 1.1.1 简史 1.1.2 创始之初 1.1.3 繁衍 1.1.4 BSD 1.1.5 System V 1.1.6 商业化 1.1.7 Mach 1.1.8 标准 1.1.9 OSF 和UI 1.1.10 SVR4 及其之后 ...2.2 模式....16.6.1 buf ...

    过程控制工程完整课件2

    17.5.3 推断控制 17.5.4精馏塔的节能控制 17.5.5 精馏塔的最优控制 第18章 化学反应器的控制 18.1 概 述 18.1.1 化学反应的一些基本概念 18.1.2 化学反应器的类型 18.1.3 化学反应器的控制要求 ...

Global site tag (gtag.js) - Google Analytics