Object-oriented has three characteristics of encapsulation, inheritance, polymorphism, object-oriented programming according to the characteristics of the real world to manage complex things, abstract them into objects, have their own state and behavior, through the response to the message to complete the task. This programming approach provides a very powerful variety, greatly increases the chances of reuse of code, increases the speed of program development, wraps the program code with independent special purpose, and modifies some program code without affecting other parts of the program.
1. Encapsulation
Each object contains all the information it needs to operate, encapsulation exposes only the external interface of the unit of code, and hides its implementation, as far as possible, without exposing the code to the outside. The use of encapsulation has many advantages, from the design point of view, the package can be shielded from some important information, such as the use of computers, as long as they know how to use the computer can be, do not know how these features are specifically implemented, from security considerations, encapsulation makes the code changes more secure and easy, Encapsulation clearly indicates which properties and methods are externally accessible, so that when the code of this class needs to be adjusted, as long as the public property is guaranteed to be the same, the parameters and return value types of the public method are unchanged, then the class can be modified without affecting the rest of the program; encapsulation also avoids the problem of naming conflicts, Encapsulation has an isolation effect, and different classes can have methods and properties of the same name, but they are not confusing or can reduce coupling.
2. Inheritance
Inheritance can use all the functionality of an existing class and extend these capabilities without rewriting the original class. A class that is produced by using inheritance is called a derived class or subclass, and the inherited class is called a base class or a superclass or a parent class. Inheritance represents a type derived from a base type that owns all member fields and functions of that base type, whose subclasses are extensions to the parent class, and interface inheritance is a signature that represents a type that inherits only functions, and does not inherit any implementation code. Inheritance divides the hierarchy of classes, it can be said that inheritance is the grouping of classes, the parent class represents the abstract class, more commonly used classes, and the subclass represents a more specific, more refined classes, inheritance is to implement code reuse, extension of the important means. The so-called abstract class refers to the specific matters, but only to express the whole rather than the specific concept of the class, for example, the shape contains squares, rectangles, circles, etc., when the shape is an abstract concept, equivalent to a parent class, and the square, rectangle, circle is the specific shape, equivalent to a subclass.
3. polymorphic
Polymorphism refers to the coexistence of different methods with the same name in the program, mainly through the subclasses of the parent class method to achieve polymorphism. In this way, non-homogeneous objects can accomplish certain functions with the same name, but the specific implementation method can be different. For example, shapes include squares, rectangles, circles, and so on, each of which has an area and perimeter, but different shapes have different ways of calculating area and perimeter.
Here's an example to illustrate encapsulation, inheritance, polymorphism:
The base class for this example is the shape mentioned above when the concept is described, the shape is the base class, and the base class is an abstract concept, not a concrete one, and therefore an abstract class that contains the property shape name, the method of outputting the perimeter and area of the shape, and an abstract method for calculating the perimeter and area of the shape:
/// <summary>///shape base class/// </summary> Public Abstract classshape{/// <summary> ///Shape Name/// </summary> Public stringShapename {Get;Private Set; } PublicShape (stringshapename) {Shapename=shapename; } /// <summary> ///Output Shape perimeter/// </summary> Public Virtual voidPrintperimeter (Doubleperimeter) {Console.WriteLine (Shapename+"Perimeter:"+perimeter); } /// <summary> ///Output Shape Area/// </summary> Public Virtual voidPrintArea (DoubleArea ) {Console.WriteLine (Shapename+"Area :"+Area ); } /// <summary> ///Calculate shape Perimeter/// </summary> /// <returns></returns> Public Abstract DoubleCalculateperimeter (); /// <summary> ///Calculate Shape Area/// </summary> /// <returns></returns> Public Abstract DoubleCalculateArea ();}View Code
Here's a look at the specific subclass, where the subclass is a circle, containing the attribute radius, calculating the perimeter, and the area:
/// <summary>///round/// </summary> Public classcircle:shape{/// <summary> ///radius of the circle/// </summary> Public DoubleRGet;Set; } PublicCircle ():Base("Circle") { This. R =0; } /// <summary> ///circumference of the circle/// </summary> /// <returns></returns> Public Override DoubleCalculateperimeter () {return 2* Math.PI *R; } /// <summary> ///area of the Circle/// </summary> /// <returns></returns> Public Override DoubleCalculateArea () {returnMath.PI * R *R; }}View Code
Take a look at the rectangle, which includes the height and width of the attribute, the circumference and area of the calculation:
Public classRectangle:shape {/// <summary> ///length of the rectangle/// </summary> Public DoubleWidth {Get;Set; } /// <summary> ///height of the rectangle/// </summary> Public DoubleHeight {Get;Set; } PublicRectangle ():Base("Rectangle") {Width=0; Height=0; } /// <summary> ///the circumference of a rectangle/// </summary> /// <returns></returns> Public Override DoubleCalculateperimeter () {return(Width + Height) *2; } /// <summary> ///the area of a rectangle/// </summary> /// <returns></returns> Public Override DoubleCalculateArea () {returnWidth *Height; } }View Code
Here's the code for the call:
Circle Circle =NewCircle (); circle. R= -; Square Square=NewSquare (); Edge=Ten; Rectangle Rectangle=NewRectangle (); Rectangle. Width= -; rectangle. Height= -; //assigning a class to a parent class can better manifest polymorphismIlist<shape> shapelist =NewList<shape>(); Shapelist.add (circle); Shapelist.add (square); Shapelist.add (rectangle) ;foreach(varShapeinchshapelist) {shape. Printperimeter (Shape. Calculateperimeter ()); Shape. PrintArea (Shape. CalculateArea ());}View Code
In this example, the method of outputting the perimeter and area of the shape does not work much, because the implementation of the method is relatively simple and can be useful if it is a complex method. For example, to implement drag-and-drop functionality, each shape can be dragged, and each shape of the method will be the same drag, but to achieve the drag function is not as simple as the output, when the subclass can inherit the parent class method, directly called.
C # Object-oriented three main features: encapsulation, inheritance, polymorphism