惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - mjgforever

sp_executesql和临时表的用法 51 条 ActionScript 3.0 和 Flex 优化技术与实践 open source Flex project list Open Source Replication and Synchronization Tools Written in Java 用SyncML进行异构数据库复制 WF学习系列之六:工作流和宿主程序通讯异常引发的思考 - mjgforever - 博客园 WF学习系列之五:状态机工作流学习 WF学习系列之四:顺序工作流控制台应用程序模板介绍 WF学习系列之三:RuleSet知识点概述 WF学习系列之二:开发工作流知识点概述 WF学习系列之一:WF基本知识点概述 ArcIMS 安装指南 Google推出Protocol Buffers:争夺网络时代数据格式 Protocol Buffers 简介 Protocol Buffers:Google 的数据交换格式 - mjgforever 微软vs.VMware:虚拟化中的真实战争 - mjgforever 文本文件与二进制文件 字符编码简介:ASCII,Unicode,UTF-8,GB2312 Adobe Flex的十大误解
abstract, virtual, override, new 关键字的用法
mjgforever · 2008-11-28 · via 博客园 - mjgforever

简单一点来说,相见msdn的介绍。

virtual:包含函数体,继承类无需重写,当然也可以重写;
abstract:不包含函数体,继承类必须重写;
new:一般方法,继承类想覆盖之,采用new关键字进行;

using System;
using System.Collections.Generic;
using System.Text;

namespace Keywords
{
    
public abstract class ShapeClass
    
{
        
public string GetShapeName()
        
{
            
return "shape class";
        }


        
public abstract int Area();

        
public virtual int Volume()
        
{
            
return 11;
        }


    }


    
public class Square : ShapeClass
    
{
        
// 采用new关键字重写基类一般方法
        public new string GetShapeName()
        
{
            
return "square";
        }


        
// 必须重写
        public override int Area()
        
{
            
return 22;
        }


        
// 可以重写,也可以不重写
        public override int Volume()
        
{
            
//return base.Volume();
            return 33;
        }

    }


    
public class Program
    
{
        
public static void Main(string[] args)
        
{
            Square square 
= new Square();
            Console.WriteLine(square.GetShapeName());
            Console.WriteLine(square.Area());
            Console.WriteLine(square.Volume());

            Console.ReadLine();
        }

    }

}

附录:msdn的介绍

abstract 修饰符可以和类、方法、属性、索引器及事件一起使用。在类声明中使用 abstract 修饰符以指示某个类只能是其他类的基类。标记为抽象或包含在抽象类中的成员必须通过从抽象类派生的类来实现。

在此例中,类 Square 必须提供 Area 的实现,因为它派生自 ShapesClass

        abstract class ShapesClass
            {
            abstract public int Area();
            }
            class Square : ShapesClass
            {
            int x, y;
            // Not providing an Area method results
            // in a compile-time error.
            public override int Area()
            {
            return x * y;
            }
            }
  复制代码

有关抽象类的更多信息,请参见抽象类、密封类及类成员(C# 编程指南)

备注

示例

在本例中,DerivedClass 类是从抽象类 BaseClass 派生的。抽象类包含一个抽象方法 AbstractMethod 和两个抽象属性 XY

// abstract_keyword.cs
            // Abstract Classes
            using System;
            abstract class BaseClass   // Abstract class
            {
            protected int _x = 100;
            protected int _y = 150;
            public abstract void AbstractMethod();   // Abstract method
            public abstract int X    { get; }
            public abstract int Y    { get; }
            }
            class DerivedClass : BaseClass
            {
            public override void AbstractMethod()
            {
            _x++;
            _y++;
            }
            public override int X   // overriding property
            {
            get
            {
            return _x + 10;
            }
            }
            public override int Y   // overriding property
            {
            get
            {
            return _y + 10;
            }
            }
            static void Main()
            {
            DerivedClass o = new DerivedClass();
            o.AbstractMethod();
            Console.WriteLine("x = {0}, y = {1}", o.X, o.Y);
            }
            }
  复制代码

输出

x = 111, y = 161
 

注释

在上面的示例中,如果试图通过使用下面的语句将抽象类实例化:

BaseClass bc = new BaseClass();   // Error
  复制代码

将出现错误,指出编译器无法创建抽象类“BaseClass”的实例。

virtual 关键字用于修饰方法、属性、索引器或事件声明,并且允许在派生类中重写这些对象。例如,此方法可被任何继承它的类重写。

public virtual double Area()
            {
            return x * y;
            }
  复制代码

虚拟成员的实现可由派生类中的重写成员更改。有关使用 virtual 关键字的更多信息,请参见使用 Override 和 New 关键字控制版本(C# 编程指南)了解何时使用 Override 和 New 关键字(C# 编程指南)

备注

示例

在该示例中,Dimensions 类包含 xy 两个坐标和 Area() 虚方法。不同的形状类,如 CircleCylinderSphere 继承 Dimensions 类,并为每个图形计算表面积。每个派生类都有各自的 Area() 重写实现。根据与此方法关联的对象,通过调用正确的 Area() 实现,该程序为每个图形计算并显示正确的面积。

在前面的示例中,注意继承的类 CircleSphereCylinder 都使用了初始化基类的构造函数,例如:

public Cylinder(double r, double h): base(r, h) {}
  复制代码

这类似于 C++ 的初始化列表。

// cs_virtual_keyword.cs
            using System;
            class TestClass
            {
            public class Dimensions
            {
            public const double PI = Math.PI;
            protected double x, y;
            public Dimensions()
            {
            }
            public Dimensions(double x, double y)
            {
            this.x = x;
            this.y = y;
            }
            public virtual double Area()
            {
            return x * y;
            }
            }
            public class Circle : Dimensions
            {
            public Circle(double r) : base(r, 0)
            {
            }
            public override double Area()
            {
            return PI * x * x;
            }
            }
            class Sphere : Dimensions
            {
            public Sphere(double r) : base(r, 0)
            {
            }
            public override double Area()
            {
            return 4 * PI * x * x;
            }
            }
            class Cylinder : Dimensions
            {
            public Cylinder(double r, double h) : base(r, h)
            {
            }
            public override double Area()
            {
            return 2 * PI * x * x + 2 * PI * x * y;
            }
            }
            static void Main()
            {
            double r = 3.0, h = 5.0;
            Dimensions c = new Circle(r);
            Dimensions s = new Sphere(r);
            Dimensions 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());
            }
            }
  复制代码

输出

Area of Circle   = 28.27
            Area of Sphere   = 113.10
            Area of Cylinder = 150.80