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 ...
随机推荐
- 笔试算法题(06):最大连续子数组和 & 二叉树路径和值
出题:预先输入一个整型数组,数组中有正数也有负数:数组中连续一个或者多个整数组成一个子数组,每个子数组有一个和:求所有子数组中和的最大值,要求时间复杂度O(n): 分析: 时间复杂度为线性表明只允许一 ...
- python3.x Day6 多线程
线程???进程????区别???何时使用??? 进程:是程序以一个整体的形式暴露给操作系统管理,里边包含了对各种资源的调用,内存的使用,对各种资源的管理的集合,这就叫进程 线程:是操作系统最小的调度单 ...
- ajax中文乱码解决(java)
方法1: 页面端发出的数据做一次encodeURI,服务器端使用new String(old.getBytes("iso8859-1"), "utf-8") 方 ...
- 85-Momentum 动量指标.(2015.7.3)
Momentum 动量指标 动量数值就是当天价格同前几个时段的价格的比率 MOMENTUM = CLOSE(i)/CLOSE(i-N)*100 注解: CLOSE(i) - 当前柱形的收市价格: CL ...
- jquery 修改input输入框的 readOnly属性 && input输入框隐藏
html的代码 <div class="control-group"> <label class="control-label required&quo ...
- 前端开发:JavaScript---DOM & BOM
DOM:Document Object Model 文档对象类型 模态框案例 <!DOCTYPE html> <html lang="en"> <h ...
- P1160 队列安排 洛谷
https://www.luogu.org/problem/show?pid=1160 题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进 ...
- cogs——73. 找最佳通路
73. 找最佳通路 ★☆ 输入文件:city.in 输出文件:city.out 简单对比时间限制:1 s 内存限制:128 MB 问题描述有 n 个 城市,它们之间的交通情况已知.现在 ...
- IOS程序崩溃报告管理解决方案(Crashlytics 在2014-09-24)
预研Crashlytics 在2014-09-241:实现原理在原理上,Crashlytics通过以下2步完成崩溃日志的上传和分析:(1)提供应用SDK,你需要在应用启动时调用其SDK来设置你的应用 ...
- Maven项目中遇到的奇葩问题(续)
场景描写叙述 开发项目搞环境是一个很蛋疼的问题.总是会遇到各种奇葩的问题,上一篇文章http://blog.csdn.net/gao36951/article/details/50955526中遇到的 ...