私人建筑师的问题 [英] Question on Private Constructor

查看:68
本文介绍了私人建筑师的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PRIVATE构造函数是在.net中使用还是不在?

如果是,那么在哪种情况下我们使用PRIVATE Con​​structor?

给我举一个例子?

解决方案

私有构造函数偶尔会在.Net中使用。

An例如,可以生成一个单例类 - 一个只有实例的类。



例如



 A 
$ {
静态A o;
private A()
{
}

public static A getinstance()
{
if(o == null)
{
return o = new A();
}
其他
{
返回o;
}
}
}


私有构造函数用于禁用使用构造函数构造类。当然,它对于非静态类是有意义的。让我们留出静态构造函数:它们根本不允许访问修饰符,并且始终被视为私有。



这就是为什么:如果没有定义构造函数,一个默认的隐式定义构造函数仍然允许实例化这个类。当然,这个构造函数是无参数的。因此,定义私有无参数构造函数使得构造类的实例成为不可能。其他私人建筑师也可以加入。



现在,为什么?显然,如果一个对象不能在类之外构造,它可以在里面构造。因此,所有这些活动的目的实际上是创建一个工厂方法,它返回同一个类的实例化实例。显然,即使所有构造函数都是私有的,也可以执行内部构造。



为什么不是通常的方式,非私有构造函数?由于代码设计者的作者可能会想到不同的原因,但其中一个原因是必不可少的:因为这样的工厂方法可能会返回一些派生类的实例。因此,编译时返回类型属于同一个类(因此也可以是 abstract ),但返回的运行时类型是派生的(当然,从不抽象) )。



举个好例子,看看这个FCL类型: System.Net.WebRequest 。它根本没有构造函数,但它有一个工厂方法 Create 。这个怎么运作?它没有公共或内部构造函数(对不起,不是一个完美的例子:构造函数不是私有的,但是获取一个想法仍然很有趣)。相反,您使用工厂方法。考虑工厂方法 Create(string)是最有趣的。它应该是一个URI字符串,实际的运行时类型由URI scheme 确定( http://en.wikipedia.org/wiki/URI_scheme [ ^ ])。根据检测到的方案,构造并返回一个派生类型: HttpWebRequest HttpFtpRequest 等等。 />


请参阅:

http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx [ ^ ]。



-SA


私有构造函数 [ ^ ]

PRIVATE Constructor is use in .net or Not?
if YES then in Which Situation we use PRIVATE Constructor?
give me one example?

解决方案

A private constructor can occasionally be used in .Net.
An example could be to generate a singleton class - a class which has only instance.

For e.g.

class A
{
  static A o;
  private A()
  {
  }

  public static A getinstance()
  {
    if (o == null) 
    { 
       return o = new A();
    }
    else
    {
       return o;
    }
  }
}


Private constructor is used to disable construction of a class using a constructor. It makes sense for a non-static class, of course. Let''s set aside static constructors: they do not allow access modifiers at all, and are always treated as private.

Here is why: if no constructors defined, one default implicitly defined constructor still allows to instantiate this class. Naturally, this constructor is parameterless. So, defining a private parameterless constructor makes it impossible to construct an instance of a class. Other private constructors can be also added.

Now, why? Apparently, if an object cannot be constructed outside of the class, it can be constructed inside. So the purpose of all this activity is actually to create a factory method which returns the instantiated instance of the same class. Apparently, even though all constructors are private, inside class construction can be performed.

Why not a usual way, with non-private constructors? By different reasons an author of code designer may think of, but one of the is essential: because such factory method might return instances of some derived classes. So, compile-time return type is of the same class (which therefore can also be abstract), but run-time types returned are derived (and, of course, never abstract).

For a good example, look at this FCL type: System.Net.WebRequest. It has no constructors at all, but it has a factory method Create. How it works? It does not have public or internal constructor (sorry, not a perfect example: the constructors are not private, but it''s still interesting to get an idea). Instead, you use factory methods. It''s most interesting to consider the factory method Create(string). It is supposed to be a URI string, and the actual run-time type is determined by the URI scheme (http://en.wikipedia.org/wiki/URI_scheme[^]). Depending of the scheme detected, one of the derived types is constructed and returned: HttpWebRequest, HttpFtpRequest, etc.

Please see:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^].

—SA


look Private constructor[^]


这篇关于私人建筑师的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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