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

推荐订阅源

L
LINUX DO - 最新话题
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
宝玉的分享
宝玉的分享
Cisco Talos Blog
Cisco Talos Blog
Help Net Security
Help Net Security
月光博客
月光博客
V
V2EX
量子位
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
P
Privacy International News Feed
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
A
Arctic Wolf
S
Schneier on Security
H
Hacker News: Front Page
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cisco Blogs
G
GRAHAM CLULEY
The Cloudflare Blog
博客园 - Franky
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
H
Heimdal Security Blog
The GitHub Blog
The GitHub Blog
C
Check Point Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Blog of Author Tim Ferriss
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
T
Tenable Blog
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
T
Troy Hunt's Blog
B
Blog
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC

博客园 - 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 的数据交换格式 微软vs.VMware:虚拟化中的真实战争 文本文件与二进制文件 字符编码简介: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);
            }
            }

输出

注释

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

  复制代码
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