1. What is abstract class
Abstract classes are virtual classes, cannot create objects, are modified with an abstract, override in subclasses with override
Abstract classes can hold abstract methods, properties, or non-abstract methods, attributes (this can be seen in the following code)
Non-abstract classes can only be stored in non-abstract methods (this can be seen in the code below the class)
If the subclass is also an abstract class, then all the abstract methods in the parent class, the attributes do not have to be fully implemented; If the subclass is not an abstract class, then all the abstract methods in the parent class must be fully implemented (if not implemented, the error will be observed)
2. Let's take a look at the implementation of the code to do something
//Define a car parent class firstAbstract classCar {Private string_name;//modified with private, can only be used in the parent class Public stringName//can be inherited from subclasses again using the { Get { return_name; } Set{_name=value; } } Public Abstract voidSay ();//abstract method, which must be overridden in a subclass Public voidStart ()//can be inherited from subclasses again using the{Console.WriteLine ("I'm a car, I'm starting now."); } }//define a Benz class that inherits car classBenz:car { Public Override voidSay ()//overriding in subclasses{Console.WriteLine ("I'm a Big Ben, my name is {0}", name); } }//define a BMW class, inherit car classBmw:car { Public Override voidSay ()//overriding in subclasses{Console.WriteLine ("I'm BMW, my name is {0}", name); } }
3. If you are using polymorphic
A variable that defines a parent class Car B = new Benz ();
b = new BMW ();
First loaded is the Mercedes-Benz class, later loaded is the BMW class, installed that class, showing the characteristics of that class, which is polymorphic
3.1 A parent class can be loaded with different subclasses to achieve different patterns
3.2 When a parent class is inherited by more than one class, the methods in the subclass are many, and the methods in the parent class cannot be determined, the abstract method is defined in the parent class and then implemented in the subclass, and can be used directly when using polymorphism.
An overview of the implementation of CSharp polymorphism
Implementation of CSharp polymorphism (abstract class)