Java Series 1--talking about object-oriented

Source: Internet
Author: User

Perhaps every computer professional , at the university of Java, when the teacher began to say that a word, "Java is an object-oriented language ." So what is the object of Oo and what is his advantage, or is he stronger than the other popular languages C + + ? What does "object-oriented" do in our actual programming?

before we start the actual Java tour , we laosheng a long conversation. I'll just say it in my own understanding, combining my programming experience for two years or so. In fact , before Java I was learning C and C + + , but did not actually write C or c + + What project, so, at first I was directly using Java , in the first year, my understanding of object-oriented is confined to the book of "Encapsulation", "Inheritance", "polymorphism", and these three words for me at that time, that is, three nouns, there is no understanding.

before we talk about object-oriented programming (OOP), first we need to say that there is no pre-OOP programming situation, when everyone is doing process-oriented programming, and what is process-oriented? Actually this thing I also can not say its meaning, because did not take C and other process language developed, simply speaking, the process-oriented language core is a method, each method to achieve a specific function, To achieve a more complex function must be in order to assemble these methods, it seems that this and Java programming is the same kind of, the difference in the end where AH? The difference is in the operation of the data, in C , the method operation of the variable is either a local variable in the method, only for their own use, or all the methods can be shared global variables, he did not just for a few of the relevant methods used by the "small local "Variable, this is his biggest malady, and this is the problem that the object-oriented language solves."

deep down in my heart, I always thought, "programmers use language to simulate the world!" "

to use language to simulate the world, that first we have to simulate the real world of things, and the different things will have only their own attributes, such as people have ears, nose, eyes, mouth, he can listen, breathe, see, eat, the car there is no such things. We can listen, breathe, see, eat. These verbs are understood as a method in C, while the ears, nose, eyes, and mouth understand the properties of the manipulation of these methods, we want to exercise, these organs need to be coordinated, that is to say, these methods can work together these properties, then in C In the language, these attributes need to be stored in a number of global variables accessible to all methods, but the car launches, the method of flying does not need these variables, these variables are only needed when people exercise, that is, these variables should only be visible to the movement of people, the things are invisible, but C these process-oriented languages do not have these.

This is the creation of a powerful object-oriented Java language.

He combines the attributes of a class of things and the actions of manipulating them (and the methods in Java) into an object (note that the object here is generalized, not an instantiated object in the Java language Class), Let these properties only allow the methods in this object to operate (access), which is the class in the Java language. The behavior of combining these attributes and methods into a class is "encapsulation" in the three main features of object-oriented .

What is the meaning of "inheritance" and "polymorphism" of the three major features of OOP?

"Inheritance" literally means getting what belongs to them from their ancestors and then becoming their own. In Java , "Inheritance" is also the case. For example, there is an animal class, he has eyes, nose, ears, mouth, can see, breathe, listen, eat. And people are animals, there are those properties and methods, then in Java , we go to create a person this class when there is no need to write this property and method again, we only need to inherit the class of animals, and then in the human class to create only the talent has attributes and methods , such as doing homework, writing code, etc. these are only human methods.

So how does "polymorphic" understand? It is a literal understanding that the same method is used to process objects from different classes. Of course, these methods must be derived from the same parent class, meaning that the classes of these different objects must inherit the same base class. Like what animal (animal (Eat () { eat }) This method, human (person) , Ox (cattle , and they all rewrite it. eat () This method, person eat () { eat rice },< Span lang= "en-US" >cattle eat () { grazing }

at this time, I use animal class affirmed two objects Animal a1,a2 ; then use the person and cattle to instantiate him, and then call the eat () method, the pseudo-code is as follows:

A1 = new person ();

A1.eat ();

A2 = new cattle ();

A2.eat ();

A1And A2 are Animaleat () animaleat () personcattle? Of course the correct approach is a1 call person eat () way to eat rice, a2 go to call cattle eat () way to eat grass. and javajava

Maybe people will ask at this time, how did he do it? This specific working principle that we put in later articles explains in detail, this involves the dynamic binding of Java. Since I am writing this article after work, now the time is 00:28, tomorrow will work, so this article is written here. The next article continues ...

Java Series 1--talking about 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.