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 Construction Function <1> You cannot use the return value to distinguish between different functions, such as: Void f () {} int f () {} We may call a method, ignore the return value; usually refer to this "for it Side effects to call 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 there is a self-variable), the compiler will not help us automatically synthesize: Class Bush {Bush (INT i) {} Bush (Double D) {}} Now, if used The following code: New bush (); Compiler will report yourself less than 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, you are going to save it." 4.2 Waste Collection Finalize () One of the most useful places is to observe garbage The process of collecting it. 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 gcrun = false; static boolean f = false; static int created = 0; static int finalized = 0; int i; Chair () {I = created; if (create == 47) System.out.println ("create 47");} protected void finalize () {if (! Gcrun) {gcrun = true; system.out.println (" Beginning to Finalize After " Created " Chairs Have Been Created ");} IF (i == 47) {System.out.Println (" Finalizing Chair # 47, " " Setting Flag to Stop Chair Creation); f = True;} finalized ; if (femalized> = 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 chair (); New String ("To Take Up Space");} System.out.Println ("After All Chairl Have Been Created: / N" "Total Created =" Chair.created ", Total Finalized =" Chair.Finalized ); If (args [0]. Equals ("Before")) {system.out.println ("GC ():"); system.gc (); system.unt.println ("Runfinalization ():") System.RunFinalization ();} system.out.println ("bye!"); If (args [0] .Equals ("after")) System.RunfinalizersoneXIT (TRUE);
}}} ///: ~ 4.3 Object Array Writing Integer [] B = new integer [] {new integer (1), new integer (2), new integer (3), // final "," is existing }; Chapter 5 Hide implementation process "When the object-oriented design is: how to separate the changes in things with the remaining things." Chapter 6 Regeneration "Java A focus is a reuse or regeneration of the code. But the most revolutionary meaning is that except for the copy and modification of the code, we can do more things. "In this chapter, we will introduce Two methods of achieving this goal [ie code multiplexed]. 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 the basic class //: Cartoon.java // constructor calls during inheritance class art {art () {system.out.println ("art constructor");}}}}} class drawing extends art {Drawing () {system .out.println ("Drawing Constructor");}} 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 call: Art ConstructRuctor This shows that when the subclass is called, the parent class constructor is called once - containing the argument Builder - 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 with Super Keywords 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); // Lack of this sentence, it is 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-Division "For object-oriented programming languages, multiplexity is the third most basic feature (the first two are data abstraction and inheritance).
"Trace the shape can be simply expressed in the following statement: Shape S = New circle (); here, we created a Circle object and immediately assigned the result handle to a shape. This surface looks like a wrong operation (will be Type assignments to the other), but actually feasible - because of the inheritance relationship, Circle belongs to the Shape. Therefore, the compiler recognizes the above statement, and will not prompt us to prompt a mistake message. When we call one of the foundations When the class method (already covered in the derivative class): s.draw (); Similarly, everyone may think that the shape Draw (), because this is a shape handle. So how can the compiler know what other What is it? But at this time, Circle.draw () is Circle.Draw () because the later binding has been intervened (versatile). 7.1 Override the parameter name and type of the overload coverage finger class and the corresponding overload of the parent class Refers to the method overload, but no overlay is inherited from an abstract class, and wants to generate an object of the new type, you must provide a method for all abstract methods in the underlying class. If you don't do this (you can choose not to do), The derivative class will also be abstract, and the compiler will force us to use the Abstract keyword logo. Even if any Abstract method is not included, you can declare a class "abstraction class". 7.2 interface " Interface "(interface) keyword makes abstract concepts deep into a layer. We can imagine it as a" pure "abstract class. It allows creators to specify a basic form of a class: method name, argument list, and return type However, there is no way the method. The interface also contains the basic data type data member, but they are defaulting to static and final. The interface only provides a form of implementation. The interface describes yourself: "For me All classes look like I am like this. "So, all code to use a specific interface knows what method can be called for that interface. This is the all meaning of the interface. So we often use the interface A "protocol" 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 packet constant values, which has C or The ENUM of C is very similar. As shown in the following example: 7.4 Internal classes and traceable streads so far, the internal class looks still nothing special. After all, use it to achieve hidden seemingly small. Java already has a very Excellent hidden mechanism - only the class is "friendly" (visible only in one package), instead of creating it into an internal class. Small knot "versatile" means "different forms". In the object-oriented object In the programming, we have the same Appearance (the general interface of the base class) and different forms of using that appearance: dynamically bind or organize, different versions of the way. Through this chapter, everyone knows that if you don't use data abstraction and inheritance technology, you can't understand, even an example of creating versions. Parallelism is an indispended feature (just like a Switch statement), only coordinating with other elements. We should look at it as part of the overall relationship. It is often confused from other Java, non-object-oriented features, such as methods overload, and these features sometimes have certain features targeted. But don't be fooled: If there is no binding in the future, it is not a conformal. To use versatile or even object-oriented technologies, especially in their own procedures, they must extend their programming views to members and messages including alone, but also consistency between classes and classes. Relationship.