gpt4 book ai didi

vb.net - 枚举类型实现位于类实现内部还是外部

转载 作者:行者123 更新时间:2023-12-04 19:12:04 24 4
gpt4 key购买 nike

目前,我的基础模型有以下代码:

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
...
...

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

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

最佳答案

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

关于vb.net - 枚举类型实现位于类实现内部还是外部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14012899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com