Object-oriented basis (II.)

Source: Internet
Author: User

In theory, object-oriented technology has four basic characteristics: encapsulation, abstraction, inheritance, polymorphism.

First, the package

  This is a hidden feature. You can use a formula to demonstrate the encapsulation characteristics of a class:

Encapsulated class = data + operations on this data (i.e. algorithm)

In layman's words, encapsulation is: wrap what the outside world does not need to know, only to show things to the outside world.

  In object-oriented theory, the concept of encapsulation has a broader meaning. Small to a simple data structure, large to a complete software subsystem, static as a software system to collect data information items, dynamic, such as a work process, can be encapsulated into a class.

The awareness of this package is the key to mastering object-oriented analysis and design techniques.

Second, abstract

Speaking of abstraction, we have to say that the foundation of modern science and technology-mathematics.

Mathematics is an abstract science, in the face of the complexity of the world, mathematics ignore the unique characteristics of various things, but only to extract their quantitative characteristics, and profoundly reveal the world of all things in the number of the common law, abstract is the essence of mathematics characteristics.

One of the branches of mathematics-discrete mathematics is one of the foundations of computer science, so computer science is inextricably linked to mathematics from the day of its birth, and abstract thinking is one of the main thought methods of computer science.

When designing a software system using an object-oriented approach, the first thing to do is to distinguish the types of things in the real world, to analyze what properties and functions they have, and then to abstract them into entity classes that make sense in the computing and virtual worlds, and when the program runs, the objects are created by the class. Use the mutual relationships between objects to simulate the interconnectedness of things in the real world.

Abstraction plays a key role in the transformation from the real world to the virtual world of computers.

Iii. Inheritance:

  inheritance is the most important feature of object-oriented programming. Any class can inherit from another class, which means that the class owns all the members of the class it inherits from. in object-oriented programming, a class that is inherited (also called a derivation) is called a parent class (also known as a base class). Note Objects in C # can only derive from one base class.

Buses, taxis, vans, etc. are all cars, but they are different cars, in addition to the characteristics of cars, they also have their own features , such as different operating methods, different uses and so on. At this point we can implement them as subcategories of cars that inherit all the state and behavior of the parent class (car) while increasing their state and behavior . Through the parent class and the subclass, we implemented the class hierarchy, starting with the most general class, and then gradually specialization, defining a series of subclasses. At the same time, the reuse of code through inheritance also enables the complexity of the program to grow linearly rather than exponentially.

When you inherit a base class, the accessibility of the member becomes an important issue. a derived class cannot access a private member of a base class, but can access its public members. However, both the derived class and the external code can access the public members. This means that only these two accessibility can be used to allow a member to be accessed by both the base class and the derived class, but also by external code. To solve this problem, C # provides a third accessibility:protected, only derived classes can access protected members .

In addition to the protection level of a member, we can define its inheritance behavior for members. A member of a base class can be virtual, that is, a member can be overridden by a class that inherits it. A derived class can provide additional execution code for a member. This execution code does not delete the original code, and it can still access the original code in the class, but the external code cannot access them. If no other execution is provided, the external code accesses the execution code of the members in the base class. A virtual member cannot be a private member.

A base class can also be defined as an abstract class. Abstract classes cannot be instantiated directly. To use an abstract class, you must inherit the class, and abstract classes can have abstract members that do not have code implementations in the base class, so the execution code must be provided in the derived class.

  The class can be sealed. Sealed classes cannot be used as base classes, so there are no derived classes.

In C #, all objects have a common base class object (mentioned in the previous article).

Iv. polymorphic

  

  

Polymorphism is another feature of object-oriented programming. In process-oriented program design, the main work is to write a process or function, these procedures and functions can not duplicate the same name. For example, in one application, you need to sort the numeric data, and you need to sort the character data, although the same sort method is used, but to define two different procedures (the names of the procedures are different).

In object-oriented programming, the "duplicate name" can be used to improve the abstraction and simplicity of the program. first of all, we understand the actual phenomenon, for example, "Start" is the operation of all means of transport, but different specific means of transport, its "start" operation of the specific implementation is different, such as the start of the car is "engine ignition-start engine", "start" the ship to "anchor", balloon airship "start "Is" inflatable--cast off ". If you do not allow these features to use the same name, you must define "car start", "Ship Start", "Balloon airship start" methods. In this way, users need to remember a lot of names when using, the advantages of inheritance is gone. In order to solve this problem, a polymorphic mechanism is introduced in object-oriented program design.

  polymorphism refers to the coexistence of different methods with the same name in a program. polymorphism is achieved mainly through subclasses overriding the parent class method. in this way, objects of a different kind can respond to a method of the same name to accomplish a particular function, but their specific implementation can vary. For example the same addition, adding two time together and adding two integers together must be completely different.

With method overrides, subclasses can re-implement some of the methods of the parent class so that they have their own characteristics. For example, in a car-class acceleration method, a subclass (such as a racing car) may have added some new parts to improve the acceleration performance, which can be overridden in a racing class to speed up the parent class. Overrides a method that hides the parent class so that the subclass has its own concrete implementation, further demonstrating the specificity of the subclass compared to the parent class.

  Polymorphism makes language flexible, abstract, and behavior-sharing advantages, which solves the problem of application function with the same name.

  Note: not only classes that share the same parent class can take advantage of polymorphism. As long as subclasses and grandchildren have an identical class in the inheritance hierarchy, they can take advantage of polymorphism in the same way.

Method overloading

  

Method overloading is another way to implement polymorphism. With method overloading, a class can have multiple methods with the same name, and the different number of arguments passed to them determines which method to use. For example, for a drawing class, it has a draw () method for drawing or outputting text, and we can pass it to a string, a rectangle, a circle, and even the initial position of the drawing, the color of the graphic, and so on. For each implementation, only a new draw () method can be implemented without a new name, which greatly simplifies the implementation and invocation of the method, and programmers and users do not need to remember many of the method names, just pass in the corresponding parameters.

Because a class can contain instructions on how operators operate, you can use an operator with an object instantiated from a class.

We write code for overloaded operators and use them as part of the class definition, which acts on this class. You can also overload operators to handle different classes in the same way, where one (or two) class definition contains code that achieves this purpose.

Note: You can only overload an existing C # operator in this way, and you cannot create a new operator.

V. Summary

Object-oriented four characteristics are not independent of each other, "abstract" and "encapsulation" more reflected as a way of thinking, mainly embodied in the object-oriented system of the overall analysis and design, "inheritance" and "polymorphism" is more applied to the specific subsystem and software module design and coding process, and "inheritance" is the "polymorphic" Foundation.

Object-oriented basis (II.)

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.