First knowledge of Java this little girl (iii)

Source: Internet
Author: User

That's a rotten object-oriented.

What I want to say about object-oriented is actually a little disgusting thing that I feel.

It is a concept that is so elementary for getting started with the Java language. As a veteran, you can spit on me, I can dry them, but as a summary, I have to say.

Because it's not that easy for someone who has never been in touch with a programming language. any simple thing, just because you will, and for the person who does not, as the general difficult.

That year as senior small white I listen to the teacher for the first time that the concept, the face is written on the big two words: "Meng force." Object oriented? Object? In the face of the object, stay ... Well, this object is not the same as the object I'm thinking about. Because the teacher told me that in the Java world, everywhere is the object, such a harmonious world.

In fact, it's just a way of thinking about solving problems in the Java language. That is to solve all the problems, have been categorized into two aspects, what, can do. It's not like the three ultimate questions of philosophy: Who I am, where I come from, where I'm going. Seemingly simple, in fact, is a reflection of the world.

For example, a car, the prime minister to understand is, what is a car, what kind of an object can be called a car, what should have properties? is called a property in Java . What are the behaviors of automobiles? It can run, the horn will be called, can go forward, can regress and so on such behavior is called the method .

Another example is the company. What properties does the company have? Where it is, how many people there are, how many men there are, how many women employees and so on. And what can the company do? Can operate, income generation, tax and so on so much behavior.

So again to understand that everything is the object of this phrase. That is, everything can be seen, objects or things that are perceived can be abstracted as objects, which are recognized by the language. In fact, when I really began to use the object of this idea to understand the whole world, it is quite scary.

Before I touch programming, I think about things in a person's perspective.

For example, is it very easy to find the largest number from a string of 1,4,5,2,3 numbers? Yes, it's a different eye. That's just because you're too young. In the process of seeing it at one glance, the brain undergoes a process of thought. I tried to magnify this moment by 100,000 times times. First, I saw a bunch of numbers. Some of this number is in my mind. I try to find the maximum value in the previous two, or the first few, and remember the maximum. Then start the iteration, whether you can find a larger value and replace the previous maximum value. isn't it? Right? Slowed down time, the original brain is not a glance at the maximum value. If this can be understood, then it is understood that the idea of language is just a way of simulating the thinking of the brain. And perhaps all things to the most essential, are following the same principle. Just like ropes notes, we are just trying to understand the world through our own way.

In terms of object-oriented, it's about three basic features: encapsulation, inheritance, polymorphism.

First basic feature: encapsulation

In fact, the concept of encapsulation begins as soon as we try to abstract all the specific things and problems into objects. when Java has so many objects, it's as if a child has all kinds of bricks, and these objects can work together to achieve the final goal.

Encapsulation, for Java, is the class. All objects are presented to us in a class manner. A class for Java is given below, as shown in the example above.

 Public class car{                                                                     }

We have defined a car class. Secondly, what properties does the car have?

For example, cars are long, wide, tall and color. How to describe it? It's like painting, I'm not writing code, I'm painting, how do we draw a car we think?

 Public class car{                                                                       int  length;   int  width;   int  height;}

Drawing on its basic properties, what can this car do? The car can drive forward and fall backwards.

 Public class car{                                                           int  length;   int width;   int height;                                                                                     Public void GoForward () {}    Public void Gobackward () {}}

A car has been drawn up so well. It also encapsulates a car, which is a car in the Java world, as if it were in a painting.

Encapsulation hides the internal implementation mechanism of the class, which can change the internal structure of the class without affecting the usage, and also protects the data. For the outside world its internal details are hidden and exposed to the outside world just by its access methods.

The second basic feature: inheritance

Encapsulated objects can already work as a basic unit in Java. But imagine a situation where I now need to recreate an object, truck.

Truck as a kind of car, naturally has all the properties and methods of car class. (as described above, it should be no stranger to the notion of properties and methods)

So should I recreate a class? Like what:

 Public class truck{                                                        int  length;   int width;   int height;                                                                                           Public void GoForward () {}    Public void Gobackward () {}}

is not a burden, this is the role of inheritance. A thing is not created for no reason, there is the meaning of existence. In Java, when a class inherits from another class, the inherited class becomes the parent class, and the successor is called the subclass. This is achieved by extends the keyword. The code is as follows:

 Public class Truck extends car{                                                                                     }

This type of writing is the same as the one that defines truck directly above. The truck class already has a public property about the car. Then we'll define the method that it's private.

For example, the truck has cargo capacity this property and the truck can pull:

 Public class Truck extends car{  int burden;   // Cargo capacity                                                               public   void pullthings () {}     // laden }

Inheritance greatly reduces the amount of repetitive code written, reduce the burden of system operation, but also very good maintenance, such as we want to add to the car all the cars have a common property, then what to do? After using inheritance, we only need to increase the parent class car, so all the trucks, cars and vans have the added properties. You don't have to add a class in a class. Isn't it convenient? inheritance is to reuse the parent class code. Two classes you can use inheritance if there is a is-a relationship.

The third basic feature: polymorphism

Polymorphism refers to the specific type of reference variable defined in the program and the method call issued by the reference variable is not determined during programming, but is determined during the program's run, that is, a reference to which the variable bottom will point to which class the instance object, the reference variable emitted by the method call is exactly the method implemented in the class. Must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism.

For example, you are a wine God, have a unique feeling for wine. Some day home found a few cups inside are loaded with white wine, from the outside to see we can not know what this is what wine, only after drinking to be able to guess what kind of wine. You drink, this is Jiannanchun, drink again this is wuliangye, again drink this is alcoholic wine .... here we can describe it as follows:

Wine A = Jiannanchun

Wine B = Wuliangye

Wine c = Alcoholic drink

What we are doing here is polymorphism. Jiannanchun, Wuliangye, alcoholic wine are the sub-class of wine, we just through the wine this one parent can refer to different subclasses, this is polymorphic: We only run when we know the reference variable point to the specific instance object.

In the previous article, I was deeply grateful for the questions raised by my predecessors.

Thank you for your interest. Scan to identify the following QR code.

First knowledge of Java this little girl (iii)

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.