Java object-oriented into learning!

Source: Internet
Author: User
Tags export class

I. Abstract process

Smalltalk's five basic features, which represent a purely object-oriented programming approach:


1. All things are objects. Consider an object as a fancy variable that can store data, but you can also ask for it
Perform an action on itself. Theoretically, you can extract any conceptualization of the problem that you want to solve (dogs, buildings,
Service, etc.), and represent it as an object in your program.


2. Programs are collections of objects that call each other by sending messages to each other. To create a request for an object,
You must send a message to the object. More specifically, you can think of the message as a party to a particular object
The call request of the method.


3. Each object has a storage composed of other objects. You can create a package that contains a collection of existing objects by creating a
method to create a new type of object. As a result, you can build complex systems in your programs and
The simplicity of the over object is masked.


4. Each object has its type. According to general parlance, "Each object is a class
An instance (Instance) ", where" class "is synonymous with" type ". The most important difference in each class differs from the other
The attribute of the class is "what message can you send to it?" ”


5. All objects of a particular type can receive (receive) the same message. This is a meaningful statement,
You'll see later. Because an object of type Circle (circle) is also a geometry (shape) type
Object, so a "circle" object must be able to accept messages sent to the "Geometry" object. This
means that you can write code that interacts with the geometry and automatically handles all things related to the geometric nature.
This "alternative (substitutability)" is one of the most powerful concepts in OOP.

Second, specifically, every object in memory has a unique address

Three, each object to provide services


When you are developing or understanding a program, one of the best ways is to think of the object as a "service provider
(Service Provider) ". Your program itself will provide services to the user, and it will provide services by invoking other objects
To achieve this goal. Your goal is to create (or preferably look for in an existing code base) to provide the ideal
A collection of objects that serve to resolve the problem.

Seeing an object as a service provider also has an additional benefit: it helps to improve the cohesion of the object (cohesiveness).
High cohesion is one of the basic quality requirements of software design: This means that a software artifact (such as an object, though it
Can be used to refer to various aspects of a method or an object library "combination" (fit together) is very good.


As a result, one of the problems in designing objects is to put too much functionality in one object.

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

1. Permission control

Java uses three keywords to set boundaries within the class: public, private, protected. Their meaning and use
Very easy to understand. These "access specifier" determine what can be defined immediately after the
Use. Public means that immediately after the element is available to anyone, on the other hand, the private keyword represents
An element that is not accessible to anyone other than the type creator and the internal method of that type. Private is like you and the client
A brick wall between the sequencer, and if someone tries to access the private member, it will get an error message at compile time.
The protected keyword is equivalent to the private function, except that the inheriting class (inheriting class) can access the PROTECTE
Members, but cannot access private members. Inheritance (inheritance) will be introduced later.

Java also has a default access right, and when you do not use any of the previously mentioned access to the specified word,
will play a role. This permission is often referred to as "package access" because under this permission
Classes can access members of other classes in the same package, but outside of the package, these members are like private.

2. Concrete implementation of multiplexing

Once a class is developed and tested, it should (ideally) represent a useful unit of code. Fact
proves that this reusability (reusability) does not easily reach the level we want, producing a reusable object
Design requires a wealth of experience and keen insight. But once you have such a design, it will ask for a duplicate
Use. code reuse is one of the greatest advantages offered by object-oriented programming languages .

The simple way to reuse a class is to use an object of that class directly , and you can also put an object of that class
In a new class. We call it "create a member object". The new class can be made up of any number, any type
Other objects are made up of any way you can implement the functionality you want in a new class. Because you're using an existing class composition.
The class, so this concept is called a combination (composition), if the modular dynamic occurs, then it is usually
is an aggregation (aggregation). The combination is often seen as a "has-a" relationship, as we often say, "a car
Engine. "

Since inheritance is so important in object-oriented programming , it is often highly emphasized, so novice programmers will have
The impression: Inheritance should be used everywhere. This can lead to difficult-to-use and overly complex designs . In fact, in establishing
New class, you should consider the combination first, because it is simpler and more flexible
. If you use this approach, your design will
Become clearer. Once you have some experience, you will be able to see through the situations where inheritance must be used.

3. Inheritance: Multiplexing interface

The idea of an object is, in itself, a very handy tool that allows you to concept data and functionality through concepts
Packaged together, so you can give an appropriate representation of the concept of the problem domain without being subject to the need to use the underlying machine
Language. These concepts are represented by the keyword class, which forms the basic unit in the programming language.

Unfortunately, this is still a lot of trouble, after creating a class, even if another new class has a similar
function, you still have to recreate a new class. If we were able to replicate it based on an existing class, then add
Add and modify this copy to create a new class that would be much better. This effect can be achieved through inheritance , but there are also
Exception, when the source class (called the base class), the superclass (super Class), or the parent class, is changed.
The modified "copy" (referred to as the export class (derived class), the inheriting class (inherited Class), or the subclass (subclass, Child
Class) will also reflect these changes.

When you inherit an existing type, you create a new type. This new type not only includes all members of the existing type (
The Tube Private member is hidden and inaccessible), and more importantly it replicates the interface of the base class. So
Is that all messages that can be sent to the base class object can also send out the exported class. Since we can send messages via
Type of the class, so this means that the exported 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 (type equivalence) is understood
An important threshold for the connotation of object-oriented programming method.

Although inheritance can sometimes mean adding a new method to an interface, especially if the inherited Java is represented by the extends keyword
), but not always necessary. The second and other ways to make the difference between the derived class and the base class are to change the existing base
The behavior of the method of the class. This is called overloading (overriding).

Java object-oriented into learning!

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.