Abstract can be used to modify classes, methods, properties, indexers and time, and fields are not included here. Classes modified with abstract, this class can only be used as the base class of other classes and cannot be instantiated. Moreover, members modified by abstract must be implemented in all derived classes, and partial implementation is not allowed, otherwise the compile exception will be compiled. For example:
using System; namespace ConsoleApplication8 { class Program { static void Main(string[] args) { BClass b = new BClass(); b.m1(); } } abstract class AClass { public abstract void m1(); public abstract void m2(); } class BClass : AClass { public override void m1() { throw new NotImplementedException(); } //public override void m2() //{ // throw new NotImplementedException(); //} } }
Abstract classes have the following features:
Abstract classes have the following characteristics:
1. Abstract classes cannot be instantiated, but they can have instance constructors. Whether the class can be instantiated depends on whether it has instantiated permissions (the permissions for abstract classes are abstract, instantiation is prohibited).
Even if the constructor is not provided, the compiler will provide the default constructor;
2. Abstract classes can contain abstract methods and accessors;
3. Abstract classes cannot be modified by sealed, sealed means that they cannot be inherited;
4. All non-abstract classes inherited from abstract classes must implement all abstract members, including methods, properties, indexers, and events;
The abstract modification method has the following characteristics:
1. Abstract method is virtual method (implicit);
2. Abstract methods can only be declared in abstract classes;
3. Because abstract methods are just declarations and do not provide implementation, the method only ends with a semicolon, without a method body, that is, without curly braces;
public abstract void MyMethod();
4. The override modification override method provides implementation and can only be used as members of non-abstract classes;
5. Virtual or static modification cannot be used in the declaration of abstract methods. That is, it cannot be static, and because abstract is already virtual, there is no need to use virtual to emphasize it.
Although abstract properties are similar to abstract methods in behavior, they still have the following differences:
1. The abstract modifier cannot be applied on static properties;
2. Abstract attributes are overridden in non-abstract derived classes and use the override modifier;
Abstract classes and interfaces:
1. The abstract class must provide implementations of all interface members;
2. The abstract class inherits the interface can abstract the interface's member map bits.
like:
interface I { void M(); } abstract class C: I { public abstract void M(); } Abstract class instance: // abstract_keyword.cs // Abstract classusing System; abstract class BaseClass // Abstract class{ protected int _x = 100; //Abstract classes can define fields, but they cannot be abstract fields, and there is no such statement.protected int _y = 150; public BaseClass(int i) //A instance constructor can be defined, only for derived non-abstract class calls; constructors are explicitly provided here, and the compiler will no longer provide default constructors.{ fielda = i; } public BaseClass() { } private int fielda; public static int fieldsa = 0; public abstract void AbstractMethod(); // Abstract methodpublic abstract int X { get; } //Abstract propertiespublic abstract int Y { get; } public abstract string IdxString { get; set; } //Abstract propertiespublic abstract char this[int i] { get; } //Abstract indexer} class DerivedClass : BaseClass { private string idxstring; private int fieldb; //If there is no parameterless constructor defined in the base class, but there is a constructor with parameters,//Then the derived class must be constructor here that must call the parameter constructor of the base class, otherwise there will be an error in compilationpublic DerivedClass(int p) : base(p) // Here: base(p) can be omitted because the base class defines the default parameterless constructor{ fieldb = p; } public override string IdxString //Overwrite reproperties{ get { return idxstring; } set { idxstring = value; } } public override char this[int i] //Overwrite the indexer{ get { return IdxString[i]; } } public override void AbstractMethod() { _x++; _y++; } public override int X // Overwrite the rewrite attribute{ get { return _x + 10; } } public override int Y // Overwrite the rewrite attribute{ get { return _y + 10; } } static void Main() { DerivedClass o = new DerivedClass(1); (); ("x = {0}, y = {1}", , ); } }
The above is a detailed explanation of the usage of abstract in C# introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!