From C to oc-from process-oriented to object-oriented

Source: Internet
Author: User

1. in C's learning, we write a piece of code, usually taking into account the development process of the entire event and starting -- going through -- the result. In oC, such an idea needs to be transformed, people and things are involved in one thing, and they are responsible for what they do. For example, we play the Plants vs. botnets game:

C's way of thinking: botnets -- moving forward at a certain speed -- plants -- death

OC thinking: people and things involved in the entire event include:

Botnets: 1. botnets (such as roadblocks), botnets (such as roadblocks) 2. Walking, blood loss, and death

Plants: 1. plant types (such as the pea shooter) 2. Projection Shells

In this case, botnets and plants are called "Classes" (who or what). Taking botnets as an example, "names and items" are the attributes of botnets, "Walking, blood loss, and Death" are called botnets ). We can use these attributes and methods of botnets to create objects. For example, a barrier Botnet is an object created by the botnet class and countless objects can be created by the class.

Glossary: Super: parent class, self: object itself

2. Three Characteristics of OC: inheritance and encapsulation Polymorphism

-- Inheritance: In oC, if some attributes and methods of a Class B are the same as all attributes and methods of another class A, B can inherit, to write a piece of code for a, and B is fully owned.

Note: 1. inheritance is unidirectional. B inherits from a, and B has all attributes and methods of A. When B has additional attributes and methods, A does not. At this time, a is called a parent class, B is called a subclass.

2. Inheritance has the feature of first child and then parent. If the subclass overrides a method in the parent class, the system will execute the subclass method during execution.

3. nsobject is the parent class of all classes. Any class we create inherits this class, and its interior is the implementation method of all the system methods that can be called externally.

 

-- Encapsulation: implements access restrictions on attributes, methods, and other specific implementations. It only sets aside the interface for incoming data to make the written code more confidential, just like a bag, the specific implementation method is put in the bag, leaving only the opening of the pocket for external data input. At the same time, we can modify the Internal Attributes and methods as needed at any time, which also enhances the maintainability of the project.

 

-- Polymorphism: Polymorphism means that methods with the same method name can have different implementation functions. For example, in class A, there is a method named "havedinner ", the implementation is to output "I love to eat noodles". In Class B, there is also a method with the same method name. The implementation can be changed to the output "I love to eat hamburger ". A and B can be inherited, associated, or irrelevant. The system method can also be rewritten.

 

From C to oc-from process-oriented to object-oriented

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.