The original Java world has so many wonderful stories, learning Java is really interesting!

Source: Internet
Author: User
Tags garbage collection modifiers

There is no world . There is not only the human world, but also our Java world in this world.
Let me take you through this internship guide today to learn about the wonders of our Java world.
  • There is a warm man called catch, there is a true love called Try---catch, the world's most sincere love is you in the try I catch, no matter what your temper, I quietly accept, silently deal with, no matter what you have wrong, I will forgive you, love you.
  • There is a lazy employee called throws, do not have anything to throw out, do wrong to the superior wipe the buttocks, the superior will not give superiors, to the boss so far, the boss will not that GG, closed it.
  • There is a kind of obedient employee called circulation, you let him do several times work he do several times, let him not to sleep always overtime he really dare to do, no boss break's permission is flatly dare not stop.
  • There is a kind of overbearing president called Finally, as long as he wants to do things must be done, even if it is return to stop all not, unless death, otherwise he will desperate to get his prey.
  • There is a garbage collection station called Finallize, he and our overbearing president finally's name very similar, but must not be called wrong, otherwise the consequences are very serious. Important things say three times, can not be called wrong, can not be called wrong, must not be called wrong!
  • There is a kind of lakes and rivers called the class, the lake has a person called the object, the person has the skill is called the method.
  • There is a name code called identifiers, specifically used to give classes, methods and variables of the name, the code stipulates that must be the letter, underscore _ or dollar $ sign for the beginning of the line, but also very sensitive to the case, and must not be a keyword, like class,catch that is never used to name.
  • There is a rule in the code called the Hump rules, the name of methods and variables when the first word must be lowercase, the second, the third word capitalized, this is the hump rule. Of course, if it is the name of the class, it is the first letter of each word must be capitalized, do not ask me why, is such a cow.
See here may be some people have doubts, what is a variable?
don't worry, I'll tell you.
the variable has three brothers, divided into eldest brother static variable, second member variable and old three local variable.
eldest brother, more selfless, to share their own out; the second is more naughty, in the river, often in the class and method of free access; As for the old three, obediently one, like and methods play together, when the method disappears, old three will follow the method immolation together.

Do you know why the boss is so selfless, because the eldest brother was shared the boss static influence, all heard the static lecture variables will be affected by it, willing to share their own.
This static has a very large static family system, has static code block, takes precedence over ordinary code block first execution, static inner class, can be directly called by the external class, static method, do not need the object can be called by the class.
The static family is loaded with the load of the class and takes precedence over the existence of the object. So you can't use the This keyword in the static method, why? Because this represents a reference to the current object, in short, this is not yet born when Static appears, so this cannot be used in the static method. There is an old stubborn called final, was final identified things can not be changed, who dare to move with who anxious, small partners are afraid of him, especially class, meet final will be tied, henceforth no longer have sub-category. It's a sad, tearful story. Variable meet him, henceforth no longer tall. Method meet him, henceforth no longer quilt class rewrite. So, for those who don't want to change themselves, the variables can go to final.   There is a kind of thought called everything objects! where did the object come from? In the eyes of the programmer's father, that's the new one.  see this is not a bold idea, is not thinking of themselves or a single dog, eager to give themselvesnew An object comes out. Bold.New, sexy years,Newyou're still a single dog. The object is still an object of the class used to invoke the method and member variable drops. An example of creating an object: <ignore_js_op>basically each class has at least one constructor, except for the anonymous inner class, and if a class does not write any of the constructor methods, then the class takes an empty parameter construct. If there is a parameter structure, then the value can be initialized directly when the object is created, which is called a convenience.

example of a construction method: <ignore_js_op>The class grows up also should marry to have the son, the class child is called the child class, this name is easy to understand. Subclasses can beextendsinherit the methods and properties of the parent class, as if the children of the human being can inherit the property of their parents. Some subclasses are independent, unwilling to mediocrity, at this time they added some of their own properties and methods, the more powerful sub-class not only the self-creation method, but also to improve the method of the parent class has become stronger, that is, the method of rewriting. examples of inheritance and method overrides:<ignore_js_op>However, the constructor subclass of the parent class cannot inherit, and subclasses can only call the parent class construction method through the Super keyword. Like a father has a house, but the father will continue to live, not to the son, gave the son a key, son can often come back to live.

