Thinkinginjava Study notes 06_ reuse class

Source: Internet
Author: User

Marsedit Sticky code Good Trouble, all the code handed to github:https://github.com/lozybean/myjavalearning

Reuse a class commonly used in two ways: combination, inheritance;

Combination

The object reference is placed in the new class, and the new class completes the reuse of these objects;

In Java, each non-primitive object has a ToString method that is called directly when a string object is needed, and the object's reference is initialized to NULL in the definition of the class, and the reference initialization can be done in four places: Object definition > Instance initialization > Class Builder? > Before objects are used (in order of initialization); Sample code

Inherited

Use the extends keyword to inherit the members of a class (except private);

From the sample code, you can see that the main method can exist in more than one class, so that it's easier to do this for unit testing, even though cleanser is not a public class, but public main () can still be called externally ; that is, even if a class has only package access, its public main () is still accessible;

The detergent class uses extends to inherit cleanser, which enables the reuse of all inheritable methods in the cleanser, and in addition, some methods can be rewritten in the detergent class, such as the scrub () method, during the rewriting process, If you need to call a method of the same name of the base class, use Super.scrub () to invoke it; Of course, the detergent class can define methods other than inherited methods;

The base class is initialized before the subclass is created, that is, when an object of a subclass is created, not only the interface of the base class is copied, it contains a child object of the base class, which is wrapped inside the object of the subclass and has the same properties as the object created directly from the base class, such as: In the sample code, Before the cartoon class constructor can access the object, the constructor of the base class can be accessed and initialized, and even without the cartoon constructor, the compiler produces a default constructor and calls the constructor of the base class, and as you can see, when Class C inherits Class A, And with a Class B object, if the Class C object is instantiated, the Class A constructor is called first, the Class B constructor is called, and the constructor is called itself (not called if it is not defined);

When a constructor in a base class is not the default constructor, but is a constructor with arguments, and there is no default constructor, you must use Super (args) in the subclass to call the constructor and pass in the corresponding argument list, otherwise you cannot complete the construction of the base class, and the compiler will error; Sample code

The constructors in a subclass are executed in the following order: A constructor that contains other class objects (when initialized in the constructor, a sequential level with the subclass constructor), a base class constructor, a subclass constructor, and a constructor that contains other class objects that must be called correctly if the first two do not have a default constructor. The base class must use Super (args) to call the constructor correctly; Sample code

The initialization process for the inheritance process is: 1. Load the base class, static initialization of the base class, 2. If, in the process of loading the base class, there is also a base class, the base class is loaded, static initialization is performed, and then the static initialization process in the first base class is returned, and so on; 3. After the base class is loaded, the object is created, the base class constructor is called (generating the implied sub-object), and then the subclass constructor is called, and the object creation is completed, as in the example code, when the Beetle.main () is executed, the Beetle class is loaded, a base class insect is found, and the base class is loaded. With the initialization of its static member, then completes the static initialization of the Beetle class, then executes the println statement sequentially, then creates a new Beetle object, loads the data member of the base class first, then calls the constructor of the base class, and then the data member of the subclass is loaded, Finally, the constructor of the subclass is called, and only such a process can guarantee that the base class member to which the subclass depends is properly initialized;

Agent

Java does not provide direct support for the proxy, placing a member object in the class being constructed, but exposing all the methods of that member object in the new class, adding an object in the new class that wants to inherit the class, and then implementing the method of the object in the new class, at which point, Objects created with the proxy can have the same interface as the class that is inherited by the implemented method, but the proxy can choose to inherit a subset of the methods, so it has more flexible control force; Sample code

Clean

In Java, destructors can be called automatically when an object is destroyed (the Finalize () method), and when it is necessary to perform some necessary cleanup activities within the life cycle of a class, in addition to the Finalize () method, it is not known when Java destroys an object. However, placing the method in a finally clause guarantees that the method is invoked (whether it is a normal execution or an exception exit);

try{
? ? ... do something
}?finally{
? ? X.dispose (); Here is your own implementation of the cleanup method, the method is bound to be executed;
}

When implementing a cleanup method, you must pay attention to the order in which the base class and member object cleanup methods are called, to prevent a child object from being dependent on a child object, and (in general, in the reverse order of the constructor invocation), such as: sample code

Covered

When you overload a method of a base class in Java, you do not need to mask the method (such as in C + +), but you can use all the overloaded methods in the base class, such as: In the sample code, Bart overloads the method Doh and uses the new parameter list. But the object instantiated by the Bart class can still invoke the same name method in Homer;

Such a feature can sometimes cause confusion, so Java SE5 adds a new @override comment (not a keyword), and when the comment is added to a method, if the subclass calls the method directly it generates an error, and the method must be overloaded to be used correctly; (Suddenly understand Swift Various override used in iOS development learning)

Choose

Although both combinations and inheritance can be reused, the combination is to explicitly place the child object into the new class, and the inheritance implicitly places the child object in the new class;

The combination is often used to implement the functionality of an existing class in a new class, rather than its interface, where an object is embedded in the new class, allowing the object to implement the required functionality, because the user of the new class wants to see the interface of the new class, not the interface of the embedded object, so the object needs to be embedded with private, but sometimes It is meaningful to allow the composition component in the new class to be visible, and the object should be embedded with public at this time;

Inheritance is often used to use an existing class, and to develop a special version of it; inheritance can get the protected domain in the base class, which cannot be accomplished by the combination and the proxy; the more important aspect of inheritance is performance: The new class is a type of existing class (the noun translated in the book, it feels easy to mix, The type here is not a term in a programming language, but a type in a natural language, and through inheritance it is possible to achieve upward transformation, that is, the example mentioned in the introduction, a method of a base class can directly deal with subclasses, the benefit is to generalize the method to achieve specialization, if the design does not explicitly require an upward transformation of the operation, The use of inheritance should be carefully considered, and the common combination;

While the agent is used to implement the function of sub-objects, adding the corresponding interface method is to re-implement a part of the base class;

Final keyword

The final keyword means that it cannot be changed;

When final is used for data, it means that the data is a constant, or a value initialized at run time, but does not want to be changed; In practice, the fields that are both static and final are capitalized, and each word is split with underscores; Java allows blank final to be generated. That is, the final is not initialized directly, but the blank final must ensure that it is initialized before use;

When final is used for a parameter, it means that the object referenced by the parameter cannot be changed in the method;

When final is used for a method, it indicates that the method is not locked, cannot be overloaded in inheritance, and that all private methods in the class are implicitly added to the final keyword;

When final is used for a class, it means that the class cannot be inherited;

Thinkinginjava Study notes 06_ reuse class

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.