VB.NET 使用可选输入创建 class

VB.NET Creating a class with optional input

我正在努力学习更多关于面向对象编程的知识。

我正在尝试创建一个 class 代表此图像中的圆锥形对象。

A:连接直径 B:直径 C : 身高

但对我来说,在选择圆锥体时存在一些差异

1) 锥体连接是否会大于直径,你有一个扩展锥体还是一个缩小锥体。为此,我基于在构造函数中设置的 Enum 创建了一个只读的 属性。这是在创建圆锥对象时设置的并且是固定的。

2) 您想如何标注尺寸。图像有 3 个维度,但第四个维度是角度。如果高度是主要尺寸,则应计算角度,如果角度是主要尺寸,则应计算高度。同样为此,我基于在构造函数中设置的枚举创建了一个只读 属性。

但是我应该如何标记我的属性呢?在一种情况下,高度应该是只读的,而在另一种情况下,角度应该是只读的。您如何处理这种情况?

Public Class Cone

    Public ReadOnly Property ConeType As ConeTypeEnum

    Public Enum ConeTypeEnum
        kExpansion = 1
        kReduction = 2
    End Enum

    Public ReadOnly Property DimensionType As DimensionTypeEnum

    Public Enum DimensionTypeEnum
        kAngle = 1
        kHeight = 2
    End Enum

    Public Property Height As Double
    Public Property Diameter As Double
    Public Property ConnectionDiameter As Double
    Public Property Angle As Double

    Public Sub New(ByVal oConeType As ConeTypeEnum,
                   ByVal oDimensionType As DimensionTypeEnum)

        ConeType = oConeType
        DimensionType = oDimensionType

    End Sub

End Class

我会这样做:

根据只读 属性 中的当前 AB 动态确定圆锥类型 属性。据我了解,这只是您拥有的圆锥类型的一个指标。

所有其他属性都可以 read/write,更新各自的其他属性。例如。如果用户改变角度,重新计算高度。如果用户改变高度,重新计算角度等

如果你真的想设置圆锥类型,我会在调用构造函数时设置。然后更改其他属性,以便它们在 AB 不适合当前圆锥类型的情况下抛出异常。

另一种方法是定义一个具有所有必需属性的接口 ICone,并从该接口派生到实现 [=28] 的 类 ExpansionConeReductionCone =]相应地。

这是我会做的:

Public Class Cone

    'First we have our dimensions
    Public Property Height As Double 'C on your graph
    Public Property DiameterUp As Double 'A on your graph
    Public Property DiameterDown As Double 'B on your graph

    'The cone Type can be calculated following the dimensions
    Public ReadOnly Property ConeType As ConeTypeEnum
        Get
            If _diameterDown > _diameterUp Then
                Return ConeTypeEnum.kReduction
            ElseIf _diameterDown < _diameterUp Then
                Return ConeTypeEnum.kExpansion
            End If
            'If they are equal it is a cylinder
            Return ConeTypeEnum.kCylinder
        End Get
    End Property

    Public Enum ConeTypeEnum
        kExpansion = 1
        kReduction = 2
        kCylinder = 3
    End Enum

    'The angle is just recalculated everytime
    Public Property Angle As Double
        Get
            Return 'Calculate here the angle value from the other dimensions
        End Get
        Set(value As Double)
            'Calculate here the dimension values according the value specified for the angle
        End Set
    End Property

    Public Sub New(Height As Double, DiameterUp As Double, DiameterDown As Double)
        Me.Height = Height
        Me.DiameterUp = DiameterUp
        Me.DiameterDown = DiameterDown
    End Sub
End Class

我会根据用户提供的是角度还是高度来动态计算高度或角度。像 Martin 一样,我会动态地获取圆锥体的类型。我还将 New 函数设为私有,以防止人们创建空代码,而是共享(工厂)函数。由于可以使用角度或高度创建圆锥体,因此 class 的用户将必须调用适当的函数来创建对象。

Cone.GetConeByAngle(...)
Cone.GetConeByHeight(...)

使用自定义 属性 计算高度和角度的另一个值。任何人都可以在计算正确后更改高度或角度。

由于我没有时间看数学,所以我将 CalculateAngle 和 CalculateHeight 留空。

我不知道你为什么把 "k" 作为枚举值的开头。

Public Class Cone

    Public Enum ConeTypeEnum
        Expansion = 1
        Reduction = 2
        Cylinder = 3
    End Enum

    Private _height As Double
    Private _angle As Double

    Public Property Diameter As Double
    Public Property ConnectionDiameter As Double

    Public Property Height As Double
        Get
            Return _height
        End Get
        Set(value As Double)
            _height = value
            CalculateAngle()
        End Set
    End Property

    Public Property Angle As Double
        Get
            Return _angle
        End Get
        Set(value As Double)
            _angle = value
            CalculateHeight()
        End Set
    End Property

    Public ReadOnly Property ConeType As ConeTypeEnum
        Get
            If Diameter > ConnectionDiameter Then
                Return ConeTypeEnum.Reduction
            ElseIf Diameter < ConnectionDiameter Then
                Return ConeTypeEnum.Expansion
            End If

            'If they are equal it is a cylinder
            Return ConeTypeEnum.Cylinder
        End Get
    End Property

    Private Sub New()

    End Sub

    Public Shared Function GetConeByAngle(ByVal angle As Double, ByVal diameter As Double, ByVal connectionDiameter As Double)

        Dim newCone As New Cone

        newCone.Diameter = diameter
        newCone.ConnectionDiameter = connectionDiameter
        newCone.Angle = angle

        Return newCone
    End Function

    Public Shared Function GetConeByHeight(ByVal height As Double, ByVal diameter As Double, ByVal connectionDiameter As Double)

        Dim newCone As New Cone

        newCone.Diameter = diameter
        newCone.ConnectionDiameter = connectionDiameter
        newCone.Height = height

        Return newCone
    End Function

    Private Sub CalculateAngle()
        ' Calculate angle base on height
        _angle = 0
    End Sub

    Private Sub CalculateHeight()
        ' Calculate height base on angle
        _height = 0
    End Sub

End Class