VB.Net/Class/Protected — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:40, 26 мая 2010
Protected Member Variables
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim circle As Circle
circle = New Circle(37, 43, 2.5) " instantiate Circle
Console.WriteLine( "X coordinate is " & circle.X & vbCrLf & _
"Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _
circle.Radius )
circle.X = 2
circle.Y = 2
circle.Radius = 4.25
Console.WriteLine("The new location and radius of circle are " & _
vbCrLf & circle.ToString() )
Console.WriteLine( "Diameter is " & _
String.Format("{0:F}", circle.Diameter()) )
Console.WriteLine("Circumference is " & _
String.Format("{0:F}", circle.Circumference()) )
Console.WriteLine( "Area is " & String.Format("{0:F}", circle.Area()) )
End Sub
End Class
" Circle class that inherits from class Point.
Public Class Circle
Inherits Point " Circle Inherits from class Point
Private mRadius As Double " Circle"s radius
" 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)
" implicit call to Point constructor occurs here
mX = xValue
mY = 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 = 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 Function Area() As Double
Return Math.PI * mRadius ^ 2
End Function " Area
" return String representation of Circle
Public Overrides Function ToString() As String
Return "Center = " & "[" & mX & ", " & mY & "]" & _
"; Radius = " & mRadius
End Function " ToString
End Class " Circle
" Point class represents an x-y coordinate pair.
Public Class Point
" implicitly Inherits Object
" point coordinate
Protected 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 value As Integer)
mX = value
End Set
End Property " X
" property Y
Public Property Y() As Integer
Get
Return mY
End Get
Set(ByVal value As Integer)
mY = value
End Set
End Property " Y
" return String representation of Point
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function " ToString
End Class " Point