Programming | Repeating Java has a lot of praise of the function, the application of code is one of them. But if you want revolutionary change, you have to go far beyond the old code of "Replicator code, then change": combination (composition) and inheritance ( Inheritance)
ToString (): Objects of each non-basic class have ToString (), which is called when the compiler expects a string and you have only those objects in your hand.
Test tips: "Provide main () for each class, regardless of class is public", you can start each main () at the command line. This makes each class unit test easier. And after the unit test is complete, you do not need to delete main (); You can leave it to be tested later.
Super Keyword: You might call a function (usually a ()) in the base class in a function in derived class (for example, A ()), which you certainly cannot call directly, because the same identifier causes recursion. To solve this problem, Provides a keyword super. He represents the Super class (superclass), so you can call it: Super.a ();
Initialization of base class: calls the base class constructor. The Java compiler automatically inserts the invoke action on the base class default constructor in the derived class constructor. But if your class does not have the default constructor, or if you want to invoke the base class constructor with arguments, you have to use the keyword Super and match the appropriate citation sequence to clearly write the call action, such as Super (i); (Note that this is different from the syntax for calling general member functions.) Also, a call to the base class constructor must be the first thing that the derived class constructor does (if you do something wrong, the compiler will remind you).
To catch the exception of the base constructor: the "first thing" constraint above also makes it impossible for the derived class constructor to catch all exceptions from base class. Sometimes it's inconvenient to hear.
Maintain proper cleanup: because destructors do not exist in Java but sometimes we want to do some cleanup when the object is destroyed, and then put the code in the final block.
Name hiding: A function can work correctly whether it is defined with a layer class or its base Class,java overload (overloading) mechanism. Of course, using the same name as "signature" and the return category "Write" (override) the same function, so that in the ordinary case, this also makes the polymorphic basis.
Keyword final: The basic meaning is "this cannot be changed." Based on design and efficiency, we may want to prevent change. These two reasons are very different and can lead to the misuse of the final keyword. The final three places can be used: Data,method,class.
Final data: Used to tell the compiler that a piece of data is "fixed". The use is as follows:
1. Use immutable "compiler constants (Compile-time constant)".
2. Can be initialized during the execution period (run-time), but you don't want to change it.
If a data is static and final in time, they have a storage space that cannot be changed.
Blank Finals:java allows for "white finals", which allows us to declare the data member as final without giving the initial value, and in any case, Blank finals must initialize before use, and the compiler will enforce the guarantee. But blank finals offers more flexibility than final. Because the final data members within this class can be different in each object.
Final arguments: Declaring the argument final means that you cannot change the argument (a reference) to another place in this function.
When the base type argument is declared final, it means that you can read the value represented by the argument, but you cannot change the value.
Final method: There are two reasons to use it:
1. Locking this function to ensure that it is not derived class replication (override) is a design consideration.
2. Allow the compiler (but this action does not necessarily occur, the compiler will have a judgment action at this time) to convert all call actions on this function to inline (inline) call. This is a kind of efficiency consideration.
Since the above two points also work, all of the time you have to weigh the value to use final method: for example, a function is small you want to convert it into an inline function but at this point it will completely lose the possibility of being override. So it's best to declare a certain function town to be final when it's small or if you really don't want him to be carbon.
All private functions in class will automatically be final because they cannot be used by other classes, and cannot be overwritten. This illustrates a very important feature: overriding can only occur when the function belongs to the base class interface.
Final classes: The reason is that you don't want it to be inherited (either you are in the privacy consideration or you are out of design considerations)
Note that whether the class is final or not, data members can be either final or final.
But all the functions in final class are also final, because they cannot be overwritten.
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.