JAVA: Class Regeneration

Source: Internet
Author: User

Reuse code is one of the many compelling features of Java. But to be a very revolutionary language, it is not enough to just copy and change the code, and it must be able to do more.

Two types of code reuse mechanisms: Composition and inheritance

1. Combinatorial grammar

Grouping: Placing an object reference in a new class

A class can be automatically initialized to zero when its domain is a base variable. But object references are initialized to null

Initialize the reference, which can be done in the following location in your code:

1. Where to define the object. This means that they can always be initialized before the constructor.

2. In the constructor of the class

3. This approach is called lazy initialization before the objects are being used.

4. Initialize with instance.

2. Inheritance Grammar

Inheritance is an integral part of all OOP languages and Java languages. When you create a class, you are always inheriting, because unless you explicitly indicate that you want to inherit from another class,

Otherwise, it is implicitly inherited from Java's standard root class object.

Class A extends b{}

2.1 Initializing the base class

Using the Super keyword

3. Combination and Inheritance

There are many times when it is required to combine the two technologies of synthesis and inheritance.

3.1 Ensure the correct removal

The garbage collector automatically reclaims memory when necessary. Garbage collectors work well most of the time, but in some cases our classes may take some action in their own time,

These actions require a clear clearance process.

The Order of garbage collection: You cannot expect to know exactly when a garbage collection will start. The garbage collector may never be invoked. Even if it is invoked, it may reclaim the object in any order that it wishes.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

In addition, the garbage collector mechanism of the Java 1.0 implementation typically does not invoke the Finalize () method. In addition to memory recycling, it is best not to rely on garbage collectors for recycling.

If you want to clear anything explicitly, make your own cleanup method, and do not rely on finalize (). However, as previously noted, you can force Java1.1 to call all closure modules (Finalizer).

4. Re-discussion on synthesis and inheritance

In object-oriented programming, one of the most likely ways to create and use code is to encapsulate data and methods in a single class and use objects of that class. There are times when

New classes need to be constructed using the "compositing" technique with ready-made classes. And inheritance is one of the most uncommon practices. Thus, while inheritance has gained a lot of emphasis in learning OOP, it does not mean that it should be used everywhere as much as possible.

On the contrary, use it with particular caution. It can only be considered if it is clear that inheritance is most effective in all methods. One of the easiest ways to determine if you should choose to synthesize or inherit is to consider whether

You need to return to the base class from a new class. If you must go back, you need to inherit. But if you do not need to go back to modelling, you should remind yourself to prevent the abuse of inheritance.

5.protected

It means "it is private, but it can be accessed by anything inherited from this class or any other thing within the same package." In other words, protected in Java can become "friendly".

6. Cumulative development

One benefit of inheritance is that it supports "cumulative development", allowing us to introduce new code without causing errors to existing code. This will isolate the new error into the new code. By inheriting from a ready-made, functional class,

By adding new members and methods (and redefining existing methods), we can keep the existing code intact (others may still be using it) without introducing their own programming errors. Once an error occurs,

Know it must have been caused by its own new code. As a result, the time and effort needed to correct the error is much less than the body that modifies the existing code.

7. Tracing the shape

The most noteworthy part of inheritance is that it does not provide a method for the new class. Inheritance is an expression of the relationship between a new class and the underlying class. You can summarize the relationship by saying that the new class belongs to a type of an existing class.

What is "tracing the shape"? It is called this name, in addition to a certain historical reasons, but also because in the traditional sense, the representation of the class inheritance graph is the root at the top, and then gradually downward expansion.

8.final keywords

It means "this thing cannot be changed." The reason to ban change may be to consider two factors: design or efficiency. We will discuss three applications of the final keyword: data, methods, and classes.

8.1 Final Data

Many programming languages have their own way of telling the compiler that a certain data is "constant". The constants are mainly used in the following two areas:

(1) The compile-time constant, which will never change

(2) A value initialized at runtime and we do not want it to change

For a basic data type, final will change the value to a constant, but for an object handle, final will turn the handle into a constant. When making a declaration, the handle must be

Initialize to a specific object. And you can never turn a handle into another object. However, the object itself can be modified

Blank final: They belong to some special fields. Although declared final, it did not get an initial value. In either case, the blank final must be properly initialized before it is actually used.

And the compiler will take the initiative to ensure that this provision is implemented. However, blank final has the greatest flexibility for all applications of the final keyword. Now force us to assign to final--

Either use an expression when defining the field, or in each builder. This ensures that the final field gets the correct initialization before it can be used.

Final argument: Java 1.1 allows us to set our arguments to the final property by making an appropriate declaration of them in the list of arguments. This means that in the interior of a method, we cannot change the argument

The handle points to something.

8.2 Final method

The final method may be used for two reasons. The first is to "lock" the method to prevent any inherited class from changing its original meaning. When designing a program, if you want a method to behave

This can be done if the inheritance period remains unchanged and cannot be overwritten or rewritten. The second reason to adopt the final approach is the efficiency of program execution.

8.3 Final class

If the entire class is final (prefixed with the final keyword before its definition), it means that you do not want to inherit from this class, or that no one else is allowed to take this action.

9. Initialization and class loading

In many traditional languages, programs are loaded as a one-time part of the startup process. The subsequent initialization, and then the formal execution of the program. In these languages, the initialization process must be carefully controlled,

Ensuring that the initialization of static data does not cause trouble. For example, when a static data is initialized, another static data is expected to be a valid value, which can cause problems in C + +.

Java does not have such a problem, because it uses a different load method. Because everything in Java is an object, the code for each object exists in a separate file. Unless you really need the code,

Otherwise, the file is not loaded.

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.