C# [method Modifiers] abstract virtual override new
abstract :表示方法是抽象方法,在子类中必须重写。抽象方法所在的类必须是抽象类,即用abstract modifiers;
virtual:表示此方法是virtual方法,除了在子类中可以重写外(在子类中也可直接使用),和普通方法完全一样;
override:表示重写父类的virtual方法;
new: 显式隐藏从基类继承的成员;
区别:
virtual:标记方法为虚方法
1.可在派生类中以override覆盖此方法
2.不覆盖也可由对象调用
3.无此标记的方法(也无其他标记),重写时需用new隐藏原方法
abstract 与virtual : 方法重写时都使用 override 关键字
Eg1:
public abstract class Book
{
public Book()
{
}
public abstract void getPrice(); //抽象方法,不含主体
public virtual void getName() //虚方法,可覆盖
{
Console.WriteLine("this is a test:virtual getName()");
}
public virtual void getContent() //虚方法,可覆盖
{
Console.WriteLine("this is a test:virtual getContent()");
}
public void getDate() //一般方法,若在派生类中重写,须使用new关键字
{
Console.WriteLine("this is a test: void getDate()");
}
} public class ChineseBook : Book
{
public override void getPrice() //实现抽象方法,必须实现
{
Console.WriteLine("this is a test:ChineseBook override abstract getPrice()");
}
public override void getName() //覆盖原方法,不是必须的
{
Console.WriteLine("this is a test:ChineseBook override virtual getName()");
}
public new void getDate() {
Console.WriteLine("this is a test:ChineseBook new getDate()");
} public void Run() {
getPrice();
getName();
getContent();
getDate();
} }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Output:
this is a test:ChineseBook override abstract getPrice()
this is a test:ChineseBook override virtual getName()
this is a test:virtual getContent()
this is a test:ChineseBook new getDate()
Eg2:
public abstract class FlowModel
{
public abstract void A(); public virtual void B(){
Console.WriteLine("Orginal B()");
} public virtual void C(){
Console.WriteLine("Orginal C()");
} public void D(){
Console.WriteLine("Orginal D()");
} } public class Flow:FlowModel
{
public override void A()
{
//执行步骤A
Console.WriteLine("Execute Step A ");
} public virtual void B()
{
//执行步骤B
Console.WriteLine("Execute Step B");
} public void C()
{
//执行步骤C
Console.WriteLine("Execute Step C");
} public new void D() {
Console.WriteLine("Execute Step D");
} public void Run()
{
A();
B(); //步骤B是扩展点 ,可以由子类决定具体执行什么
C();
D();
}
}
Output:
Execute Step A
Execute Step B
Execute Step C
Execute Step D
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
C# [method Modifiers] abstract virtual override new的更多相关文章
- knowing abstract,virtual,override,new
If a class has at least one member which modified by "abstract",this class is an abstract ...
- C# abstract virtual override new finally java final finalize
virtual:声明虚方法.可以被其派生类所重写的.重写方法需要使用override或者new关键字. override:覆盖原方法.可对重写virtual.override.abstract进行重写 ...
- abstract,virtual,override个人
1.abstract 可以修饰类和方法,修饰方法时只声明不实现: 2.继承实现abstract类必须通过override实现abstract声明的方法,而virtual方法可选择override(重写 ...
- abstract,virtual,override
1.abstract 可以修饰类和方法,修饰方法时只声明不实现: 2.继承实现abstract类必须通过override实现abstract声明的方法,而virtual方法可选择override(重写 ...
- Modifiers: virtual, override, new, abstract, sealed, internal
internal 声明类.类成员.接口或接口成员具有内部可见性. internal 修饰符使类.接口或成员仅在当前包中可见. 当前包之外的代码不能访问 internal 成员.只有在同一程序集的文件中 ...
- c#中virtual, abstract和override的区别和用法
virtual是把一个方法声明为虚方法,使派生类可重写此方法,一般建立的方法是不能够重写的,譬如类A中有个方法protected void method(){ 原代码....;}类B继承自类A,类B能 ...
- sealed、new、virtual、abstract与override 趣解
1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于 方法或属性时,sealed修饰符必须始终与override一起使用. ...
- sealed、new、virtual、abstract与override 总结
1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed修饰符必须始终与override一起使用. ...
- abstract、override、new、virtual、sealed使用和示例
abstract修饰类名为抽象类,修饰方法为抽象方法.如果一个类为抽象类,则这个类智能是其他某个类的基类.抽象方法在抽象类中没有函数体.抽象类中的抽象方法是没有方法体的,继承其的子类必须实现抽象类的抽 ...
随机推荐
- C# 连接Oracle,并调用存储过程(存在返回值),C# 调用sql存储过程
1.获取Oracle表格信息 public OracleHelpers(string ConnStr) { ConnectionString = ConnStr; conn = new OracleC ...
- 开源的 .Net Core MVC CMS 推荐
简介 ZKEACMS Core 是基于 ZKEACMS 的 Asp.Net Core 版本. 架设环境: .Net Core 跨平台 Microsoft Sql Serverl 2008 或以上 .N ...
- codeforces|CF1054D Changing Array
因为数据范围是2e5级别的,所以我们考虑用异或前缀和来处理区间的异或情况.(比如说a包括b,那么我们通过异或可以知道b对于a的补区间的信息) 之后因为对任意\(a_i\)进行取反操作,会改变它和它之后 ...
- log(m+n)找第k大
递归 int find_kth(vector<int>& nums1, int begin1, int size1, vector<int>& nums2, i ...
- “全栈2019”Java第七十五章:内部类持有外部类对象
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 本地Windows环境下安装MySql
Windows 上安装 MySQL Windows 上安装 MySQL 相对来说会较为简单,你需要在 MySQL 下载中下载 Windows 版本的 MySQL 安装包. Download Link: ...
- awk常用用法
一. 基本使用方法: awk '{pattern + action}' filenames #其中 pattern 表示 AWK 在数据中查找的内容,而 action 是在找到匹配内容时所执行的一系列 ...
- nginx高性能WEB服务器系列之一简介及安装
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- 数论 CF230B T-primes
CF230B T-primes 我们知道质数是只有两个不同的正数因数的正整数.相似的,我们把一个正整数 t 叫做 T质数,如果 t 恰好有三个不同的正整数因数. 你被给了一个含有 n 个正整数的数组. ...
- css 清楚浮动三种方法
我们可以看到这样一个布局: <style> .left{ width: 200px; height: 200px; background-color: #00ee00; float: le ...