20190818 On Java8 Chapter VIII multiplexing

Chapter VIII multiplexing

Combined syntax

There are four ways to initialize a reference:

  1. When an object is defined. This means that they are always initialized before the constructor is called.
  2. In this class constructor.
  3. Prior to the actual use of the object. This is often called lazy initialization. At the cost of creating large and do not need to create every object in the object case, it can reduce overhead.
  4. Example initialization.

Inheritance syntax

Initialize the base class

Java call to be automatically inserted in the base class constructor derived class constructor.

Constructed from the base class "outwardly", the base class is initialized before the derived class constructor can access it. If not create a constructor for the Cartoon, the compiler will synthesize for you a no-argument constructor calls the base class constructor.

First calls the base class constructor, the most outward layer called from the base class.

Constructor arguments

Call the constructor for the base class must be the first operation of a derived class constructor.

Entrust

Java does not directly support third reuse relationship is called trust. This is between inheritance and composition, because you will be a member of the object being built on the class (such as a combination), but at the same time all the methods in the new class members of an object from the public (such as inheritance).

With a combination of inheritance

Ensure proper cleanup

In the cleaning process it must also be noted that the order of the base class and invoking a method of cleaning an object member, to prevent a child object is dependent on another sub-object. First of all, according to the reverse order of all class-specific cleanup created . (Generally, this requires that the base class elements still be accessible.) Then call the base class cleanup method.

Combined with the inheritance of choice

When you want to include an existing class of functions in the new class, a combination, rather than inheritance.

When using inheritance, use an existing class and the development of its new version. Usually this means using a generic class, and its demand for a particular specialization.

" Is a " relationship is inherited to express, and " there is a relationship" with the combination to express.

protected

protected also provides package access.

Upcast

FIG derived class inherits a base class transition is upward, it is usually referred to as upward transition. Because it is transformed from a more specific classes for a more general class, upcast is always safe.

Again on the combinations and inheritance

Minimize the use of inheritance.

Before using inheritance, ask "I need to upcast it?"

final keyword

final data

In Java, compile-time constant must be a basic type, and modified by the keyword final.

For primitive types, final make constant value, and for object references, final make constant reference. Once the reference is initialized to point to an object, it can not be changed to point to other objects. However, the object itself can be modified , Java does not provide a method to set an arbitrary object constants. This restriction also applies array, the array is an object.

Not because some data is considered final modified at compile time to know its value. When the value can be initialized at run-time.

Blank final

Or assignment must be performed in each final variable when defining constructor. This ensures that the final properties before use has already been initialized.

final parameters

In the parameter list, the parameter is declared as final means you can not change the basic parameter points to an object or variable in the method. This feature is primarily used to communicate data to the anonymous inner classes.

final method

The reason for using final approach are: a method to lock to prevent subclasses override the behavior by changing the method.

final and private

If a method is private, it is not part of the base class interface.

But if you create the public with the same name in a derived class, protected or access method package, none of these methods in a base class method of contact, you do not overwrite method, just create a new method only. Since private methods can not reach and can effectively hide, in addition to it as part of the class, and anything else that does not need to consider it.

final class

final class can not be inherited.

Initialization and class loading

The constructor is a static method of static keyword although it is implicit.

Class initialization sequence:

  1. Loading the base class . Regardless of whether the object is to create a base class, the base class will be loaded.
  2. Foundation class static initialization started, followed by a derived class, and so on.
  3. Create objects . First, all basic types of variables in an object are set to default values, object references are set to null - this is done by objects in memory to zero binary value generated in one fell swoop. Then calls the base class constructor. A base class and derived class constructor is configured in the same order as subjected to the same procedure. After completion of the base class constructor, instance variables initialized textual order. Finally, the remaining portion of the constructor is performed.

When we started to design a system, remember that program development is an incremental process, just like human learning. It depends on the experiment, you can do more analysis as possible, but still does not know all the answers at the beginning of the project. If the project considered as an organic, evolving of life to cultivate, rather than as effective as fast as skyscrapers, you can get more success and more rapid feedback. It may be a combination of inheritance and object-oriented programming allows you to perform such experiments in two basic tools.

Guess you like

Origin www.cnblogs.com/huangwenjie/p/11371775.html