Features and uses of private constructor
This article describes the characteristics of private constructor and the circumstances in which private constructor is used.
□Classes with private constructors cannot be inherited
Declare a private constructor in the Animal class and let the Dog class inherit the Animal class.
public class Animal
{
private Animal()
{
Console.WriteLine("i am animal");
}
}
public class Dog : Animal
{
}
Generate a solution and report the following error:
□Classes with private constructors cannot be instantiated
class Program
{
static void Main(string[] args)
{
Animal animal = new Animal();
}
}
public class Animal
{
private Animal()
{
Console.WriteLine("i am animal");
}
}
Generate a solution and report the following error:
□Application of private constructor
Sometimes, we do not want a class to be instantiated too much, such as global classes and routing classes. At this time, we can set constructors for classes and provide static methods.
class Program
{
static void Main(string[] args)
{
string str = Animal.GetMsg();
Console.WriteLine(str);
Console.ReadKey();
}
}
public class Animal
{
private Animal()
{
Console.WriteLine("i am animal");
}
public static string GetMsg()
{
return "Hello World";
}
}
Summary: Once a class is set as a private constructor, it cannot be inherited or instantiated. In this case, a static method is usually provided for the class for calling.
C # usage of private constructor
A private constructor is a special instance constructor. It is usually used in a class that only contains static members. If a class has one or more private constructors without a public constructor, other classes (except Nested classes) are not allowed to create instances of the class. For example:
Class NLog
{
// Private Constructor:
Private NLog (){}
Public static double e = System. Math. E; // 2. 71828...
}
Declare an empty constructor to Prevent Automatic Generation of default constructor. Note: if you do not use an access modifier for the constructor, it is still a private constructor by default. However, the private modifier is explicitly used to explicitly indicate that the class cannot be instantiated.
When there are no instance fields or instance methods (such as the Math class) or when a method is called to obtain an instance of the class, the private constructor can be used to block the creation of the instance of the class. If all the methods in the class are static, consider making the entire class static. For more information, see static and static class members.
Example
The following is an example of a class using a private constructor.
Public class Counter
{
Private Counter (){}
Public static int currentCount;
Public static int IncrementCount ()
{
Return ++ currentCount;
}
}
Class TestCounter
{
Static void Main ()
{
// If you uncomment the following statement, it will generate
// An error because the constructor is inaccessible:
// Counter aCounter = new Counter (); // Error
Counter. currentCount = 100;
Counter. IncrementCount ();
System. Console. WriteLine ("New count: {0}", Counter. currentCount );
}
}
Output
New count: 101
Note: If you uncomment the following statement in this example, it generates an error because the constructor is not accessible due to its protection level:
// Counter aCounter = new Counter (); // Error
================================
If you are not tired, please refer to this document or check the MSDN
Function of constructor in C #
Commonalities:
All objects are instantiated and the data is initialized.
The default constructor means that all classes inherit the constructor of the null parameter from the ancestor object. You do not write or write the null parameter constructor, the construction of parameters is generally self-written, so there is no write. The function of parameters is the same as that of null parameters, but it can contain parameters. Here is an example to illustrate it.
There is a class Monitor which has the property String height; String
Width
Public Monitor (){}
Public Monitor (String height, String width ){}
There is a construction method with parameters for an empty parameter.
In the main method, I perform initialization.
Monitor monitor = new Monitor ();
This structure only creates the Display object, and its attributes have no value. You can also manually assign a value to it.
For example, monitor. height = 100;
Monitor. width = 200;
There are a lot of things to do with parameters.
Monitor monitor2 = new Monitor ("100", "200 ");
Here, I just need to put the parameters that I want to put in directly to assign values to my attributes. Does it save a lot of trouble?
This is its advantage!
The answer is complete!