in ourin Java, a subclass can inherit only one parent class, and like humans, a son has only one father. What? You asked me if I could have a godfather? Of course, the interface is the godfather of the subclass, and a subclass can have multiple interfaces, the subclass nameImplementsInterface name , interface name can implement multiple interfacesThere is a kind of secrecy called encapsulation, encapsulation can hide some of the information in the class inside the class, in addition to this class does not allow other classes to access directly, after all, who has a small secret is not to let people know.
so how to encapsulate it? Simple, just add private to the front of the property. Examples of encapsulation properties: <ignore_js_op>If you want to modify and view the encapsulated properties, you need to pass the Get (), set () method. data that is not particularly important can be modified using protected, which can also be accessed in different packages, as well as two access modifiers, public and default (which is not), public is more hospitable, and anyone can access him. four kinds of permission modifiers: <ignore_js_op>encapsulation can hide the implementation details, just like human have a TV, human only need to know how to use, and do not know the TV components, but also very safe, because others can not see Ah, haha ha! There is a kind of deception called polymorphism, to raise a chestnut bar: I heard the recent human popular online love, but no line to meet who do not know what kind of people on the end of the phone, ah, the video is too vague to see, so according to cheat appeared, human photo PS of the Mei Da, big Lianpan into oval face, small eyes become big eyes, A variety of whitening grinding skin, and then send the photos to each other to attract each other, finally the relationship has further developed to the extent of the current, then there is no then. The story tells us that you never know what the other side of the phone looks like without actually seeing the other person. Polymorphism is also a truth, the left side is a same. There are 3 necessary conditions for polymorphic existence: 1. To have inheritance 2. To have a method override 3. Parent class reference to child class objectPolymorphism is divided into two forms, one is the upward transformation, the format is: the parent class name parent class object = new subclass name (); The upward transformation object cannot use the new member variables and methods of the subclass;
There is also a downward transformation, the format is: sub-class name Subclass Object = (parent class name) parent class object;a down-transition object can invoke both the parent class method and the methods and properties of the subclass itself. I have a formula to share with you: (for upward transformation) polymorphic member variables and methods: Compile to see Left (parent Class), run to see right (subclass); Polymorphic member method: Compile to see Left (parent Class), run see right (subclass)
make a downward transitionYou must first make an upward transformation, or you will get a type conversion exception---classcastexception. examples of polymorphism:
we all know that Van Gogh, the famous abstract painter in the human race ,JavaThere is also a class in the family called abstract class. Abstract class is a strict parent class, his abstract method has no method body, he provides a generic template for subclasses, can avoid the arbitrariness of subclass design, subclasses must rewrite the method of the abstract parent class.
both abstract methods and abstract classes must be usedAbstract, and abstract classes can also have no abstract methods.
Examples of abstract classes:<ignore_js_op>

referring to an abstract method, you have to say the interface, all the methods in the interface are abstract, all variables default to the public static final decoration, that is, the variables in the interface are static constants, the value can not be modified. Examples of interfaces:<ignore_js_op>finally give us a good picture of Amway, human circulation of the story of Sleeping Beauty, the princess met the spindle let the curse come into force, and in the castle has been sleeping waiting for the Prince's kiss to wake her up. And weJavaThere is always a picture in the world:
There is a kind of spindle called wait (), there is a kiss called Notify ().
There is a beautiful thread princess, she met wait (), unless the Prince kissed her, otherwise she would have been asleep, and soon the thread Prince appeared, the Prince with notify () to the princess woke up. Since then the princess and the Prince have been together to live a happy and beautiful life.
How about that? I've seen so much, I think weJavaThe world is quite interesting, is not still want to continue to explore it?



This article source: Shanghai Shang School, tease the yard farmer-when you are old:
Https://www.shsxt.com/it/java/1258.html

This is the original article of the Academy students, thank the still school dear students, you are really the Han cold in the programmer world. Reprint Please indicate the source.

The original Java world has so many wonderful stories, learning Java is really interesting!

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.