什么是使用隐式/显convertions代替构造的原因是什么? [英] What's the reason of using implicit/explicit convertions instead of constructors?

查看:250
本文介绍了什么是使用隐式/显convertions代替构造的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个例子是:

 的XNamespace NS =我的空间
 

为什么不?

 的XNamespace NS =新的XNamespace(我的空间)
 

什么是使用隐式/显convertions,而不是构造背后的想法?便利?

有一个准则吗?

解决方案
  

便利?

更多或更少,是的。想想,当你有一些类似对象在其上做计算的情况下(复杂比方说,一个)。显然,写作code,如:

 复合结果= C1 *新的复合物(2)+新的复合体(32);
 

是很烦人的,难以阅读。隐式转换有助于在这里(另一种是在这个例子中操作符重载,但是这会导致许多类似重载)。

  

有一个准则吗?

提供尽可能少的隐式转换成为可能,因为它们可能隐藏的问题。隐式转换通过其简洁增加相同数量的减少明确性。有时候,这是很好的,但有时并非如此。

我觉得最好限制隐式转换的非常的类型相似,如在我的例子上述数字般的对象:一个 INT 本质上是-A 复合(从数学的角度来看,即使它不是通过继承建模),因此隐式转换是有道理的。

在VB中,隐式转换被称为拓宽(而不是缩小,这是明确),这说明它好:不会丢失信息在转换的过程中

此外,操作员实质上是一种助洗剂的功能,并且具有(部分)的过构造一种助洗剂功能的通常的优点:即,它可以重复使用缓存的值,而不是总是创建新实例

考虑我的复合的例子。我们可能要缓存值,常用复数:

 类复{
    //执行休息。

    私有静态复[]缓存=新的[] {
        新大楼(-1),新大楼(0),新的复合体(1)};

    公共隐含的复杂运算符(int值){
        如果(值GT = -1&功放;&放大器;值小于= 1)
            返回高速缓存[值];
        其他
            返回新的复杂(的价值);
    }
}
 

当然,这条微的优化是否有效是另一个问题。

An example would be:

XNamespace ns = "my namespace"

Why not?:

XNamespace ns = new XNamespace ( "my namespace" )

What's the idea behind using implicit/explicit convertions instead of constructors? Convenience?

Is there a guideline for this?

解决方案

Convenience?

More or less, yes. Consider the case for when you’ve got a number-like object (say, a Complex) on which you do calculations. Clearly, writing code such as:

Complex result = c1 * new Complex(2) + new Complex(32);

is very annoying and hard to read. Implicit conversions help here (an alternative would be operator overloads in this example, but that would lead to lots of similar overloads).

Is there a guideline for this?

Provide as few implicit conversions as possible, since they may hide problems. Implicit conversion reduce explicitness by the same amount by which they increase terseness. Sometimes this is good, but sometimes not.

I find it best to restrict implicit conversions to very similar types, such as the number-like objects in my example above: an int essentially is-a Complex (from a mathematical standpoint; even if it’s not modelled via inheritance), hence an implicit conversion makes sense.

In VB, an implicit conversion is called "Widening" (as opposed to Narrowing, which is explicit) and this describes it well: no information is lost in the course of the conversion.

Furthermore, an operator is essentially a builder function, and has (some of) the usual advantages of a builder function over a constructor: namely, it can re-use cached values instead of always creating new instances.

Consider my Complex example. We may want to cache values for often-used Complex numbers:

Class Complex {
    // Rest of implementation.

    private static Complex[] cache = new[] {
        new Complex(-1), new Complex(0), new Complex(1) };

    public implicit operator Complex(int value) {
        if (value >= -1 && value <= 1)
            return cache[value];
        else
            return new Complex(value);
    }
}

Of course, whether this micro-optimization is effective is another question.

这篇关于什么是使用隐式/显convertions代替构造的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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