在比较静态方法时,构造函数的优点是什么 [英] What is advanteges of constructors in comparing of static methods

查看:118
本文介绍了在比较静态方法时,构造函数的优点是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起家伙们提出愚蠢的问题。我没有说明构造函数只在程序中调用一次,但是当我应该使用构造函数时,我无法理解。

解决方案

< blockquote>你确定这是一个有效的回答问题吗?这是理解事物的错误方法。



您不需要寻找福利。相反,您需要1)了解一个或另一个语言和平台功能究竟是做什么的; 2)分析你需要达到的目标; 3)根据之前的项目,选择适合你的语言表达特征。



甚至不要试图考虑好处。您的最终决定应该提供好处。语言功能没有好处,它们都是必需的(当然是用一种好的语言)。这一切都取决于目的。



两种方法的比较根本没有意义。但您可以比较两个或更多用例。让我们考虑一下。

  //  案例1: 
class MyClass {
internal static MyClass Create( string initializationParameter){
return new MyClass( string
}
private MyClass( string initializationParameter){ / * ... * / }
}

// vs simple
class MyClass {
internal MyClass( string initializationParameter){ / * ... * / }
}

在这里,你可以用不同的方式实例化 MyClass ,而且很明显第一种方式是过度而且没有意义。但是有些情况下第一种方法(我们可以称之为工厂方法很有意义)。



考虑一个用例来自.NET FCL,类 System.Net.WebRequest

http://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29 .aspx [ ^ ]。



让我们理解为什么不通过构造函数创建此类的实例,而是通过其工厂方法创建?这两个:

http://msdn.microsoft.com/en-us/library/system.net.webrequest.create%28v=vs.110%29.aspx [ ^ ]。



此类是抽象。因此,没有它的实例,您只能调用静态成员,包括工厂方法。这些方法将返回此类的实例,但此实例的运行时类型不能是抽象类。这些实例的运行时类型将是一些派生的非抽象类型。要实例化的实际类型取决于表示URL的初始化参数。根据URL方案(http://,htts://,ftp://),工厂方法选择了适当的派生实现类。这种机制利用真正的OOP,基于虚拟方法的后期绑定。



-SA


Inimicos写道:

构造函数只在程序中调用一次





对于静态构造函数,而不是实例构造函数 - 你要问的是什么呢?


构造函数初始化对象:它们放置对象(类的实例) )处于知情状态(更多信息请访问维基百科 [ ^ ])。



另一方面,静态方法的用途非常不同:它们是类方法,不处理类实例。


Sorry guys for stupid question.I undarstand that constructor calls only once in the program,but i don't understand in anyway when i should use a constructor.

解决方案

Are you sure this is a valid answerable question? This is a wrong approach to understanding things.

You don't need to look for benefits. Instead, you need to 1) understand what exactly one or another language and platform feature does; 2) analyze what do you need to achieve; 3) based on that previous item, choose the expressive feature of language which suits you more.

Don't even try to think in terms of "benefits". Your final decision should provide benefits. Language features do not have benefits, they all are needed (in a good language, of course). It all depends on purpose.

Comparison on the two kinds of method simply makes no sense. But you can compare two or more use cases. Let's consider them.

// case 1:
class MyClass {
    internal static MyClass Create(string initializationParameter) {
        return new MyClass(string)
    }
    private MyClass(string initializationParameter) { /* ... */ }
}

// vs simple
class MyClass {
    internal MyClass(string initializationParameter) { /* ... */ }
}

Here, you can instantiate MyClass in different ways, and it's quite apparent that first way is excessive and pointless. But there are cases where first approach (which we can call factory method makes a lot of sense).

Consider one use case from the .NET FCL, the class System.Net.WebRequest:
http://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx[^].

Let's understand why you create instances of this class not via its constructors, but via its factory methods Create? These two:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.create%28v=vs.110%29.aspx[^].

This class is abstract. So, not having its instance, you can call only the static members, including the factory methods. These methods will return you the instance of this class, but runtime types of this instance cannot be of the abstract class. These runtime types of the instances will be some derived, non-abstract type. The actual type to instantiate depends on the initialization parameter representing URL. Depending on the URL scheme ("http://", "htts://", "ftp://"), the factory methods chose appropriate derived implementation class. This mechanism leverage "true" OOP, late binding based on virtual methods.

—SA


Inimicos wrote:

constructor calls only once in the program



That's true of static constructors, not of instance constructors -- which are you asking about?


Constructors initialize objects: they put the objects (instances of a classes) in a know state (more at Wikipedia[^]).

On the other hand, static methods serve to a very different purpose: they are 'class methods' and don't deal with class instances.


这篇关于在比较静态方法时,构造函数的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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