virtual (C# Reference)
https://msdn.microsoft.com/en-us/library/9fkccyh4.aspx
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.
For example, this method can be overridden by any class that inherits it:
public virtual double Area()
{
return x * y;
}
The implementation of a virtual member can be changed by an overriding member in a derived class.
For more information about how to use thevirtual keyword,
see Versioning with the Override and New Keywords (C# Programming Guide)
and Knowing When to Use Override and New Keywords (C# Programming Guide).
Remarks
When a virtual method is invoked, the run-time type of the object is checked for an overriding member.
The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
By default, methods are non-virtual. You cannot override a non-virtual method.
You cannot use the virtual modifier with the static, abstract, private, or override modifiers.
The following example shows a virtual property:
public class MyBaseClass
{
/// <summary>
/// virtual auto-implemented property.
/// Overrides can only provide specialized behavior if they implement get and set accessors.
/// </summary>
public virtual string Name { get; set; } private int number;
/// <summary>
/// ordinary virtual property with backing field
/// </summary>
public virtual int Number
{
get { return number; }
set { number = value; }
}
} public class MyDerivedClass : MyBaseClass
{
private string name;
/// <summary>
/// Override auto-implemented property with ordinary property to provide specialized accessor behavior.
/// </summary>
public override string Name
{
get
{
return name;
}
set
{
name = value.Equals(string.Empty) == false ? value : "Unknown";
}
}
}
Virtual properties behave like abstract methods, except for the differences in declaration and invocation调用 syntax.
It is an error to use the virtual modifier on a static property.
A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
Example
In this example, the Shape class contains the two coordinates x, y, and the Area() virtual method.
Different shape classes such as Circle, Cylinder, and Sphere inherit the Shape class, and the surface area is calculated for each figure.
Each derived class has it own override implementation ofArea().
Notice that the inherited classes Circle, Sphere, and Cylinder all use constructors that initialize the base class, as shown in the following declaration.
public Cylinder(double r, double h): base(r, h) {}
The following program calculates and displays the appropriate area for each figure by invoking the appropriate implementation of the Area()method, according to the object that is associated with the method.
class TestClass
{
public class Shape
{
public const double PI = Math.PI;
protected double x, y;
public Shape()
{
}
public Shape(double x, double y)
{
this.x = x;
this.y = y;
} public virtual double Area()
{
return x * y;
}
} /// <summary>
/// 圆
/// </summary>
public class Circle : Shape
{
public Circle(double r) : base(r, )
{
} public override double Area()
{
return PI * x * x;
}
} /// <summary>
/// 球体
/// </summary>
class Sphere : Shape
{
public Sphere(double r) : base(r, )
{
} public override double Area()
{
return * PI * x * x;
}
} /// <summary>
/// 圆柱体
/// </summary>
class Cylinder : Shape
{
public Cylinder(double r, double h) : base(r, h)
{
} public override double Area()
{
return * PI * x * x + * PI * x * y;
}
} static void Method()
{
double r = 3.0, h = 5.0;
Shape c = new Circle(r);
Shape s = new Sphere(r);
Shape l = new Cylinder(r, h);
// Display results:
Console.WriteLine("Area of Circle = {0:F2}", c.Area());
Console.WriteLine("Area of Sphere = {0:F2}", s.Area());
Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());
}
}
virtual (C# Reference)的更多相关文章
- abstract (C# Reference)
https://msdn.microsoft.com/en-us/library/sf985hc5.aspx The abstract modifier indicates that the thin ...
- DIY FSK RFID Reader
This page describes the construction of an RFID reader using only an Arduino (Nano 3.0 was tested, b ...
- RFID 读写器 Reader Writer Cloner
RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...
- Translation Lookaside Buffer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In principle, then, e ...
- RFID Reader 线路图收集
This 125 kHz RFID reader http://www.serasidis.gr/circuits/RFID_reader/125kHz_RFID_reader.htm http:// ...
- C++ 虚函数机制学习
致谢 本文是基于对<Inside the c++ object model>的阅读和gdb的使用而完成的.在此感谢Lippman对cfront中对象模型的解析,这些解析帮助读者拨开迷雾.此 ...
- (4)top详解 (每周一个linux命令系列)
(4)top详解 (每周一个linux命令系列) linux命令 top详解 引言:今天的命令是用来看cpu信息的top top 我们先看man top top - display Linux pro ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
/********************************************************************************* * Caused by: java ...
随机推荐
- 动态规划----最长公共子序列(C++实现)
最长公共子序列 题目描述:给定两个字符串s1 s2 … sn和t1 t2 … tm .求出这两个字符串的最长公共子序列的长度.字符串s1 s2 … sn的子序列指可以表示为 … { i1 < i ...
- NOIp模拟赛 西行妖下
题目描述: 给出一棵n个节点的树,每个点初始m值为1. 你有三种操作: 1.Add l r k ,将l到r路径上所有点m值加k. 2.Multi l r k ,将l到r路径上所有点m值乘k. 3.Qu ...
- Python 反射-isinstance-issubclass-__str__-__del__
用到的 isinstance(对象,类) ------------------- 判断一个对象是否是一个类的实例 issubclass(子类,父类) ---------------- 判断一个 ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- safepoint与UseCountedLoopSafepoints
safepoint: JIT编码时,会在代码中所有方法的返回之前,以及所有非counted loop的循环(无界循环)回跳之前放置一个safepoint(counted loop则没有放置safepo ...
- Github ==〉本地(克隆)
[情景] 新员工入职后,一般会将项目下载到本地. [下载(克隆)] 命令 git clone url地址 示例
- 关于Django中,实现序列化的几种不同方法
前言 关于序列化操作,就是将一个可迭代的数据结构,通过便利的方式进行我们所需要的操作. 今天历来归纳一下,Django中的几种不同得分方法,已经Django-restframework提供的方法 创建 ...
- python接口测试之Http请求(三)
python的强大之处在于提供了很多的标准库,这些标准库可以直接调用,本节部分,重点学习和总结在 接口测试中Python的Http请求的库的学习. 首先来看httplib,官方的解释为:本模块定义了类 ...
- 《ajax学习》之ajax+JavaScript事件验证用户名是否可注册
当用户注册时,服务器数据库需要对用户输入的用户信息(以用户名为例子)进行验证,在不刷新页面的情况下又需要页面和服务器进行数据请求,最好的方法是用ajax异步请求. 一.实现思路: 1.用户输入信息 2 ...
- acm 一年总结
首先是大一的一段简短历史,和其他人不太一样,刚上大一的我等于是刚刚接触电脑,开始下载程序啦,安装系统了,电脑出个小问题啦自己都不会解决,然后大一还开了一门叫做c语言的课程,顿时傻逼了,当时也不用功,大 ...