[Learning Notes] Thinking in Java (The 2nd Edition) Study Note (2)

xiaoxiao2021-03-06  39

Chapter 4 Initialization and Clear "With the progress of the computer, the program design of 'unsafe' has become one of the culprits that have a high pricing cost."

"Initialization" and "Clear" are two of these security issues.

4.1 Constructor

<1> You cannot distinguish between different functions, such as: Void f () {} int f () {} We may call a method, ignore the return value; usually call this "to" call it A method, because we care about the return value, but other effects that are called. Therefore, if we call the method below: f (); how Java determines the specific call mode of F ()? And how do others identify and understand the code? Due to the problem of this class, it is not possible to distinguish the method according to the return value type.

<2> This line below: New bird (); its role is to create a new object and call the default builder - even if it is not clearly defined a builder like this. If there is no, there is no way to call, and our object cannot be constructed. However, if a builder has been defined (whether or not there is a variety of variables), the compiler will not help us automatically synthesize:

Class Bush {Bush (INT I) {} Bush (Double D) {}}

Now, if you use the following code: New bush (); Compiler will report yourself not to find a constructed builder. Just as we have no builder, the compiler will say: "You seem to need a builder, so let us create one." But if we wrote a builder, the compiler will say: "Ah, you have written a builder, so I know what you want; if you don't place a default, it is because you intend to omit it."

4.2 Garbage collection

One of the most useful places of Finalize () is to observe the process of garbage collection. The following example shows you the process of garbage collection, and summarized the previous statement.

Garbage.java// Demonstration of the Garbage // Collector and Finalization

