加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 枚举类型实现位于Class实现的内部或外部

发布时间:2020-12-17 07:20:01 所属栏目:百科 来源:网络整理
导读:目前我有我的底层模型的代码: Public Enum vehicleType Car Lorry BicycleEnd EnumPublic Class TrafficSurveyA ' Declare the fields here. Private fCars As Integer Private fBicycles As Integer Private fLorries As Integer Public Sub New() ' An in
目前我有我的底层模型的代码:

Public Enum vehicleType
    Car
    Lorry
    Bicycle
End Enum
Public Class TrafficSurveyA
    ' Declare the fields here.
    Private fCars As Integer
    Private fBicycles As Integer
    Private fLorries As Integer

    Public Sub New()
        ' An instance of TrafficSurveyA is created with all vehicle counts set to zero.
        fCars = 0
        fBicycles = 0
        fLorries = 0
    End Sub
    Public Sub incrementCount(ByVal vehicle As vehicleType)
        ' Preconditions: none
        ' Postconditions: If vehicle is "Car","Bicycle" or "Lorry" then 1 is added
        ' to the corresponding count. Otherwise nothing is done.

        Select Case vehicle
            Case vehicleType.Car : fCars = fCars + 1
            Case vehicleType.Bicycle : fBicycles = fBicycles + 1
            Case vehicleType.Lorry : fLorries = fLorries + 1
            Case Else 'do nothing
        End Select
    End Sub

    Public Function getCount(ByVal vehicle As vehicleType) As String
        ' Preconditions: none
        ' Postconditions: If vehicle is "Car","Bicycle" or "Lorry",the string
        ' representation of the corresponding count is returned.
        ' Otherwise the empty string is returned.

        Dim result As String
        result = ""
        Select Case vehicle
            Case vehicleType.Car : result = Convert.ToString(fCars)
            Case vehicleType.Bicycle : result = Convert.ToString(fBicycles)
            Case vehicleType.Lorry : result = Convert.ToString(fLorries)
            Case Else : result = ""
        End Select
        Return result
    End Function

    Public ReadOnly Property Vehicles() As String
        ' Preconditions: none
        ' Postconditions: The total number of vehicles recorded is returned.
        Get
            Return (fCars + fBicycles + fLorries).ToString()
        End Get
    End Property
End Class

似乎Enum可以很容易地放在TrafficSurveyA类中,就像这样……

Public Class TrafficSurveyA

    Enum vehicleType
        Car
        Lorry
        Bicycle
    End Enum

    ' Declare the fields here.
    Private fCars As Integer
    Private fBicycles As Integer
    Private fLorries As Integer

    Public Sub New()
        ' An instance of TrafficSurveyA is created with all vehicle counts set to zero.
        fCars = 0
        fBicycles = 0
        fLorries = 0
    End Sub
    ...
    ...

唯一的区别似乎是我需要使用此TrafficSurveyA.vehicleType.Lorry而不是此vehicleType.Lorry的GUI代码.

两者似乎都运行正常,但枚举类型的这些实现之一是错误的吗?

解决方法

不,两者都很好.这只是一个偏好问题,也是组织目的最有意义的问题.我唯一的建议是,如果枚举将在任何其他类中用作输入或输出类型,我不会把它放在这个类中.那只会令人困惑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读