[Learning Notes] Thinking In Java (Third Edition) Chapter 6 Reusing Classes (Reuse Class)

xiaoxiao2021-03-06  43

Class multiplexing: 1 In the new class, create an old object directly. This method is called synthetic. 2 Create a new, with the original class belongs to a type. You accept The old class form, there is no modification of it

The new code is added to the inside. This method is inheritance.

If you want to initialize Reference, you can perform: 1 When defining objects. This means that you have initiated it before constructor. 2 In this class constructor. 3 Before I use that object.

Inherited syntax inheritance is the most important concept of Java. In fact, when you create a class, you are inheriting. Either to inherit other classes, either implied

Inherited the standard Java root, Object. Inherit uses the extends keyword.

Inherit is not just the interface of the base class. When you create a derived class object, there is a sub-object of a base class in this object.

(SubObject). Construction behavior is developed from the base class "outward", so the base class will initialize it before the derived constructor is accessed. That is

The compiler will force you to put the call of the base class constructor in the most in front of the constructor of the derived class. If the class does not have the default constructor, or

The base class constructor you call is a parameter, you must use the super keyword and the appropriate parameters to express the constructor of the base class.

.

Make sure that proper cleanup If you want to clean it properly, you must write a cleaning method, don't use Finalize (). The order in which you execute is the opposite of the order of the creation object.

Name Hidding) A class inherits another class, and one of its methods also overloads another method. The method of the base class in this class is still valid. 即 base class

Methods are also available. Derived class is a base class, which may have some basic classes. Therefore, "upcasting" is always safe.

All. "Downcasting" may have problems.

One of the advantages of inheritance is to support progressive development (INCREMENTAL Develop). When adding new code, it will bring BUG to the old code.

Inheriting although it is important, do not abuse.

It's important to realize That Program Development is An Incremel Process, Just Like

Human learning. You can do as much analysis as you want, but you still Won't know all the

Answers when you set out on a project. You'll Have Much More Success-and more immediate

Feedback-if You Start Out to "Grow" your project as an in Organic, Evolutionary Creature,

Rather Than Constructioning It All at Once Like a Glass-Box Skyscraper.

Final keyword

Java's final keyword is also different depending on the context. But in general, it means that "this thing is not allowed to change

"The three uses of .final: Data (DATA), Method (Method), class (Class).

Final data

Constant: constant is used in the following two cases: 1 can be "Compile-Time Constant", so no longer change. 2 can also be the initial value of the initial initial time, this You don't want to change it later.

This constant in Java must be Primitive, and use Final keywords. This constant is assigned to be defined.

One is static and final data members will only occupy a memory, and it is not modified. When Final does not refer to Primitive, it is used

When the object's Reference is. Final means that this reference is a constant. When initialization, one will replace the Reference to a certain

Object, then it can't point to other objects. Blank factory data (BLANK FINAL), that is to say, but not to the initialization value. You must initialize, then use blank data members And the compiler will force you to do this.

Final parameters: You can read, but you can't change the parameters.

Final method: The purpose is 2: First, the sect is prohibited from being modified. Second, it is efficiency. If the method is final, then the compiler will convert the call to

Inline. However, it is best not to leave the efficiency problem to the compiler and JVM destination.

The private method implies Final meaning, you can add a final modifier to a method, but do not do this meaning.

The method is private, then it does not belong to the interface of the base class. It can only be hidden by the class. If you create the same name in the derived class

Public, protected or package methods, they have no contact. You didn't overwrite that method.

Final Class: That's not to inherit this class, you will not inherit this class, but you don't allow others to inherit this class. But this is not

meaningful.

Loading: In a more traditional language, the program is launched once, then load all things, then initialize, then start execution. In Java, each class is saved in it since Java. In the file. When you need it, this class will not be loaded.

转载请注明原文地址:https://www.9cbs.com/read-69517.html

New Post(0)