The formation of Java Object-oriented Thinking

Source: Internet
Author: User
Tags export class

1. abstract process

 

These five basic features of Smalltalk demonstrate a pure Object-Oriented Programming Method:

 
1. Everything is an object. Think of an object as a fancy variable. It can store data. In addition, you can also ask it
Perform the operation on itself. Theoretically, you can extract any conceptual component (dog, building,
Service), which is represented as an object in the program.

 
2. A program is a collection of objects. They send messages to each other to call each other. To generate a request to an object,
You must send a message to this object. More specifically, you can think of a message as
Method.

3. Each object has a storage composed of other objects. You can create a package containing an existing object set
To create a new type of object. Therefore, you can build a complex system in a program and make it more complex.
The simplicity of the object is shielded.

4. Each object has its type ). According to the general statement, "Every object is of a class.
An instance, where "class" is the synonym of "type. The most important difference between each class and other
The feature of the class is "What message can you send to it ?"

 
5. All objects of a specific type can receive the same message (receive. This is a meaningful expression,
You will see it later. Because objects of the circle type are also of the shape type.
So a "Circular" object must be able to accept the message (accept) sent to the "geometric" object. This
This means that you can write code that interacts with ry and automatically process all things related to the ry.
This "substitutability" is one of the most powerful concepts in OOP.

 

2. Specifically, each object has a unique address in the memory.

 

3. Each object provides services

When you develop or understand a program design, one of the best ways is to think of the object as a "service provider"
(Service Provider )". Your program itself will provide services to users, and it will call services provided by other objects
To achieve this goal. Your goal is to create (or preferably find it in the existing code library) to provide the ideal
Service to solve the problem.

 

Seeing an object as a service provider has another additional benefit: it helps improve the object cohesion (cohesiveness ).
High cohesion is one of the basic quality requirements of software design: this means a software component (such as an object, although it also
It may be used to refer to all aspects of a method or an object library. "fit together" is good.

As a result, the problem arises. One problem we face when designing an object is to fill in too many functions in an object.

This requires us to use a good programming style and basic java knowledge.

 

1. Permission Control

 

Java uses three keywords to set the boundaries within the class: public, private, and protected. Their meaning and usage
Easy to understand. These "Access specifier" determine who can be defined immediately after the access specifier.
. "Public" indicates that the element following "is available to anyone. On the other hand," private "indicates that
Elements that are inaccessible to anyone other than the type creator and internal method of the type. Private is like you and the client
A brick wall between sequencers. If someone tries to access a private member, it will get an error message at the Compilation Time.
The protected keyword is equivalent to the private keyword. The difference is that the inheritance class can access protecte.
But cannot access private members. We will introduce inheritance later.
 
Java also has a default access permission. When you do not use any of the aforementioned access-specific words,
Will play a role. This type of permission is usually called "package access" because
Class can access other class members in the same package, but outside the package, these members are like private.

 

2. Reuse specific implementation

 

Once a class is developed and tested, it should (ideally) represent a useful code unit. Fact
It turns out that this reusability is not as easy as we want to produce a reusable object.
Design requires rich experience and keen insight. But once you have such a design, it will request
.Code reuse is one of the greatest advantages of object-oriented programming languages..

 

A simple way to reuse a class is to directly use an object of the class.In addition, you can also set an object of this class
In a new class. We call it "Creating a member object ". The new class can be of any number or type.
Other objects are composed of any methods that can implement the desired functions in the new class. Because you are using existing class Synthesis
So this concept is called composition. If a combination occurs dynamically, it is usually
Aggregation ). Combinations are often considered"Has-a" (owned)Relationship, as we often say
Engine.

 

BecauseInheritance is so important in Object-Oriented ProgrammingSo it is often highly emphasized, so new programmers will have
The impression is that inheritance should be used everywhere. This results in hard-to-use and overly complex design.. In fact
When creating a new category, you should first consider combination because it is simpler and more flexible.
. If you use this method, your design will
Become clearer. Once you have some experience, you can see through the occasions where inheritance must be used.

 

3. Inheritance: Reuse Interfaces

 

The concept of object itself is a very convenient tool, so that you can use the concept (concept)
Encapsulated together, so you can give an appropriate representation of the concept of the problem domain, instead of being subject to the need to use the underlying machine
Language. These concepts are represented by the keyword class, forming the basic unit in programming languages.
 
Unfortunately, it is still a lot of trouble to do this. After a class is created, even if another new class has a similar
Function, you still have to create a new class. If we can use an existing class as the basis, copy it, and then add
It is much better to add and modify this copy to create a new class.This effect can be achieved through inheritance., But there are also
When the source class (called base class), super class, or parent class) changes,
The modified "copy" (called the exported class), the inherited class (inherited class), or the sub-class (subclass, child
Class) will also reflect these changes.

 

When you inherit the existing type, a new type is created. This new type includes not only all members of the existing type (
Private member is hidden and inaccessible), and more importantly, it copies the interface of the base class. That's it.
Yes. All messages that can be sent to the base class object can also be sent to the export class. Because we can send messages
You can see the type of the class, so this means that the export class has the same type as the base class. In the previous example,
"A circle is also a geometric shape ". The Type equivalence generated by inheritance is an understanding
An important threshold for the connotation of object-oriented programming methods.

 

Although inheritance may sometimes mean adding new methods to the interface (especially the Java
), But not always.The second and other ways to make the differences between the export class and the base class are to change the existing base class.
Class. This is called overriding ).

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.