"Java Programming Thought" (Second Edition) No. 6: Repeated Application Classes

xiaoxiao2021-03-06  15

My notes

Java has a sense of praise, and the repetition of program code is one of them. However, if you want to get a revolutionary change, you have to be far beyond the "copy program code, then change" the reuse of the old program code. : Composition and inheritance (inheritance)

TOSTRING (): Each non-basic category is available for TOString (), and when the compiler wants to go to a string, this function will be evident in the case of the object.

Test Tips: "Provide main () for each class, regardless of the Class is not public", you can start each main () in the command line. This makes each Class unit test easier. And after the unit test, no need Delete main (); you can leave it to add test after you.

Super Key: You may be in the Derived Class function (such as A ()) to call a function in the base class (usually a ()), you certainly can't call directly because the same marker will Causes recursive. In order to solve this problem, it provides a keyword super. He represents superclass, so you can call: super.a ();

Initialization of Base Class: Call the Base Class constructor. The Java compiler will automatically insert a call action for the Base Class default constructor in the Derived Class constructor. But if your class does not have a default constructor, or you want to call the base class constructor with the quoter, you have to use the keyword super, and match the appropriate quotation column, understand the write call action, such as Super ( i); (Note that this is different from the syntax of the general member function). In addition, the call to the Base Class constructor must be the first thing made by the Derived Class constructor (if you are wrong, the compiler will remind you).

The exception to capture the base constructor: The "first thing" limit on the above also makes the DeriveD Class constructor unable to capture all from the base class. Sometimes this is quite convenient.

Keep an appropriate cleanup: Since there is no destructive function in Java, sometimes we want to make some cleaning action when the object is destroyed, this time you have to put the code in the Final block.

Name Hiding: Function Whether it is defined with a layer of Class or its base Class, the Java overloading mechanism can operate normally. Of course, use the "Signature" and return category "and return category" to copy the "Override", which makes the basis of the polymorphism. Keyword final: Basic meaning "This is not changed". We may want to prevent changes based on design and efficiency. These two reasons are very different, which may cause misuse of keyword final. You can use the three places of Final: Data, Method, Class.

Final Data: The data used to tell the compiler is "fixed". The use of the following: Compile-Time Constant "is used. 2. Can be executable (Run-Time ) Is initialized, but you don't want to change it. If a certain amount of data is Static, Static is Final, they have a memory space that cannot be changed. Blank Finals: Java allows "White Finals", that is, allow us to declare data members to final simultaneously without initialization, BLANK Finals must initialize before use, and the compiler will force the guarantee. However, Blank Finals provides more flexibility than Final. Because the final data member in this class can be different in each object. Final Arguments: Stained the quotes as Final, means that you cannot make this quotes (a reason) in this function. When the basic type quotes declare to FINAL, it means that you can read the value represented by the number, but cannot change this value. Final Method: It is two: 1. Lock this function to ensure that it is not written by DeriveD Class (Override), this is a design consideration. 2. Allow the compiler (but this action is not necessarily generated, the compiler will have a judgment action at this time) to convert all call operations for this function to Inline. This is a consideration of efficiency. Since the above two points will work, all sometimes you must weigh the value is not worth it in Final Method: For example, a function is small. You want to convert it to the inline function, but it will completely lose the possibility of Override Sex. So it is best to make him stunned as Final when the volume of a function town or when you really don't want him to be replied. All private functions in the class will naturally be Final because they cannot be used by others, and they cannot be replied. This shows a very important nature: overriding can only occur when "function belongs to the base class interface".

Final Classes: The reason is that you don't want it to be inherited (either you are confidential, either you are thinking about design) Please note that whether the Class is Final, the data member DATA can be or final, or not Final. All functions in Final Class are naturally final because they can't be overwritten.

When is the combination inheritance? Just think about this question: Do I need to transform up?

Inheritment and initialization and Class loading:

The Class program code is loaded when it is initially used. The so-called initial use, not only when its first object is constructed, or it is possible to be taken when a Static data member or a three tower inscription function is loaded. Example of P216

my question

When there is protected f () in the base class, I define public f () or private f () in the Derived Class, is OverLoading or Overriding?

C inherit from B, B inheritance from A, I want to call a function A () in a function in C, how to write?

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

New Post(0)