Valid Java notes (7)

Source: Internet
Author: User

List of related Reading Notes

No. 18 give priority to static member classes

Nested classes only serve its peripheral classes.

There are four types of nesting: static member, non-static member, anonymous member, and Department (the last three are called internal classes)

If an instance of a nested class exists independently outside the instance of its external class, the nested class should be set to a static member class (I .e: if the declared member class does not access the peripheral instance, put the static modifier in the declaration of the member class ).

Anonymous class usage:

① Create a function object;

② Creation process object;

③ Internal use of static factory methods;

④ In a complex type security Enumeration type (which requires a separate subclass for each instance), it is used in the public static final domain initializer;

Summary: four different Nested classes have their own purposes. If a nested class needs to be visible outside of a single method, or it is too long, it is not suitable to be placed inside a method, so the Member class should be used. If each instance of the member class needs a reference pointing to its peripheral instance, the member class will be made non-static; otherwise, it will be made static. Suppose a nested class belongs to the interior of a method. If you only need to create its instance in one place, if a pre-existing type can be used to describe the features of the class, the class is made into an anonymous class; otherwise, the class is made into a local class.

 

Note: The following points (No. 19 ~ No. 22) It is an alternative solution to explain the C language structure. Because I am only engaged in Java Development, I skipped this chapter. To ensure the integrity of the notes, I Will repost the article no. 19 to other places ~ Note No. 22.

 

No. 19 Replace the structure with classes
At the beginning of Java, many c programmers thought that replacing the structure with classes is too complicated and costly. However, if a Java class degrades to contain only one data domain, the structure of such a class is roughly equivalent to that of C language.
For example, the following two program snippets:
Class point <br/>{< br/> private float X; <br/> private float y; <br/>}
In fact, this code is basically no different from the structure of the C language, but this code is probably not well understood by many OO design fans, because it does not reflect the encapsulation superiority, it does not reflect the advantages of object-oriented design. When a domain is modified, you cannot take any auxiliary measures, let's take a look at the OO design code segment that includes private domains and common access methods:
Class point <br/>{< br/> private float X; <br/> private float y; <br/> Public point (float X, float y) <br/>{< br/> This. X = x; <br/> This. y = y; <br/>}< br/> public float getx () {retrun x ;}< br/> public float Gety () {return y ;} <br/> Public void setx (float X) {This. X = x ;}< br/> Public void sety (float y) {This. y = y ;}< br/>}
On the surface, this code has a lot more lines and functions than the one above, but think about it carefully. Such an OO design seems to be more user-friendly, we can extract and modify the value range without directly having a relationship with the value range. Such code is not only easy to understand, but also safe, I also learned the flexibility of object-oriented programming. Just imagine if a common class exposes its value range, it would be impossible to modify it in future versions, because the customer code of the common classes is everywhere.
It should be noted that if a class is package-level private or a private nested class, it is not inappropriate to directly expose its value range.

No. 20 replace union with class hierarchy
When we use C language for development, we often use the concept of joint, such:
Typedef struct {<br/> double length; <br/> double width; <br/>} rectangledimensions_t;
So we don't have the concept of union in Java. What should we use? Yes! Inheritance is also one of the most attractive aspects of Java. It can use better mechanisms to define delayed data types, bruce Eckel's thinking in Java also mentioned a shape-related example multiple times. We can first define an abstract class in a general sense, that is, the superclass we usually refer, each operation defines an abstract method. Its behavior depends on the tag value. If other operations do not depend on the tag value, the operation is changed to the root class (inherited class).
The most important advantage of this is that the class layer provides type security.
Secondly, the code is very clear, which is also the advantage of OO design.
In addition, it is easy to expand, and can be equally competent even for multiple aspects of work.
At last, it can reflect the essential hierarchies between these types, so as to allow more flexibility for Type checks during compilation.

No. 21 replaces the enum structure with a class
The Java programming language proposes a type-safe enumeration mode to replace the enum structure. Its basic idea is simple: Define a class to represent a single element of the enumeration type, no public constructor is provided. On the contrary, a public static final class is provided so that every constant in the enumeration type corresponds to a field.
One disadvantage of the type security Enumeration type is that it takes a certain amount of time and space to load the enumeration class and construct constant objects, this issue will not be considered in reality unless it is on devices with limited resources, such as cell phones and toaster machines.
In short, the type-safe Enumeration type is obviously better than the int type, unless an enumeration type is mainly used as a collection element, or is mainly used in an environment with very limited resources, otherwise, the type security Enumeration type is not a problem. In order to use an enumeration type environment, we should first consider the type security Enumeration type mode.

No. 22 replace function pointers with classes and interfaces
As we all know, the biggest difference between the Java language and C is that the former removes pointers, and the first time that Xiaosheng came into contact with Java, he felt very uncomfortable because he suddenly lost the pointer, the essence of C language lies in the use of its pointer, while Java has cut it down, which makes people very depressing, but over time, I gradually realized that applications using classes and interfaces can also provide the same function. We can directly define a class whose method is to execute operations on other methods, if a class only exports such a method, it is actually a pointer to the method. For example:
Class stringlengthcomprator {<br/> Public int compare (string S1, string S2) <br/>{< br/> return s1.length ()-s2.length (); <br/>}< br/>}
This class exports a method with two strings, which is a specific policy for string comparison. It is stateless and has no domain. Therefore, all instances of this class are functionally equivalent and can save unnecessary object creation costs. However, it is difficult to pass this class directly to the user, because the user cannot pass any other comparison policies. Instead, we can define an interface, that is, when designing a specific policy class, we also need to define a policy interface:
Public interface comparator {<br/> Public int compare (Object O1, object O2); <br/>}
We can define it according to our own needs.
The specific policy class usually uses Anonymous class declaration.
In Java, in order to implement the pointer mode, we declare an interface to represent this policy, and declare a class that implements this interface for each specific policy, if a specific policy is used only once, an anonymous class is usually used to declare and instantiate this specific policy class. If a policy class is used repeatedly, its Class is usually a private static member class.

 

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.