15 design tools

In the design class, the method may be configured using a private static methods and members of the class to design a tool. An example is given below:

The first is the case in accordance with conventional design category:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class   {     public () {                                          // unused private keyword     }




// member methods public int the Add ( int A, int B) { // No static keyword return A + B; } }





class MyMathDemo {
public static void main(String[] args) {
int a = 4;
int b = 3;
System.out.println("a + b = " + MyMath.add(a, b)); //ERROR
System.out.println("---------------");
MyMath myMath = new MyMath();
System.out.println("a + b = " + myMath.add(a, b)); //OK
}
}

The above code defines a simple mathematical class (MyMath) and a simple math test class category (MyMathDemo). In no instance field MyMath class. Test method MyMath class add (int, int) in MyMathDemo class.

According to conventional design class, you can not direct way to access class by class name, the object must first create a class and then the class object access method through. Because there is no instance fields in this class, only the method, it did not make any sense to create an object, but also take up memory.

In this case, the compiler will complain MyMath.add encountered. Encounter myMath.add compile without error.


Improved version

For this "problem" can be effectively prevented by the static keyword. Now MyMath method to the class with the keyword static:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class   {     public () {                                          // unused private keyword     }




// member methods public static int the Add ( int A, int B) { // static keyword return A + B; } }





class MyMathDemo {
public static void main(String[] args) {
int a = 4;
int b = 3;
System.out.println("a + b = " + MyMath.add(a, b)); //OK
System.out.println("---------------");
MyMath myMath = new MyMath();
System.out.println("a + b = " + myMath.add(a, b)); //OK
}
}

When the method uses the static keyword, it becomes a static method. At this time, this method can be called directly by the class name.

In this case, the compiler will not encounter MyMath.add error. MyMath.add compiler will not encounter an error.


final version

Although the above through the effective realization of the static keyword no longer need to create an object directly through the class name will be able to access this method. But it also poses a problem that the program did not create the object of shielding the entrance, in the above code, we can still create an object MyMath class and then access MyMath class methods through the object. At this time, we can add keywords to private constructor to create an object of shielding the entrance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class   {     private MyMath () {                                         // use private keyword     }




// member methods public static int the Add ( int A, int B) { return A + B; } }





class MyMathDemo {
public static void main(String[] args) {
int a = 4;
int b = 3;
System.out.println("a + b = " + MyMath.add(a, b)); //OK
System.out.println("---------------");
MyMath myMath = new MyMath(); //ERROR
System.out.println("a + b = " + myMath.add(a, b));
}
}

When using a private key can only be accessed by methods of its own class, the class can not be any other way to access. Here MyMath to the constructor of the class MyMath () after adding private key, you can not create an object MyMath class by class constructor MyMath MyMath () in MyMathDemo class.

In this case, the compiler will not encounter MyMath.add error. Encountered MyMath () compiler error.

At this point, we use the constructor private static methods and members of simple implements a utility class.

Original: Big Box  15 tools designed


Guess you like

Origin www.cnblogs.com/chinatrump/p/11584795.html