abstract (C# Reference)
https://msdn.microsoft.com/en-us/library/sf985hc5.aspx
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation.
The abstract modifier can be used with classes, methods, properties, indexers, and events.
Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.
Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
In this example, the class Square must provide an implementation of Area because it derives from ShapesClass:
abstract class ShapesClass { abstract public int Area(); } class Square : ShapesClass { int side = 0; public Square(int n) { side = n; } // Area method is required to avoid // a compile-time error. public override int Area() { return side * side; } static void Main() { Square sq = new Square(12); Console.WriteLine("Area of the square = {0}", sq.Area()); } } // Output: Area of the square = 144
Abstract classes have the following features:
-
An abstract class cannot be instantiated.
-
An abstract class may contain abstract methods and accessors.
-
It is not possible to modify an abstract class with the sealed (C# Reference) modifier because the two modifers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
-
A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.
Abstract methods have the following features:
-
An abstract method is implicitly隐式地 a virtual method.
-
Abstract method declarations are only permitted被允许 in abstract classes.
-
Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon分号 and there are no curly braces ({ }) following the signature. For example:
public abstract void MyMethod();
-
The implementation is provided by an overriding method override (C# Reference), which is a member of a non-abstract class.
-
It is an error to use the static or virtual modifiers in an abstract method declaration.
Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
-
It is an error to use the abstract modifier on a static property.
-
An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
For more information about abstract classes, see Abstract and Sealed Classes and Class Members (C# Programming Guide).
An abstract class must provide implementation for all interface members.
An abstract class that implements an interface might map the interface methods onto abstract methods. For example:
interface I { void M(); } abstract class C : I { public abstract void M(); }
Example
In this example, the class DerivedClass is derived from an abstract class BaseClass.
The abstract class contains an abstract method,AbstractMethod, and two abstract properties, X and Y.
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); } } // Output: x = 111, y = 161
In the preceding example, if you attempt to instantiate the abstract class by using a statement like this:
BaseClass bc = new BaseClass(); // Error
you will get an error saying that the compiler cannot create an instance of the abstract class 'BaseClass'.
C# Language Specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
See Also
C# Reference
C# Programming Guide
Modifiers (C# Reference)
virtual (C# Reference)
override (C# Reference)
C# Keywords
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了