Class chair {static boolean gtrun = false; static boolean f = false; static int created = 0; static int finalized = 0; INT i; chair () {i = created; if (created == 47) System.out .println ("created 47");} protected void finalize () {if (! gcrun) {gcrun = true; system.out.println ("beginning to finalize after" create "chairs have been create);} IF (i == 47) {system.out.println ("Finalizing Chair # 47," "Setting Flag to Stop Chair Creation); f = true;} finalized ; if (Finalized> = CREATED) System.out. Println ("all" finalized "finalized");}} public class garbage {public static void main (string [] args) {if (args.length == 0) {system.err.println ("usage: / N " " Java Garbage Before / N or: / N " " Java Garbage After "); return;} while (! chair.f) {new color (); new string (" to take up space ");} System.out.println ("After All Chairs Have Been Created: / N " " Total created = " chair.created ", Total Finalized = " chair.finalized); if (args [0] .Equals (" before ") {system.out. Println ("GC ():"); system.gc (); system.out.println ("Runfinalization ():"); system.Runfinalization ();} system.out.println ("bye!"); if! (Args [0]. Equals ("after"))) System.RunfinalizersOnexit (TRUE);}} / ///: ~

4.3 Object arrays Integer [] b = new integer [] {new integer (1), new integer (2), new integer (3), // final "," is the existing}; Chapter 5 Hide Implementation process

"When performing object-oriented design, a basic consideration is: how to change the change in things that remain unchanged."

Chapter 6 Regeneration

"The characteristic of Java is a reuse or regeneration of code. But the most revolutionary meaning is that more other things we can do except for the copy and modification of the code."

In this chapter, we will introduce two ways to achieve this goal [ie code multiplex]. The first simplest: simply creates the original class object in the new class. We call this method as "synthesis" because the new class is merged by an existing class. We only simply repeat the functionality of the code, not the form of it. The second approach looks slightly some tips. It creates a new class that as a "type" of the existing class. We can take the existing form of existing classes and join the new code and will not affect the existing classes. This magic behavior is called "inheritance".

6.1 Inheritance: Initialization of Basic Type //: Cartoon.java // Constructor Calls During Inheritance

Class Art {Art () {system.out.println ("art constructor");}}

Class Drawing Extends Art {Drawing () {system.out.println ("Drawing Construction");}}

Public class cartoon extends Drawing {cartoon () {system.out.println ("cartoon constructor");} public static void main (string [] args) {cartoon x = new cartoon ();}} ///: ~

The output of the program shows automatic calls: Art ConstructOrdrawing ConstructionorCartoon Constructor This shows that when the subclass is called, the constructor of the parent class is called once.

- Builder containing an argument - the above example has its own default builder; that is, they do not contain any arguments. The compiler can easily call them, because there is no problem with which the self-variable is not present. If the class does not have the default argument, or if you want to call a certain basic class builder with an argument, you must clearly write a call code for the base class. This is implemented using the super key and the appropriate argument list:

Class Art {Art (INT I) {System.out.Println ("Art Constructor");}}

Class Drawing Extends Art {Drawing () {Super (1); System.Out.println ("Drawing Construction");}}

Public class cartoon extends Drawing {cartoon () {super (11); // Missing the sentence, it will be compiled, that is, you must explicitly provide the construction method of the base class, unless the base class provides the default non-refined constructor System.out.println ("cartoon constructor");} public static void main (string [] args) {cartoon x = new cartoon ();}} ///:~6.2 Accumulation development Everyone should remember such a focus: Program development is a process of increasing or accumulating, just like people learning knowledge. Of course, as much analysis can be performed as required, but in the beginning of a project, no one can know all the answers in advance. If you can think of your project as an organic, you can constantly develop and improve it, it is expected to achieve greater success and more direct feedback.

6.3 Final <1> .Final data For basic data types, Final will turn the value into a constant; but for the object handle, Final will turn the handle into a constant. When making a declaration, you must initialize the handle to a specific object. And never turn the handle to another object. However, the object itself can be modified. Java does not provide any means to this, you can turn a object directly into a constant (however, we can write a class yourself, so that the object has a "constant" effect). This limitation is also suitable for arrays, which also belongs to objects.

<2>. Blank Final now forced us to assign value for Final - either use an expression when defining a field or in each builder. This ensures that the final field gets correct initialization before use.

<3>. The use of Final methods can be used for the consideration of two reasons. The first is to "lock" on the method to prevent any inheritance classes from changing its original meaning. This approach can be taken if the behavior of a method is remained unchanged during the inheritance and cannot be overwritten or overwritten. The second reason to adopt the FINAL method is the efficiency of the program execution. After setting a method into Final, the compiler can put all the calls to that method into the "embedded" call.

<4> .final class If the entire class is Final (in its definition crown with final keyword), you don't want to inherit from this class, or do not allow anyone else to take this action. In other words, for such a reason, our class definitely does not need to make any changes; or for security, we do not want subclasses (subclass processing). In addition, we may also take into account the issue of efficiency, and want to ensure that all actions involving this object should be effective as possible.

Chapter 7 Multi-Image Design Language for object-oriented programming is the third most basic feature (the first two are data abstraction and inheritance). "

Trace shape can be simply manifested by using this statement:

Shape s = new circle ();

Here, we created a Circle object and assigned the result handle to a shape. This surface seems to be an error operation (assigning a type to another), but it is actually feasible - because in inheritance relationship, Circle belongs to the Shape. Therefore, the compiler recognizes the above statement and will not prompt us to prompt an error message. When we call one of the basic types (already over-derived class): s.draw (); Similarly, everyone may think that Shape's DRAW (), because this is a shape handle. So how can the compiler know what else should be other? But at this time, Circle.draw () is Circle.Draw () because the later binding has been intervened (versatile). 7.1 Override and Types of the Overload Overlay Finger Subclass Directive to the parent class, but not overwritten

If you inherit from an abstract class, and you want to generate an object of a new type, you must provide a method to define all abstract methods in the underlying class. If you don't do this (you can choose not to do it), the derivative class will also be abstract, and the compiler will force us to use the Abstract keyword flag of "abstract" essentity. Even if there is no ABSTRACT method, a class can be declared into "abstract classes".

7.2 Interface "Interface" keyword makes abstracted concepts deep into a layer. We can imagine it as a "pure" abstraction class. It allows the creator to specify a basic form of a class: method name, argument list, and return type, but does not specify the method main body. The interface also includes data members of the basic data type, but they are default to static and final. The interface only provides a form without providing details. The interface describes yourself: "For all the classes of my class, it should look like I now." Therefore, all code to use a particular interface know what method may be called for that interface. This is the all meaning of the interface. So we often use interfaces to build a "agreement" between classes and classes.

7.3 Constant Packets Automatically have STATIC and FINAL properties due to all fields placed in an interface, so the interface is a good tool for packeting constant values, which has very similar effects with C or C Enum. As shown in the following example:

7.4 Internal classes and tracehes so far, the internal classes seem to have no special place. After all, use it to hide a small question. Java already has a very good hidden mechanism - only allows classes to be "friendly" (visible only in a package), not to create an internal class.

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

New Post(0)