sometimes, a base class is not associated with a specific thing, but rather an abstract concept that provides a common interface for its derived classes. For this reason, the concept of abstract class is introduced in C # . Note:C + + programmers are most likely to make mistakes here. There is no direct declaration of an abstract class in C + +, and the class is an abstract class as long as the pure virtual function is defined in the class. The concept of pure virtual function is obscure, it is not easy to be accepted and mastered intuitively, so C # abandons the concept. Abstract classes Use the abstract modifier, which provides the following: an abstract class can only be used as a base class for other classes, it cannot be instantiated directly, and the new operator cannot be used for abstract classes . Abstract classes, if they contain abstract variables or values, are either null types or contain references to instances of non-abstract classes. abstract classes allow the inclusion of abstract members, although this is not required. Abstract classes cannot be sealed at the same time. if a non-abstract class derives from an abstract class, it must be overloaded to implement all inherited abstract members, see the following example:Abstruct class A{Public abstruct void F ();}Abstract class B:a{Public void G () {}}class C:b{Public override void F () { Specific implementation code for the //f}}Abstract classAprovides an abstract method of F. Class B inherits from the abstract class A and provides a method, G, because b does not contain an implementation of F , so b must also be an abstract class. Class C is inherited from class B , the abstract method F is overloaded in the class, and theconcrete implementation of f is provided, and Class C is allowed to be non- Abstract. Let's inherit the example of a car class. We understand the Vehicle class from the perspective of "transport" , which should express an abstract concept that we can define as abstract classes. This abstract class is inherited by car class car and truck class Truck as classes that can be instantiated. Program Listing 14-5: using System;abstract class Vehicle// Define car class {public int wheels;// Members: Number of wheels protected float weight;// Protection member: Weight Public Vehicle (int w,float g) {wheels=w;weight=g; }Public virtual void Speak () {Console.WriteLine ("The W Vehicle is speaking!"); }};Class car:vehicle// definition Sedan class {int passengers;// Private member: Number of passengers Public Car (int w,float g,int p): Base (w,g) {wheels=w;weight=g;passengers=p; }Public override void Speak () {Console.WriteLine ("The Car is speaking:di-di!"); }}Class truck:vehicle// definition Truck class {int passengers;// Private member: Number of passengers float load;// Private Member: Load weight Public Truck (int w,float g,int p,float L): Base (w,g) {wheels=w;weight=g;passengers=p;load=l; }Public override void Speak () {Console.WriteLine ("The Truck is speaking:ba-ba!"); }}
C # abstract class