In this article, we will discuss one of the hot topics in OOP: abstract classes. Abstract classes have the same concept in different programming languages, but C # is slightly different. In this article, we will use code to implement abstract classes and parse them one by one.
Abstract classes
650) This. width = 650; "width =" 403 "Height =" 232 "Title =" image "style =" border: 0px; padding-top: 0px; padding-Right: 0px; padding-left: 0px; margin-Right: auto; margin-left: auto; float: none; Background-image: none; "alt =" image "src =" http://image.mamicode.com/info/201505/20181024034909042105.png "border =" 0 "/>
In Microsoft's msdn, the abstract class is defined as follows:
Abstract keywords can be used to define an abstract class, requiring its subclass to implement functions and attributes of the abstract class. Abstract classes cannot be instantiated. Abstract classes provide unified definitions for directly sharing data and functions with different sub-classes. Abstract classes can also define abstract functions.
Abstract classes Practice
Add the console program in Visual Studio and name it"InheritanceAndPolymorphism
", AddClassa. CS,Add abstract classClassa.
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
using System;namespace InheritanceAndPolymorphism{ public abstract class ClassA { } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassA classA = new ClassA(); Console.ReadKey(); } }}
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation error:
Compile time error: cannot create an instance of the abstract class or interface 'heritanceandpolymorphism. classa'
Conclusion: an abstract class cannot be instantiated using the new keyword.
Abstract class non-Abstract Functions
Add code for some non-Abstract functions to the abstract class classa:
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
/// <summary> /// Abstract class ClassA /// </summary> public abstract class ClassA { public int a; public void XXX() { } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassA classA = new ClassA(); Console.ReadKey(); } }
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation, still reports an error. Abstract classes cannot be instantiated by The New Keyword regardless of whether they have abstract or non-Abstract Functions.
Abstract class as the base class
We use the abstract class as the base class and add classb-to inherit from classa.
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
/// <summary> /// Abstract class ClassA /// </summary> public abstract class ClassA { public int a; public void XXX() { } } /// <summary> /// Derived class. /// Class derived from abstract class ClassA /// </summary> public class ClassB:ClassA { } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB classB = new ClassB(); Console.ReadKey(); } }
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation Result: No error is reported.
Conclusion: A class can inherit fromAbstract class, which can be initialized by the new keyword.
Abstract class non-abstract function declaration
Declare the YYY function in classa-no function body.
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
/// <summary> /// Abstract class ClassA /// </summary> public abstract class ClassA { public int a; public void XXX() { } public void YYY(); } /// <summary> /// Derived class. /// Class derived from abstract class ClassA. /// </summary> public class ClassB:ClassA { } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB classB = new ClassB(); Console.ReadKey(); } }
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation, The result reports an error:
Compile time error: 'heritanceandpolymorphism. classa. YYY () 'must declare a body because it is not marked abstract, extern, or partial
The conclusion is that the function body needs to be added to YYY orAbstract modifier.
Abstract class abstract function declaration
Add the abstract modifier before the YYY of classa.
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
/// <summary> /// Abstract class ClassA /// </summary> public abstract class ClassA { public int a; public void XXX() { } abstract public void YYY(); } /// <summary> /// Derived class. /// Class derived from abstract class ClassA. /// </summary> public class ClassB:ClassA { } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB classB = new ClassB(); Console.ReadKey(); } }
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation result, error:
Compiler error: 'heritanceandpolymorphism. classb 'does not implement inherited abstract member 'heritanceandpolymorphism. classa. YYY ()'
Conclusion: InThe abstract class declaresAbstract function, but does not implement its content in its subclass classb; when the new keyword is used to initialize classb, an error is reported ---- The New Keyword cannot be used to initializeAbstract class.
Subclass inheritance implements Abstract Functions
Add the implementation of YYY to the subclass.
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
/// <summary> /// Abstract class ClassA /// </summary> public abstract class ClassA { public int a; public void XXX() { } abstract public void YYY(); } /// <summary> /// Derived class. /// Class derived from abstract class ClassA. /// </summary> public class ClassB:ClassA { public void YYY() { } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB classB = new ClassB(); Console.ReadKey(); } }
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation result, error:
Compile time error: 'heritanceandpolymorphism. classb 'does not implement inherited abstract member' inheritanceandpolymorphism. classa. YYY () 'compile Time Warning: 'heritanceandpolymorphism. classb. YYY () 'hides inherited member' inheritanceandpolymorphism. classa. YYY ()'.
Conclusion: To make the subclass inherit the YYY function of the base class, the override keyword must be used before the classb can be instantiated using the new keyword.
650) This. width = 650; "width =" 244 "Height =" 171 "Title =" image "style =" margin: 0px; Border: 0px; padding-top: 0px; padding-Right: 0px; padding-left: 0px; Background-image: none; "alt =" image "src =" http://image.mamicode.com/info/201505/20181024034909636855.png "border =" 0 "/>
Abstract Functions of Non-abstract classes
Let's look at the Code:
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
/// <summary> /// Abstract class ClassA /// </summary> public class ClassA { public int a; public void XXX() { } abstract public void YYY(); } /// <summary> /// Derived class. /// Class derived from abstract class ClassA. /// </summary> public class ClassB:ClassA { public override void YYY() { } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB classB = new ClassB(); Console.ReadKey(); } }
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation, The result reports an error:
Compiler error: 'heritanceandpolymorphism. classa. YYY () 'is abstract but it is contained in non-abstract class 'heritanceandpolymorphism. classa'
Result Analysis: StatementAbstract function, must declare the classabstract。
Abstract Functions cannot add static or virtual keywords at the same time.
Abstract basic functions
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
/// <summary> /// Abstract class ClassA /// </summary> public abstract class ClassA { public int a; public void XXX() { } abstract public void YYY(); } /// <summary> /// Derived class. /// Class derived from abstract class ClassA. /// </summary> public class ClassB:ClassA { public override void YYY() { base.YYY(); } } /// <summary> /// Program: used to execute the method. /// Contains Main method. /// </summary> public class Program { private static void Main(string[] args) { ClassB classB = new ClassB(); Console.ReadKey(); } }
650) This. width = 650; "alt =" Copy code "src ="/img/fz.gif "/>
Compilation, The result reports an error:
Compile time error: cannot call an abstract base Member: 'heritanceandpolymorphism. classa. YYY ()'
Result Analysis: The base class abstract function cannot be called in classb because it does not exist.
The last question is whether the keyword sealed can be added to the abstract class. The result is not acceptable.
Abstract classes cannot contain sealed or static class modifiers.
Conclusion
The following points are used to summarize the conclusions of this article.
New cannot be used for instantiation.Abstract abstract class
Abstract abstract classes can have subclasses. After their subclasses implement abstract methods, they can be instantiated by new objects.
If you declareAbstract function, you must declareAbstract class
When override abstracts the base class, the signature of the base class function cannot be modified.
Abstract function, cannot add static or virtual keywords at the same time
Abstract class cannot be declared as sealed or static class
Original article: diving in OOP (Day 4): Polymorphism and inheritance (all about abstract classes in C #)
This article from the "grape city control technical team blog" blog, please be sure to keep this source http://powertoolsteam.blog.51cto.com/2369428/1643783
In-depth introduction to OOP (4): Polymorphism and inheritance (abstract class)