2016-03-30 1 views
4

私はオブジェクト指向プログラミングについてもっと学びたいと思っています。VB.NETオプションの入力でクラスを作成する

私は、この画像のようにコニカルオブジェクトを表すクラスを作成しようとしています。

enter image description here

A:接続口径 B:直径 C:身長

しかし、コーン

1を選ぶとき私のためにいくつかの違いがありますが)大きくなることになるだろうコーン接続ですあなたが拡大円錐を持っている直径よりも小さく円錐。このために、コンストラクタで設定されているEnumに基づいて読み取り専用のプロパティを作成しました。これは、円錐オブジェクトを作成中に設定され、修正されています。

2)どのように寸法を設定しますか?画像は3次元であるが、4つ目は角度である。高さが先頭の寸法の場合は角度を計算し、角度が先行寸法の場合は高さを計算する必要があります。また、このために、コンストラクタで設定されているEnumに基づいて読み取り専用のプロパティを作成しました。

しかし、私は自分のプロパティをどのようにマークする必要がありますか? 1つのケースでは、高さは読み取り専用で、もう1つは角度でなければなりません。このような状況にどうやって対処していますか?

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 
+0

AFAIKではなく、ReadOnlyと動的に切り替えることはできません。おそらく、ある条件が満たされていない場合に値を変更しないセッターを追加することができますか? –

+0

これを解決する方法はたくさんあります。コンストラクタからのみ設定可能な値を持つことから、許可されている場合はプロパティを設定するメソッドがあり、同じConeインターフェイスを実装する2つの異なるConeクラスが実際にあります。あなたが実際にプロパティの異なる実装を望むことを考えれば、インターフェイスのアプローチIMOは、ラインを下げるのに役立つ可能性があります。 –

+2

高さを変更したときの角度を計算し、角度を変更したときの高さを計算することはできますか?そうであれば、両方のプロパティを読み書き可能のままにしておくことができます。 – reduckted

答えて

2

は、ここで私がしたいものです。

は読み取り専用のプロパティで、現在ABから動的コーン型プロパティを決定します。これは私が理解しているように、あなたが持っているコーン型の指標に過ぎません。

他のすべてのプロパティは、読み書き可能で、それぞれの他のプロパティを更新できます。例えば。ユーザーが角度を変更した場合は、高さを再計算します。ユーザーが高さを変更して角度を再計算した場合

本当にコーン型を設定したい場合は、コンストラクタを呼び出すときに実行します。他のプロパティを変更して、ABが現在のcone-typeに適合しない場合に例外をスローします。

もう1つの方法は、必要なすべてのプロパティを持つインターフェイスIConeを定義し、それに応じてgetter/setterを実装するクラスExpansionConeおよびReductionConeから派生させることです。ここで

0

は、私がどうなるのかです:

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 
0

私は、ユーザーが角度や高さを提供している場合、動的に基づいて、高さや角度を計算します。 Martinのように、私は円錐のタイプを動的に得るでしょう。私は、New関数をprivateにして、人々が空のコードを作成するのを防ぎ、その代わりに(ファクトリ)関数を共有するようにします。コーンは角度または高さで作成できるため、クラスのユーザーはオブジェクトを作成するための適切な関数を呼び出さなければなりません。

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 
+0

'なぜあなたが" k "をあなたの列挙値の始まりにするのかわかりません。それは習慣です。私はAutodesk Inventorでプログラミングすることに慣れており、APIではすべての列挙型がそのように記述されています。私は本当の説明がありません。 –

関連する問題