Object-oriented features in C #

Source: Internet
Author: User

Object-oriented features in C #

C # adheres to the object-oriented features of c ++ and supports all the key concepts of object-oriented: encapsulation, inheritance, and polymorphism. Object-oriented has many features. 1. method uniqueness; 2. high continuity in the software life cycle; 3. object-Oriented Analysis (OOA), object-oriented design (OOD), and Object-Oriented Programming (OOP) are integrated into the corresponding stages of software survival. Next we will summarize the application of object-oriented in C # programming:

Abstract:

From the perspective of solving the problem, extract the main or common parts from a group of associated objects related to the problem, and merge them into a separate abstract class for implementation. The following is an example:

Namespace Abstraction

{

ClassProgram

{

StaticvoidMain(String[]Args)

{

Shapec = newCircle("Orange",3);

Console. WriteLine("The circle color is" + c. getColor()+ "The area is" + c. getArea()+ ".");

Shapes = newSquare("Black",5);

Console. WriteLine("The square Color is" + s. getColor()+ "The area is" + s. getArea()+ ".");

}

}

Publicpolicactclassshape

{

Protectedstringcolor;

PublicShape(Stringcolor)

{

This. color = color;

}

PublicstringgetColor()

{

Returncolor;

}

Publicpolicactdoublegetarea();

}

PublicclassCircle:Shape

{

Privatedoubleradius;

PublicCircle(Stringcolor,Doubleradius)

:Base(Color)

{

This. radius = radius;

}

PublicoverridedoublegetArea()

{

ReturnSystem. Math. PI * radius;

}

}

PublicclassSquare:Shape

{

Privatedoubleside;

PublicSquare(Stringcolor,Doubleside)

:Base(Color)

{

This. side = side;

}

PublicoverridedoublegetArea()

{

Returnside * side;

}

}

}


In this example, the first class is the Shape abstract class, which extracts common features from all shapes (circles and squares. The Shape class has an instance variable color, constructor, and access method getColor (), and abstract-modified virtual method getArea (), its Derived classes, Circle and Square, can both call these three methods and rewrite the virtual method getArea (). Therefore, the Shape class is abstracted. In reality, the circle and the square are similar. we abstract all their features to reduce code duplication, it also makes the code structure clearer.

Encapsulation:

Encapsulation is an information hiding technology. You can only view information on the object encapsulation interface, and the object is invisible to the user. The purpose of encapsulation is to separate the users and designers of objects. users do not have to know the specific process of behavior implementation. You only need to use the message provided by the designer to access the object. The basic unit of encapsulation is the object, which is more specific than encapsulating a class. Encapsulation itself is modular. The definition module and implementation module are separated to greatly improve Software maintainability and modification.

Encapsulation is defined:

1. with a clear boundary, the scope of the internal software of all objects is limited to this boundary; 2. interface to describe the interaction between the object and other objects. 3. protected internal implementation, which provides details about the functions provided by the software object, and implements fine-grained access to the class that defines this object.

Combined with the example of the image above, the attributes and methods of the color and area shared by the circle and square are encapsulated in the abstract class Shape, when using the function, you only need to see which function they are encapsulated in. When using the function, you can directly write the function name for calling. For example, the color attribute, use the getColor () function to obtain the color of the image without knowing how to obtain the color of the image. getcolor () is the boundary of the color object.

Inheritance:

Inheritance is a mechanism for automatically sharing methods and data in classes, subclasses, and objects. A class can have a parent class and a subclass to form a hierarchy. An important feature of this structure is inheritance: A class can inherit all non-private descriptions of its parent class, it also has transmission. A class directly inherits multiple classes, which are called multi-inheritance. in C #, interfaces are multi-inheritance, but multi-inheritance of classes is not supported; that is, a class can only have one parent class, which is called single inheritance. Similar: A class inherits from a parent class, but can be inherited by multiple classes, forming a tree structure; multiple inheritance forms a network structure inherited from each other.

In the above Code, in Circle: Shape, this indicates inheritance. circle is a subclass and shape is a parent class. You have your own constructor In the derived class circle. You can call the constructor in the shape of the parent class to set your own variables. ": base (color)" indicates that the constructor of the parent class is called with the color parameter.

Polymorphism:

That is, a name has multiple semantics. This is called polymorphism when the same message can be sent to an object of the parent class and its subclass. In C #, a multi-state reference indicates that instances of multiple classes can be referenced, which is related to both dynamic and static types. For example:

Namespace Polymorphism

{

ClassProgram

{

StaticvoidMain(String[]Args)

{

DrawingObject[]DObj = newDrawingObject[3];

DObj[0]= NewLine();

DObj[1]= NewCircle();

DObj[2]= NewDrawingObject();

Foreach(DrawingObjectdrawshapeindObj)

{

Drawshape. Draw();

}

}

}

PublicclassDrawingObject

{

PublicvirtualvoidDraw()

{

Console. WriteLine("I am just a painting method. ");

}

}

PublicclassLine:DrawingObject

{

Publicoverristmiddraw()

{

Console. WriteLine("I am a straight line. ");

}

}

PublicclassCircle:DrawingObject

{

Publicoverristmiddraw()

{

Console. WriteLine("I am a circle. ");

}

}


In this code example, there are encapsulation, inheritance, and Polymorphism: At runtime, each of them calls the Draw () method of each object, in this way, the Draw () method in the parent class DrawingObject is called, but the final result is different.

The programming language is also very practical. It is used to develop some application software and can also be used for windows desktop development. Of course, this is a very advanced application, which has been accumulated from the above two small examples, from easy to difficult to learn. They all have advantages and disadvantages, but they are reasonable. Therefore, C # is also very important.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.