用工具多层次的继承在VB.NET特性对C# [英] Multi-level inheritance with Implements on properties in VB.NET vs C#

查看:156
本文介绍了用工具多层次的继承在VB.NET特性对C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有2个接口定义如下:

Let's say I have 2 interfaces defined like so:

public interface ISkuItem
{
    public string SKU { get; set; }
}

public interface ICartItem : ISkuItem
{
    public int Quantity { get; set; }
    public bool IsDiscountable { get; set; }
}

当我去到C#实现的接口,VS产生以下模板code:

When I go to implement the interface in C#, VS produces the following templated code:

public class CartItem : ICartItem
{

    #region ICartItem Members

    public int Quantity { get {...} set {...} }

    public bool IsDiscountable { get {...} set {...} }

    #endregion

    #region ISkuItem Members

    public string SKU { get {...} set {...} }

    #endregion
}

在VB.NET中,同一个类被建造出来,像这样:

In VB.NET, the same class is built out like so:

Public Class CartItem
    Implements ICartItem

    Public Property IsDiscountable As Boolean Implements ICartItem.IsDiscountable
        'GET SET'
    End Property

    Public Property Quantity As Integer Implements ICartItem.Quantity
        'GET SET'
    End Property

    Public Property SKU As String Implements ISkuItem.SKU
        'GET SET'
    End Property
End Class

VB.NET明确要求您添加实现IInterfaceName.PropertyName ,获取,而C#实现每个属性只是使用后区域■要指出哪些属性和方法属于接口。

VB.NET explicitly requires you to add Implements IInterfaceName.PropertyName after each property that gets implemented whereas C# simply uses regions to indicate which properties and methods belong to the interface.

有趣的是在VB.NET,在 SKU 属性,我可以指定实现ISkuItem.SKU 实现ICartItem.SKU 。尽管通过VS默认建 ISkuItem 的模板,我还可以指定 ICartItem 如果我想。奇怪的是,因为C#只有使用区域 s至阻挡继承属性,看来我不能明确指定 SKU 在C#中像我一样在VB.NET。

Interestingly in VB.NET, on the SKU property, I can specify either Implements ISkuItem.SKU or Implements ICartItem.SKU. Although the template built by VS defaults to ISkuItem, I can also specify ICartItem if I want. Oddly, because C# only uses regions to block out inherited properties, it seems that I can't explicitly specify the implementing interface of SKU in C# like I can in VB.NET.

我的问题是:有没有背后能够指定一个接口或其他在VB.NET实现性能的任何重要的,如果是的话,是有办法模仿C#这个功能?此外,什么是指定一个接口,在另一个实施属性时的效果?

推荐答案

我觉得其他的答案其实是有点过这里的标志。

I think the other answers are actually a little off the mark here.

在这个例子中你已经发布,一个接口的继承从其他。这只是意味着它提供了相同的成员为基础,再加上一些额外的成员。

In the example you've posted, one interface inherits from the other. This simply means that it offers the same members as its base, plus some additional members.

这些不是两个这种情况发生,以暴露部件具有相同名称的独立接口。 ICartItem.SKU 是同样的事情 ISkuItem.SKU 。这 ICartItem ISkuItem 只是意味着继承 ISkuItem ,作为一个接口,再presents由 ICartItem 提供的功能的子集。

These are not two independent interfaces that happen to expose members with the same name. ICartItem.SKU is the same thing as ISkuItem.SKU. That ICartItem inherits from ISkuItem simply means that ISkuItem, as an interface, represents a subset of the functionality offered by ICartItem.

考虑这个code:

class CartItem : ICartItem
{
    public int Quantity { get; set; }
    public bool IsDiscountable { get; set; }

    string ISkuItem.SKU
    {
        get { return "ISkuItem"; }
        set { throw new NotSupportedException(); }
    }

    string ICartItem.SKU
    {
        get { return "ICartItem"; }
        set { throw new NotSupportedException(); }
    }
}

这个类就不会编译。您不能定义 ICartItem.SKU 明确在这种情况下,因为 ICartItem.SKU 只是 ISkuItem.SKU 。有没有其他 SKU 属性来定义。

This class will not compile. You cannot define ICartItem.SKU explicitly in this case, because ICartItem.SKU is just ISkuItem.SKU. There's no "other" SKU property to define.

因此​​,要直接回答你的问题:

So, to answer your questions directly:

有没有背后是任何意义   可以指定一个接口或   另一个实施型属性   VB.NET?

Is there any importance behind being able to specify one interface or another to implement properites in VB.NET?

当他们是独立的,不相关的接口的:
正如其他人所指出的那样,你可以为不同的接口的成员共享一个共同的名字提供不同的实现。

When they are separate, unrelated interfaces: yes.
As others have pointed out, you can provide different implementations for different interfaces' members sharing a common name.

但是,当一个接口继承的其他的:
不要紧,因为他们是同样的事情。

But when one interface inherits from the other: no.
It doesn't matter because they're the same thing.

什么是指定一个效果   接口在另一个时   执行型属性?

What is the effect of specifying one interface over another when implementing properites?

此外,如果他们不相关的接口,它有其他人已经讨论过的效果:两个接口,可提供不同的实现。但如果一个源于另一方面,它没有任何效果

Again, if they're unrelated interfaces, it has the effect already discussed by others: providing different implementations for the two interfaces. But if one derives from the other, it has no effect.

这篇关于用工具多层次的继承在VB.NET特性对C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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