C # basic (5) -- Role of private constructor in the class,
If a class member has a private modifier, the class member cannot be accessed outside the class scope. When the class constructor applies the private modifier, the external class is prohibited from creating instances of this class.Although it seems a little hard to understand (since it cannot be instantiated, what is the use of this class ?), But in fact this is an extremely powerful feature.
The most obvious thing is,If the class only provides functions through static methods and fields, private constructor is often used.The System. Math class in the framework class library FCL is a classic example.
The System. Math class has two static fields: pi and e (natural logarithm base number), and some methods to return trigonometric function values. These methods are used as built-in functions. Therefore, the program does not need to create Math class instances to use these fields and methods.
Now it is natural that the following problem occurs: Do you want to avoid instantiation, use private constructor, or use abstract classes better? The answer is to understand the differences between the two.First, consider inheritance. Although the abstract class cannot be instantiated, its real purpose is to be used as a base class so that a derived class (instantiated) can create its own implementation. Classes that use private constructor are not inherited or inherited. Second, the private constructor can only prohibit external classes from instantiating the class, but cannot prohibit the creation of instances within the class.
The features of private constructor can also be used to manage object creation. Although the private constructor does not allow external methods to instantiate this class, it allows public methods (sometimes known as factory methods) in this class to create objects. That is to say, the class can create its own instance, control external access to it, and control the number of created instances.
Note: This article is from 51CTO. COM.
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].