接口可以跨 vb.net 中的聚合/复合类实现吗?

发布于 2024-08-27 10:55:04 字数 624 浏览 6 评论 0原文

VB.NET .NET 3.5

我有一个名为 Package 的聚合类,作为运输系统的一部分。包包含另一个类 BoxType 。 BoxType包含有关用于运送包裹的盒子的信息,例如盒子的长度、宽度等。

包裹有一个名为 GetShippingRates 的方法。此方法调用单独的帮助程序类 ShipRater,并将 Package 本身作为参数传递。 ShipRater 检查包裹和 BoxType,并返回可能的运费/方法的列表。

我想要做的是构造一个接口 IRateable,它将提供给辅助类 ShipRater。因此,

Class ShipRater
    Sub New(SomePackage as Package)
    End Sub
End Class

我们不会这样做:

 Class ShipRater
    Sub New(SomeThingRateable as IRateable)
    End Sub
End Class

但是,ShipRater 需要来自 Package 及其聚合 BoxType 的信息。如果我编写一个接口 IRateable,那么如何使用 BoxType 属性来实现该接口的部分内容?这可能吗?

VB.NET .NET 3.5

I have an aggregate class called Package as part of a shipping system. Package contains another class, BoxType . BoxType contains information about the box used to ship the package, such as length, width, etc. of the Box.

Package has a method called GetShippingRates. This method calls a separate helper class, ShipRater, and passes the Package itself as an argument. ShipRater examines the Package as well as the BoxType, and returns a list of possible shipping rates/methods.

What I would like to do is construct an Interface, IRateable, that would be supplied to the helper class ShipRater. So instead of:

Class ShipRater
    Sub New(SomePackage as Package)
    End Sub
End Class

we would do:

 Class ShipRater
    Sub New(SomeThingRateable as IRateable)
    End Sub
End Class

However, ShipRater requires information from both the Package and its aggregate, BoxType. If I write an interface IRateable, then how can I use the BoxType properties to implement part of the Interface? Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も让我眼熟你 2024-09-03 10:55:04

该接口不需要了解有关如何聚合调用的任何信息(可以假设并非所有可计费项目都需要聚合调用)。将聚合(通过委托)调用留给 Package

Public Interface IRateable
    Function GetRate() As Double
End Interface

Public Class Package Implements IRateable
    Dim _boxType As New BoxType()

    'Rest of implementation'
    Public Function GetRate() As Double Implements IRateable.GetRate
        Return _boxType.Rate()
    End Function
End Class

The interface wouldn't need to know anything about how you aggregate the calls (it could be assumed that not all Rateable items need to agregate calls). Leave the aggregation (via delegation) calls up to Package:

Public Interface IRateable
    Function GetRate() As Double
End Interface

Public Class Package Implements IRateable
    Dim _boxType As New BoxType()

    'Rest of implementation'
    Public Function GetRate() As Double Implements IRateable.GetRate
        Return _boxType.Rate()
    End Function
End Class
小嗲 2024-09-03 10:55:04

当然。只需将所需的调用委托给聚合的 BoxType 即可。抱歉,C#:

class Package : IRateable
{
    private float weight;
    private BoxType box;

    Size IRateable.Dimensions
    {
        get { return box.Dimensions; }
    }

    float IRateable.Weight
    {
        get { return weight; }
    }
}

Sure. Just delegate required calls to an aggregated BoxType. Sorry for C#:

class Package : IRateable
{
    private float weight;
    private BoxType box;

    Size IRateable.Dimensions
    {
        get { return box.Dimensions; }
    }

    float IRateable.Weight
    {
        get { return weight; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文