5

現在、基礎となるモデルに次のコードがあります。

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唯一の違いは、 thisではなく thisを使用する必要がある GUI コードにあるようvehicleType.Lorryです。

どちらも正常に動作しているように見えますが、列挙型のこれらの実装の 1 つが間違っていますか?

4

3 に答える 3

4

いいえ、どちらも大丈夫です。それは単なる好みの問題であり、組織の目的にとって何が最も理にかなっています。私が持っている唯一の提案は、列挙型が他のクラスで入力または出力型として使用される場合、このクラス内に配置しないことです。それはただ混乱するでしょう。

于 2012-12-23T17:19:57.813 に答える
1

どのオプションも間違っていませんが、入れ子になった公開型 (クラス内で宣言された列挙型など) は一般的に推奨されません。外部クラス名でそれらを修飾する必要があるクライアントを混乱させる可能性があります。関連するガイドラインはこちらをご覧ください。関連するもの:

public のネストされた型を論理的なグループ化構造として使用しないでください。これには名前空間を使用します。

入れ子になった型を公開することは避けてください。これに対する唯一の例外は、サブクラス化やその他の高度なカスタマイズ シナリオなどのまれなシナリオで、ネストされた型の変数を宣言する必要がある場合です。

型が宣言型の外部で参照される可能性が高い場合は、ネストされた型を使用しないでください。

于 2012-12-24T03:45:14.857 に答える
0

また、スコープを最も狭い範囲に制限することも考えてください。

  • クラス内でのみ使用されるプライベート
  • アセンブリ内で使用される友人
  • パブリックは、このプロジェクトから構築された任意のプロジェクトまたはアセンブリにアクセスできます

範囲

于 2012-12-23T19:34:32.453 に答える