Thinking in JAVA notes -- Chapter 7 reuse class, thinkingjava

Source: Internet
Author: User

Thinking in JAVA notes -- Chapter 7 reuse class, thinkingjava

Reuse: use classes without damaging existing code

1. Combination: Use existing class object has-a in the new class

2. Inheritance: create a new class is-a according to the existing class

7.1 combined syntax

Define an object directly in the class.

* ToString: each class has a toString. If the compiler encounters an object where a String is needed, it will automatically execute toString.

The default value is toString in the Object class, getClass (). getName () + "@" + Integer. toHexString (hashCode (); Class Name + hash code?

Class object initialization:

1. Define

2. Constructor

3. before using the object: Inertia initialization can reduce the burden

4. Use instance Initialization

 

7.2 inheritance syntax

* All classes inherit the Object class implicitly.

* The subclass automatically obtains all the fields and methods of the parent class. The constructor automatically calls the constructor of the parent class first.

* A subclass can overwrite the parent class method. Is super used to represent the reference of the parent class? You can also add a new method.

7.2.1 initialize the parent class

* When creating a subclass object, the object contains a sub-object of the parent class, which is encapsulated inside the subclass. Subclass always secretly calls the parent class constructor before the first sentence of the constructor.

* If the parent class constructor has parameters, the super command must be used to display the call and must be located in the first sentence of the constructor!

 

7.3 proxy Delegation ??

My understanding: Using inheritance, all methods of the parent class are exposed to the outside.

Proxy: The combination is used, and the object is private. The public method is provided. The method internally calls the method of the combination object, which gives more control.

 

7.4 combined use of combination and inheritance

7.4.1 ensure proper cleaning

Java does not have the concept of C ++ destructor. If necessary, you must explicitly write the statements and ensure that class users know that they must call this method.

The cleanup action is usually put in the finally clause to ensure that it must be executed!

If you must handle the cleanup action yourself, be careful.

7.4.2 name Blocking

Java allows method overloading of the parent class in the subclass.

During overwriting, you can add the @ Override annotation to indicate that the method is overwritten. If overload occurs, the compiler reports an error to avoid overwriting!

 

7.5 select between combination and inheritance

A Combination usually uses the functions of existing objects, and is usually private only for internal use of the class.

It is also meaningful to set it to public, especially when the relationship between has-a is

The inheritance is-a, which is an abstract to a specific relationship!

7.6protected keyword

 

7.7 upward Transformation

It is required that the parent class reference can accept the reference of the Child class, so that the child class can do everything that the parent class can do! The only thing that may happen is the loss method.

7.8final keywords

Final usually indicates "This cannot be changed" because of design or efficiency

7.8.1final data

Java constants must be of the basic type. If it is a final reference, it can only be a reference that remains unchanged, and the object cannot be changed!

Blank final: no value assigned during declaration. value assigned before use

Final parameter: The reference cannot be changed in the method.

7.8.2final Method

Cause: Disable subclass Overwriting

Private: Implicit semantics prohibit overwriting, but essentially the private method is invisible to the subclass. Therefore, if a subclass has a parent class private method, it is not overwriting, but a "new" method!

7.8.3final class

The final class does not allow subclass!

7.8.4 advice

A bunch of nonsense, that is to say, you should consider carefully...

 

7.9 initialization and class loading

Loading Sequence: when running, static is loaded first, and the base class is loaded first.

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.