Several important object-oriented concepts

Source: Internet
Author: User
◆ Main features of OOP:
Abstract)
Encapsulation)
Inheritance)
Polymorphism)
Association)
Aggregation)
Composition)
Cohesion & Coupling)

Single type abstract encapsulation Encapsulation
Hierarchy inherits the inheritance Polymorphism
Association between types Association aggregation Composition
Programming principles: cohesion and cohesion & Coupling

◆ Abstract: process of extracting things in common and describing them in a unified manner. There are different levels of abstraction.

In different requirements environments, what we abstract into a class and what we regard as an instance or object is the most critical step in the beginning of object-oriented design. excessive abstraction may leadProgramThe design hierarchy is aggravated, but if the abstraction is not enough in extreme cases, it may degrade to the scenario where a program for Process Design corresponds to a problem.

Ignore the details of an object or entity and focus only on the process simplification functions and formats of its essential features to help users interact with objects.

◆ Encapsulation)
Hide data and implement
Provides public methods for users to call
Two views of an object
-- External view: what the object can do
-- Internal view: How objects work
-- TV: adjust the volume

◆ Inheritance)
Define a New Type through an existing type.
There is usually a relationship like "is a" or "kind of" between two types.
Can be implemented through inheritanceCodeIn addition, inheritance is also the basis of polymorphism.
For example, Apple "is a" fruit.

◆ Polymorphism (polymorphism)
A name in multiple forms.
Method overloading in a class is a kind of polymorphism.
Inheritance-based polymorphism.
When calling a method, select a different processing method based on the given object.
For example, football -- play (): complete with a script.
Basketball -- play (): complete by hand.
Give a specific football or basketball. The user automatically knows who to use to execute play ().

◆ Association)
A reference method for interaction between objects.
When an object uses the services or operations of another object by referencing another object, the two objects are associated.
For example, if a person uses a computer, there is an association between the person and the computer.
Person
{
Computer. playgame ();
}

◆ Aggregation)
An association. An object is an integral part of another object.
Is a strongly correlated relationship.
There is a "has a" relationship between two objects. An object exists as an attribute of another object. When an external object is produced, the client can specify the internal object associated with it.
For example, an automobile and a tire are an integral part of an automobile. They can be assembled and used after production respectively, but the automobile can be replaced with a new tire and can be unloaded for use by other cars.

Strong Association-through Constructor
Class person
{
Public Person ()
{
Create compter;
}
}

◆ Composition)
When an object contains another object, the external object is responsible for managing the lifecycle of the internal object.
The most intense Association.
The creation of internal objects is controlled by external objects.
When an external object does not exist, the internal object cannot exist, such as the TV and CRT.

◆ Composition combination
Engine car.
The vehicle life cycle determines the engine life cycle.
Garbage collection.

◆ Cohesion)
Measure the ability of a class to complete a job independently.
Cleaners (with vacuum cleaners, no, Broom ).
Temporary (none ).
 
◆ Coupling)
Measure the complexity of dependencies within or between systems.
Design Principle: increase cohesion and reduce coupling.
Interface: decoupling.

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.