OOP thought and Java implementation __java

Source: Internet
Author: User

Outline:

Java programming thought Chapter One object introduction extract the basic idea of OOP OOP and process-oriented difference OOP solves the problem. OOP performance in Java

excerpts from the introduction to the first chapter of Java programming thought

Abstract process:

All things are objects; A program is a collection of objects, they tell each other what they want by sending a message; Each object has its own storage of other objects; Each object has its type; all objects of a particular type can accept the same message; The object has state, behavior, and identity.

Each object has an interface:

A collection of objects with the same attributes (data elements) and behavior (functionality), so a class is actually a data type.

Each object provides a service:

An object is a collection of services that can be created according to different services, and is a program high cohesion.

The specific implementation that is hidden:

Use an access identifier to hide related content to prevent internal programs from being changed

The basic idea of OOP

OPP programming for object-oriented, the focus is on "objects" above, what is the object. All objects in nature, such as cats, dogs, flowers, grasses and so on, are objects, while object-oriented programming is a way of looking at the world in terms of programming. What a cat is like. is the coat color black or yellow? How much weight. How much body length. These "nouns" in human society, used to describe the state of an object, are called attributes in the world of the program. And a cat can fight with dogs, can catch mice, can call the owner to get up and shovel excrement feed cat food, these verbs in human society, used to describe what the object can do, in the world of the program called Method (also called function). In the face of object programming is to put this attribute, a method encapsulated in a class (object of an abstract description), a class as a whole, rather than divided into one attribute, one way to see, such a thought is the face of object programming ideas. For example, in a cat this object, we are concerned about the cat is a creature, I can play with it, can feed it, but I do not care how it and I play, how to eat my cat food, these are the cat thing, is the cat's own internal properties and methods, we do not care about it from the outside of this property and method is how to achieve.

the difference between OOP and process-oriented

The difference between the face and the process is the scope of the focus. In the face of process programming, we have to pay attention to a program with the beginning to the end of every detail, all the methods and attributes have to master, and object-oriented is not. For example, I feed cats to eat cat food in this one action, in the object-oriented thinking process is: I feed the cat--cat cut carbohydrates; in the process of orientation is: I what time, what place, what to take cat food, how to feed cat food to the cat, how to eat cat food, how many eat, eat when what expression, after eating will not say very good, Each of these details we need to focus on, which is a stark example of object-oriented and process-oriented. In the object-oriented, everything is already encapsulated, it is only the concept of me and the cat, and the process is concerned about all the details.

What did oop solve the problem

OPP solves the problem of code reuse and what is code reuse. Code reuse is not repeated writing code, by directly calling the class instead of copying, sticky process. There are also some, for we do not understand, not clear of the program segment, we can also invoke the class written by others to achieve our own desired results, this is a very convenient and efficient thing, and in the object-oriented code reuse to achieve the degree of lunacy.

In Java, the implementation of OOP mainly manifests itself in inheritance and interface.

First of all, in human society, children inherit the skills of their parents, inherit the property of parents, what parents have, children have what, that is inheritance. But children can not just eat their parents ' savings, they also add their own things on the basis of their parents, such as using the Internet, learn to drive cars, these are not what parents do. For parents to do things, children can also change him, such as children unwilling to use artificial to polish a product, they learned the product automation production, these are feasible. Also in programs, subclasses inherit the parent class, so subclasses have all the attributes and methods of the parent class, and subclasses can add their own properties and methods, or override the parent class's method, which is inheritance.

Because only single inheritance is supported in Java, what should a subclass do if it resolves a method that wants to inherit multiple parent classes? The interface appears to solve this problem. To understand interfaces, you have to understand abstract classes first, because they are similar things that can help you understand interfaces well. Abstract classes have abstract methods, abstract methods have only class names, there is no concrete implementation. For example, there is a method public abstract String name () in class people; In this method, a person's name is returned, and the class to which the method belongs is the parent class, so there is no implementation of this method in the parent class, and if a subclass inherits the class, Then you must implement the abstract method of the parent class in the subclass. The advantage of an abstract method is that unifying the need to implement a method under the inheritance chain, preventing programmers from forgetting to write some methods and causing bugs in the program, in another respect, the abstract method of the parent does not need to know the specific implementation of subclasses, and improves the readability of the program, for example, students, teachers, workers inherit the People class, Every student, teacher, worker has his or her own name, and the name must be written out, and the abstract class enforces the programmer to implement the method, and the unique names are implemented in subclasses. Next to the interface, it should be noted that the interface within the methods are abstract methods, you may have questions, abstract methods to be implemented in the subclass, in the subclass of code, and did not solve the code reuse, then the appearance of the interface for what it is. The answer is still code reuse. We assume that there is a lampholders and a number of light bulbs, bulbs and lampholders have different shapes of the interface, there are triangular, square, with a round, assuming that there is a way, his action is to install the bulb on the lampholders. We use the traditional method, to a triangular interface bulb we give it a triangular interface of the lamp holder, to a square interface of the lamp, we give him a square socket socket, this is the way to look for different lampholders. So is there a way to realize that I can use only one lampholders to connect a lot of the total type of light bulb. The answer is yes, we can use a conversion head, the different types of light bulbs into a circular interface, then I use whatever kind of light bulb, I can use that a round lamp. And this conversion is much simpler than the action of the Lampholders, which is the interface, and the function of the interface.

=====2015/12/21 23:13======

Correction: Java is not supported multiple inheritance, but support the declaration of multiple inheritance, the role of the interface is declared, once a class declares an interface, then he also inherited some services, in the broadest sense, the declaration is also inherited.

And because of the interface and inheritance, so that polymorphism has a useful, what is polymorphism, polymorphism is the parent class application to the subclass object.

=====2015/2/26 11:33======


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.