VB.Net/Language Basics/Is
Версия от 16:40, 26 мая 2010; (обсуждение)
Demonstrate "is a" relationship
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim point1, point2 As Point
Dim circle1, circle2 As Circle
point1 = New Point(30, 50)
circle1 = New Circle(120, 89, 2.7)
Console.WriteLine( "Point point1: " & point1.ToString() & _
vbCrLf & "Circle circle1: " & circle1.ToString() )
" use is-a relationship to assign Circle to Point reference
point2 = circle1
Console.WriteLine( "Circle circle1 (via point2): " & point2.ToString())
" downcast (cast base-class reference to derived-class
" data type) point2 to circle2
circle2 = CType(point2, Circle) " allowed only via cast
Console.WriteLine( "Circle circle1 (via circle2): " & circle2.ToString() )
Console.WriteLine( "Area of circle1 (via circle2): " & _
String.Format("{0:F}", circle2.Area()) )
" assign Point object to Circle reference
If (TypeOf point1 Is Circle) Then
circle2 = CType(point1, Circle)
Console.WriteLine( "cast successful" )
Else
Console.WriteLine( "point1 does not refer to a Circle" )
End If
End Sub
End Class
Public Class Circle
Inherits Point " Circle Inherits from class Point
Private mRadius As Double
" default constructor
Public Sub New()
" implicit call to Point constructor occurs here
Radius = 0
End Sub " New
" constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer, ByVal radiusValue As Double)
" use MyBase reference to Point constructor explicitly
MyBase.New(xValue, yValue)
Radius = radiusValue
End Sub " New
" property Radius
Public Property Radius() As Double
Get
Return mRadius
End Get
Set(ByVal radiusValue As Double)
If radiusValue >= 0 Then " mRadius must be nonnegative
mRadius = radiusValue
End If
End Set
End Property " Radius
" calculate Circle diameter
Public Function Diameter() As Double
Return mRadius * 2
End Function " Diameter
" calculate Circle circumference
Public Function Circumference() As Double
Return Math.PI * Diameter()
End Function " Circumference
" calculate Circle area
Public Overridable Function Area() As Double
Return Math.PI * mRadius ^ 2
End Function " Area
" return String representation of Circle
Public Overrides Function ToString() As String
" use MyBase reference to return Circle String representation
Return "Center= " & MyBase.ToString() & _
"; Radius = " & mRadius
End Function " ToString
End Class
Public Class Point
" point coordinate
Private mX, mY As Integer
" default constructor
Public Sub New()
" implicit call to Object constructor occurs here
X = 0
Y = 0
End Sub " New
" constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)
" implicit call to Object constructor occurs here
X = xValue
Y = yValue
End Sub " New
" property X
Public Property X() As Integer
Get
Return mX
End Get
Set(ByVal xValue As Integer)
mX = xValue " no need for validation
End Set
End Property " X
" property Y
Public Property Y() As Integer
Get
Return mY
End Get
Set(ByVal yValue As Integer)
mY = yValue " no need for validation
End Set
End Property " Y
" return String representation of Point
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function " ToString
End Class