Vb.net-classes-objects
VB.Net-クラスとオブジェクト
クラスを定義するときには、データ型の設計図を定義します。 これは実際にはデータを定義しませんが、クラス名が何を意味するのか、つまりクラスのオブジェクトが何を構成するのか、そしてそのようなオブジェクトに対して実行できる操作は定義します。
オブジェクトはクラスのインスタンスです。 クラスを構成するメソッドと変数は、クラスのメンバーと呼ばれます。
クラス定義
クラス定義は、キーワード Class で始まり、その後にクラス名が続きます。 End Bodyステートメントで終了するクラス本体。 以下は、クラス定義の一般的な形式です-
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _
Class name [ ( Of typelist ) ]
[ Inherits classname ]
[ Implements interfacenames ]
[ statements ]
End Class
どこで、
- attributelist は、クラスに適用される属性のリストです。 オプションです。
- accessmodifier は、クラスのアクセスレベルを定義します。値は、Public、Protected、Friend、Protected Friend、およびPrivateです。 オプションです。
- Shadows は、変数が同じ名前の要素、または基本クラスのオーバーロードされた要素のセットを再宣言して非表示にすることを示します。 オプションです。
- MustInherit は、クラスを基本クラスとしてのみ使用でき、そこから直接オブジェクト、つまり抽象クラスを作成できないことを指定します。 オプションです。
- NotInheritable は、クラスを基本クラスとして使用できないことを指定します。
- Partial は、クラスの部分的な定義を示します。
- Inherits は、継承元の基本クラスを指定します。
- Implements は、クラスが継承するインターフェイスを指定します。
次の例は、長さ、幅、高さの3つのデータメンバーを持つBoxクラスを示しています-
Module mybox
Class Box
Public length As Double ' Length of a box
Public breadth As Double ' Breadth of a box
Public height As Double ' Height of a box
End Class
Sub Main()
Dim Box1 As Box = New Box() ' Declare Box1 of type Box
Dim Box2 As Box = New Box() ' Declare Box2 of type Box
Dim volume As Double = 0.0 ' Store the volume of a box here
' box 1 specification
Box1.height = 5.0
Box1.length = 6.0
Box1.breadth = 7.0
' box 2 specification
Box2.height = 10.0
Box2.length = 12.0
Box2.breadth = 13.0
'volume of box 1
volume = Box1.height *Box1.length* Box1.breadth
Console.WriteLine("Volume of Box1 : {0}", volume)
'volume of box 2
volume = Box2.height *Box2.length* Box2.breadth
Console.WriteLine("Volume of Box2 : {0}", volume)
Console.ReadKey()
End Sub
End Module
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Volume of Box1 : 210
Volume of Box2 : 1560
メンバー関数とカプセル化
クラスのメンバー関数は、他の変数と同様に、クラス定義内にその定義またはプロトタイプを持つ関数です。 メンバーであるクラスの任意のオブジェクトで動作し、そのオブジェクトのクラスのすべてのメンバーにアクセスできます。
メンバー変数は(設計の観点から)オブジェクトの属性であり、カプセル化を実装するためにプライベートに保持されます。 これらの変数は、パブリックメンバー関数を使用してのみアクセスできます。
クラス内の異なるクラスメンバーの値を設定および取得するための上記の概念を入れてみましょう-
Module mybox
Class Box
Public length As Double ' Length of a box
Public breadth As Double ' Breadth of a box
Public height As Double ' Height of a box
Public Sub setLength(ByVal len As Double)
length = len
End Sub
Public Sub setBreadth(ByVal bre As Double)
breadth = bre
End Sub
Public Sub setHeight(ByVal hei As Double)
height = hei
End Sub
Public Function getVolume() As Double
Return length *breadth* height
End Function
End Class
Sub Main()
Dim Box1 As Box = New Box() ' Declare Box1 of type Box
Dim Box2 As Box = New Box() ' Declare Box2 of type Box
Dim volume As Double = 0.0 ' Store the volume of a box here
' box 1 specification
Box1.setLength(6.0)
Box1.setBreadth(7.0)
Box1.setHeight(5.0)
'box 2 specification
Box2.setLength(12.0)
Box2.setBreadth(13.0)
Box2.setHeight(10.0)
' volume of box 1
volume = Box1.getVolume()
Console.WriteLine("Volume of Box1 : {0}", volume)
'volume of box 2
volume = Box2.getVolume()
Console.WriteLine("Volume of Box2 : {0}", volume)
Console.ReadKey()
End Sub
End Module
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Volume of Box1 : 210
Volume of Box2 : 1560
コンストラクターとデストラクター
クラス*コンストラクタ*は、そのクラスの新しいオブジェクトを作成するたびに実行されるクラスの特別なメンバーSubです。 コンストラクターの名前は New であり、戻り値の型はありません。
次のプログラムは、コンストラクタの概念を説明します-
Class Line
Private length As Double ' Length of a line
Public Sub New() 'constructor
Console.WriteLine("Object is being created")
End Sub
Public Sub setLength(ByVal len As Double)
length = len
End Sub
Public Function getLength() As Double
Return length
End Function
Shared Sub Main()
Dim line As Line = New Line()
'set line length
line.setLength(6.0)
Console.WriteLine("Length of line : {0}", line.getLength())
Console.ReadKey()
End Sub
End Class
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Object is being created
Length of line : 6
デフォルトのコンストラクターにはパラメーターはありませんが、必要に応じて、コンストラクターにパラメーターを含めることができます。 このようなコンストラクターは、*パラメーター化されたコンストラクター*と呼ばれます。 この手法は、次の例に示すように、作成時にオブジェクトに初期値を割り当てるのに役立ちます-
Class Line
Private length As Double ' Length of a line
Public Sub New(ByVal len As Double) 'parameterised constructor
Console.WriteLine("Object is being created, length = {0}", len)
length = len
End Sub
Public Sub setLength(ByVal len As Double)
length = len
End Sub
Public Function getLength() As Double
Return length
End Function
Shared Sub Main()
Dim line As Line = New Line(10.0)
Console.WriteLine("Length of line set by constructor : {0}", line.getLength())
'set line length
line.setLength(6.0)
Console.WriteLine("Length of line set by setLength : {0}", line.getLength())
Console.ReadKey()
End Sub
End Class
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Object is being created, length = 10
Length of line set by constructor : 10
Length of line set by setLength : 6
- デストラクタ*は、クラスのオブジェクトがスコープから出るたびに実行されるクラスの特別なメンバーSubです。
デストラクタ*の名前は *Finalize であり、値を返すことも、パラメータを取ることもできません。 デストラクタは、ファイルを閉じる、メモリを解放するなど、プログラムから出る前にリソースを解放するのに非常に役立ちます。
デストラクタは継承またはオーバーロードできません。
次の例では、デストラクタの概念を説明します-
Class Line
Private length As Double ' Length of a line
Public Sub New() 'parameterised constructor
Console.WriteLine("Object is being created")
End Sub
Protected Overrides Sub Finalize() ' destructor
Console.WriteLine("Object is being deleted")
End Sub
Public Sub setLength(ByVal len As Double)
length = len
End Sub
Public Function getLength() As Double
Return length
End Function
Shared Sub Main()
Dim line As Line = New Line()
'set line length
line.setLength(6.0)
Console.WriteLine("Length of line : {0}", line.getLength())
Console.ReadKey()
End Sub
End Class
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Object is being created
Length of line : 6
Object is being deleted
VB.Netクラスの共有メンバー
Sharedキーワードを使用して、クラスメンバを静的として定義できます。 クラスのメンバーをSharedとして宣言すると、クラスのオブジェクトがいくつ作成されても、メンバーのコピーは1つしかありません。
キーワード Shared は、クラスに存在するメンバーのインスタンスが1つだけであることを意味します。 共有変数は、インスタンスを作成せずにクラスを呼び出すことで値を取得できるため、定数の定義に使用されます。
シェア変数は、メンバー関数またはクラス定義の外部で初期化できます。 クラス定義内でシェア変数を初期化することもできます。
メンバー関数をSharedとして宣言することもできます。 このような関数は、シェア変数のみにアクセスできます。 共有関数は、オブジェクトが作成される前でも存在します。
次の例は、共有メンバーの使用を示しています-
Class StaticVar
Public Shared num As Integer
Public Sub count()
num = num + 1
End Sub
Public Shared Function getNum() As Integer
Return num
End Function
Shared Sub Main()
Dim s As StaticVar = New StaticVar()
s.count()
s.count()
s.count()
Console.WriteLine("Value of variable num: {0}", StaticVar.getNum())
Console.ReadKey()
End Sub
End Class
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Value of variable num: 3
継承
オブジェクト指向プログラミングで最も重要な概念の1つは、継承の概念です。 継承を使用すると、別のクラスの観点からクラスを定義でき、アプリケーションの作成と保守が容易になります。 これにより、コード機能を再利用して実装時間を短縮することもできます。
クラスを作成するとき、プログラマは完全に新しいデータメンバーとメンバー関数を記述する代わりに、新しいクラスが既存のクラスのメンバーを継承するように指定できます。 この既存のクラスは base クラスと呼ばれ、新しいクラスは derived クラスと呼ばれます。
基本クラスと派生クラス
クラスは複数のクラスまたはインターフェイスから派生できます。つまり、複数の基本クラスまたはインターフェイスからデータと機能を継承できます。
派生クラスを作成するためにVB.Netで使用される構文は次のとおりです-
<access-specifier> Class <base_class>
...
End Class
Class <derived_class>: Inherits <base_class>
...
End Class
基本クラスShapeとその派生クラスRectangleを考えます-
' Base class
Class Shape
Protected width As Integer
Protected height As Integer
Public Sub setWidth(ByVal w As Integer)
width = w
End Sub
Public Sub setHeight(ByVal h As Integer)
height = h
End Sub
End Class
' Derived class
Class Rectangle : Inherits Shape
Public Function getArea() As Integer
Return (width * height)
End Function
End Class
Class RectangleTester
Shared Sub Main()
Dim rect As Rectangle = New Rectangle()
rect.setWidth(5)
rect.setHeight(7)
' Print the area of the object.
Console.WriteLine("Total area: {0}", rect.getArea())
Console.ReadKey()
End Sub
End Class
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Total area: 35
基本クラスの初期化
派生クラスは、基本クラスのメンバー変数とメンバーメソッドを継承します。 したがって、サブクラスを作成する前に、スーパークラスオブジェクトを作成する必要があります。 スーパークラスまたは基本クラスは、VB.Netで暗黙的に MyBase として知られています。
次のプログラムはこれを示しています-
' Base class
Class Rectangle
Protected width As Double
Protected length As Double
Public Sub New(ByVal l As Double, ByVal w As Double)
length = l
width = w
End Sub
Public Function GetArea() As Double
Return (width *length)
End Function
Public Overridable Sub Display()
Console.WriteLine("Length: {0}", length)
Console.WriteLine("Width: {0}", width)
Console.WriteLine("Area: {0}", GetArea())
End Sub
'end class Rectangle
End Class
'Derived class
Class Tabletop : Inherits Rectangle
Private cost As Double
Public Sub New(ByVal l As Double, ByVal w As Double)
MyBase.New(l, w)
End Sub
Public Function GetCost() As Double
Dim cost As Double
cost = GetArea()* 70
Return cost
End Function
Public Overrides Sub Display()
MyBase.Display()
Console.WriteLine("Cost: {0}", GetCost())
End Sub
'end class Tabletop
End Class
Class RectangleTester
Shared Sub Main()
Dim t As Tabletop = New Tabletop(4.5, 7.5)
t.Display()
Console.ReadKey()
End Sub
End Class
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
VB.Netは多重継承をサポートしています。