Private constructor and public parameter constructor

不打扰是莪最后的温柔 提交于 2019-12-12 08:43:09

问题


I heard that a private constructor prevents object creation from the outside world.

When I have a code

public class Product
{
   public string Name { get;set;}
   public double Price {get;set;}
   Product()
   {
   }

   public Product(string _name,double _price)
   {
   }
}

Here I still can declare a public constructor (parameter), won't it spoil the purpose of the private constructor? When do we need both private and public constructor (parameter) in code?

I need a detailed explanation please.


回答1:


The reason you would use the pattern you're describing is when you want to control how the object is instantiated.

In your example, for instance, you're saying the only way to create a Product is by specifying its name and price. This is with respect to the outside world, of course. You could also do something similar using other access modifiers, and it would have different implications, but it all boils down to controlling how you want the objects instantiated with respect to who will be doing it.

If you wanted to prevent object creation altogether you would have to make all your constructors private (or protected). That would force the object to be created from within itself (or an inherited class).

Also, as Matti pointed out in the comment below, when you define a constructor that is parameterized you don't need to specify a private default constructor. At that point it is implied.




回答2:


Constructors can be chained together, to avoid having to duplicate code, so it's quite common to have private constructors that nobody is supposed to call outside of the class, but that each public constructor then just chains to.

Example:

public class Test
{
    private Test(int? a,string b) { }
    public Test(int a) : this(a, null) { }
    public Test(string b) : this(null, b) { }
}

Here there is two public constructors, one taking a string, and one taking an int, and they both chain to the common private constructor that takes both arguments.

Also, of course, you can constructs new objects from within the same class by using the private constructor, for instance you might want specialized constructors only available through static factory methods:

public static Test Create()
{
    int? a = ReadConfigurationForA();
    string b = ReadConfigurationForB();
    return new Test(a, b);
}

Here it might not be a good idea to expose the private constructor to the outside world, but instead add a static factory method that fetches the correct arguments to pass on the constructor.




回答3:


You need a private constructor when you only want that constructor to be called from within the class itself. In your example you are forcing the calling object to provide 2 parameters when creating the object.

With a private constructor you could do something like:

public static GetInstance ()
{
   return new YourObject();
}

but nothing else except the object could call the parameterless constructor.

It's commonly used to create a singleton pattern:

http://www.dofactory.com/Patterns/PatternSingleton.aspx




回答4:


You would use a constructor with parameters when you wanted to force calling code to pass a value to the constructor in order to create an instance of your class. In your example, calling code must use the parameter version of the constructor in order to create a Product.




回答5:


A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.

For more details refer to this: http://msdn.microsoft.com/en-us/library/kcfb85a6(VS.80).aspx



来源:https://stackoverflow.com/questions/2640191/private-constructor-and-public-parameter-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!