Tij Reading Notes (sixth chapter)

Source: Internet
Author: User
Tags define constant constructor final inheritance reference
Notes
6: Multiplexing Class

The syntax used for compositing if you want to initialize reference, you can do this in the following time: 1. When you define an object.    This means that they have already been initialized before the constructor call. 2.    In the constructor of this class. 3. Before you will be using that object. This practice is often referred to as "lazy initialization (lazy initialization)".    This can reduce the overhead of a program if the cost of creating an object is high, or if you don't need to create the object every time.    Inherits the syntax used by. There is a general rule in inheritance design, which is to set the data to be private and to make the method public.    Of course, there are special circumstances to adjust, but this is a very useful guideline.    The initialization construction behavior of the base class is developed from "outward", so the base class is initialized before the constructor of the derived class accesses it.    Constructors with parameters for a derived class constructor, calling the constructor of the base class should be the first thing it does. Catches the exception thrown by the accumulating constructor.

Combining compositing with inheritance although the compiler will force you to initialize the base class, you will be asked to complete initialization at the beginning of the constructor.    But it does not check that you are initializing a member object, so you have to be careful. Make sure that clean cleanup is done first in the reverse order of the object being created.    The cleanup method for the base class is then called. The cover of a name

A "Yes (is-a)" relationship is expressed in relation to synthesis or inheritance, and the synthesis is expressed as a "has-s" relationship. The protected Java protected also provides package permissions.

Progressive development

Upload (upcasting): It's always safe.

Synthesis or inheritance, and then explore.

Final keyword final data for primitive, final will define this value as a constant, but for the object's reference, final means that the reference is constant. Java does not provide a way to make a constant of an object.    This limitation is also reflected in the array, as it is also an object.    The blank final data (Blank Finals) must be assigned the final data, either by assigning a value in the definition of the data, or by assigning it within the constructor.    Final parameter Java allows you to be final in the parameter list, and you cannot have the parameter reference point to another object in the method.    Final methods both final and private private methods implicitly have final meaning.    The final class does not allow others to inherit. Be careful with final

Initialization with the load inheritance of a class

Summary: This chapter mainly describes the concept and usage of inheritance and synthesis and some considerations. Although object-oriented change will emphasize inheritance, when you proceed to design, you should consider compositing first, and use inheritance only when necessary. Synthesis will be more flexible.


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.