Implementation of abstract classes and abstract methods
An abstract class is a special kind of basic class that is not associated with a specific thing. The definition of an abstract class uses the keyword abstract.
In the hierarchy of classes, there is no such thing as "graphics", so it is possible to define "graph" as abstract class, derive "Circle" and "quadrilateral" as some ordinary class which can be instantiated concretely, it should be noted that abstract class cannot be instantiated, he can only be used as the base class of other classes. Position the shape class as an abstract class code as follows:
Public Absract class shape
{
.....
}
Abstract methods can also be defined in abstract classes using the keyword Absract, which requires all derived non-abstract classes to overload the implementation of the abstract method, because the abstract class itself is an abstraction, and some methods do not need to be implemented, but are left behind to let the derived class overload implementation. The Getarea method in the shape class has no specific meaning in itself, and only the derived class Cricle and reatangular can calculate the specific area.
The abstract method is:
Public absract double Getarea ();
The derived class overload is implemented as:
public override double Getarea ();
{
......
}
Below we use specific engineering case to explain:---------------------------------------->
First we add a class Shape class to the project file-------Shape.cs
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceApplication277 {8 //defining a base class shape9 Public Abstract classShapeTen { One protected stringColor; A PublicShape () {;}//constructor Function - PublicShape (stringColor) { This. Color =Color;} - Public stringGetColor () {returnColor;} the Public Abstract DoubleGetarea ();//Abstract class - } - //defining the Cirle class, deriving from the shape class - Public classCircle:shape + { - Private DoubleRadius; + PublicCircle (stringColor,DoubleRadius) A { at This. Color =Color; - This. Radius =Radius; - } - Public Override DoubleGetarea () - { - returnSystem.Math.PI * Radius *Radius; in } - to } + //derived class rectangular, deriving from the shape class - Public classRetangular:shape the { * protected DoubleLength, Width; $ PublicRetangular (stringColor,DoubleLength,DoubleWidth)Panax Notoginseng { - This. Color =Color; the This. Length =Length; + This. Width =Width; A } the Public Override DoubleGetarea () + { - return(length*Width); $ } $ - Public DoublePerimeteris () - { the return(2* (Length *Width)); - Wuyi } Wu } - //derived class square, derived from the Retangular class About Public classSquare:retangular $ { - PublicSquare (stringColor,DoubleSide):Base(Color,side,side) {;} - - A } $}
Here we use the abstract shape, which is the abstraction class
Then we set up in the main program
-----------------------Program.cs------------------------------------
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceApplication277 {8 class Program9 {Ten Static voidMain (string[] args) One { ACircle Cir =NewCircle ("Orange",3.0); -Console.WriteLine ("Circle Area Is{1}", Cir.getcolor (), Cir.getarea ()); -Retangular Rect =NewRetangular ("Red",13.0,2.0); theConsole.WriteLine ("retangular Color is {0},rectangualr. {1},rectangualr Perimeter is {2}", - Rect.getcolor (), Rect.getarea (), Rect.perimeteris ()); -Square Squ =NewSquare ("Qreen",5.0); -Console.WriteLine ("Square Color is {0},square. 1},square Perimeter is {2}", Squ.getcolor (), Squ.getarea (), Squ.perimeteris ()); + } - } +}
The results are shown below:
Implementation of C # abstract classes and abstract methods