什么是真实世界的例子 - 使用私有构造函数和构造函数重载 [英] what are the real world example- use of private constructor and constructor overloading

查看:112
本文介绍了什么是真实世界的例子 - 使用私有构造函数和构造函数重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是真实世界的例子 - 使用私有构造函数和构造函数重载?

解决方案

非常简单:

私有构造函数允许你要创建一个无法从类外部实例化的类:外部世界不能这样做:

 MyClass mc =  new  MyClass(); 

创建实例的唯一方法是调用类的静态方法并让它返回实例。

这主要用于Singleton模式,其中只有一个类的实例,并且所有使用它的代码都共享。



构造函数重载允许您创建构造函数的版本,这些版本需要必须提供的特定参数。例如,你可以创建两个重载的构造函数,它们允许你传递一个字符串列表和一个字符串数组:

  public  MyClass(List< string> list)
{
...
}
public MyClass( string [] list)
{
...
}

以便您的用户可以路径并且你仍然可以正常工作。

(是的,你可能只需要创建一个构造函数来获取IEnumerable< string> - 这不是重点)


私有构造函数的最典型用法是隐藏默认的无参数构造函数。例如:

  public   class 所有者{ / *   ... * / } 

public class NotDirectlyInstantiatingObject {
// 让我们说,我们希望用户只有一种方法来实例化这个类,
// 保证将每个实例添加到某个所有者:
public static NotDirectlyInstantiatingObject创建(Onwer所有者){ / * ... * / }
// 为防止直接实例化此类,我们需要禁用d efault构造函数;方法如下:
private NotDirectlyInstantiatingObject(){} // 它存在但不能被调用
}





有很多代码的设计,其中类的直接实例化对用户是隐藏的,这种类的公共实例化方式不同,但禁用无参数默认构造函数的方式总是相同的。



要了解默认构造函数的规则,请阅读它: http://msdn.microsoft.com/en-us/library/aa645608%28v=vs.71%29.aspx [ ^ ]。



对于构造函数重载,这只是无用的术语。即使对于其他方法,我也绝不会使用令人困惑的术语重载,因为没有任何东西真正加载。让我们来看看。它实际上只是意味着语法允许创建具有相同名称的不同(正式无关)方法。编译器通过实际参数在调用点识别它们,并且只有在这种确定不明确时才会生成编译错误。现在,让我们看一下:在C#中,同一个类的所有构造函数都与此类的名称相同。这意味着没有重载或非重载构造函数;如果你愿意,那些都是超载。因此,您的问题被简化为使用不同构造函数的真实世界示例。来吧,这样的问题会很荒谬。原因太多了;即使为了班级用户的简单方便...



我希望我回答这两个问题:提供一些使用私有构造函数的例子,并解释了为什么没有需要举几个神秘的重载构造函数的例子。另外,请遵循我的建议,你可以在我对这个问题的评论中找到。



-SA


< blockquote> C#中的构造函数介绍 [ ^ ]


what are the real world example - use of private constructor and constructor overloading?

解决方案

Pretty simple:
A private constructor allows you to create a class that can't be instantiated from outside the class: the outside world can't do this:

MyClass mc = new MyClass();

The only way to create an instance is to call a static method of the class and have it return the instance.
This is mostly used for the Singleton pattern, where there is only ever a single instance of the class and it is "shared" by all code using it.

Constructor overloading allows you to create "versions" of your constructor which require specific parameters which must be supplied. For example you might create two overloaded constructors which let you pass a List of strings, and an array of strings:

public MyClass(List<string> list)
   {
   ...
   }
public MyClass(string[] list)
   {
   ...
   }

So that your user can path either and you will still work fine.
(And yes, you could probably just create a single constructor that took an IEnumerable<string> - that isn't the point)


Most typical use of a private constructor is hiding of a default parameterless constructor. For example:

public class Owner {/* ... */}

public class NotDirectlyInstantiatingObject {
    // let's say, we want to have only one method of instantiating of this class by the user,
    // to guarantee that each instance is added to some owner:
    public static NotDirectlyInstantiatingObject Create(Onwer owner) {/* ... */}
    // to prevent direct instantiation of this class, we need to disable the default constructor; here is how:
    private NotDirectlyInstantiatingObject() { } // it exists but cannot be called
}



There are very many designs of the code where the direct instantiation of the class is hidden from a user, different in the ways of public instantiations of such class, but the way of disabling of the parameterless default constructor is always the same.

For understanding the rules for default constructors, please read about it: http://msdn.microsoft.com/en-us/library/aa645608%28v=vs.71%29.aspx[^].

As to "constructor overloading", this is just the useless term. I would never use the confusing term "overloading" even for other methods, because nothing is really "loaded". Let's see. It actually simply means that the syntax allows to create different (formally unrelated) methods with the same name. A compiler recognizes them at the point of the call by actual parameters, and only if such determination is ambiguous, it generates the compilation error. Now, let's see: in C#, all constructors of the same class has the same name as the name of this class. It means that there are no "overloaded" or "non-overloaded" constructors; if you will, the are all "overloaded". So your question is reduced to the question: "what are the real world examples of using different constructors". Come on, such question would be ridiculous. Too many reasons; even for simple convenience of the class user…

I hope I answered both questions: provided some example of the use of the private constructor and explained why there is no a need to give example on those almost mythical "overloaded constructors". Also, please follow my advice you can find in my comment to the question.

—SA


An Intro to Constructors in C#[^]


这篇关于什么是真实世界的例子 - 使用私有构造函数和构造函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