VB.Net/Class/Overridable
Версия от 16:40, 26 мая 2010; (обсуждение)
Define Overridable Member Function
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
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 = 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 Point String representation
Return "Center = " & MyBase.ToString() & _
"; Radius = " & mRadius
End Function " ToString
End Class
Public Class Point
" implicitly Inherits Object
" 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 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
Overridable Overloads Method from Base Class
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim e1 As New Programmer("E", 150000D)
e1.RaiseSalary(0.1D)
Console.WriteLine(e1.TheName & " salary is now " & e1.Salary())
End Sub
End Class
Public Class Employee
Private m_Name As String
Private m_Salary As Decimal
Public Sub New(ByVal theName As String, ByVal curSalary As Decimal)
m_Name = theName
m_Salary = curSalary
End Sub
Public ReadOnly Property TheName() As String
Get
Return m_Name
End Get
End Property
Public ReadOnly Property Salary() As Decimal
Get
Return MyClass.m_Salary
End Get
End Property
Public Overridable Overloads Sub RaiseSalary(ByVal Percent As Decimal)
m_Salary = (1 + Percent) * m_Salary
End Sub
End Class
Public Class Programmer
Inherits Employee
Public Sub New(ByVal theName As String, ByVal curSalary As Decimal)
MyBase.New(theName, curSalary)
End Sub
Public Overloads Overrides Sub RaiseSalary(ByVal Percent As Decimal)
MyBase.RaiseSalary(1.2D * Percent)
End Sub
End Class
Use Overridable Function
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
" instantiate object of class Cylinder
Dim cylinder As New Cylinder(12, 23, 2.5, 5.7)
" properties get initial x-y coordinate, radius and height
Console.WriteLine( "X coordinate is " & cylinder.X & vbCrLf & _
"Y coordinate is " & cylinder.Y & vbCrLf & "Radius is " & _
cylinder.Radius & vbCrLf & "Height is " & cylinder.Height )
" properties set new x-y coordinate, radius and height
cylinder.X = 2
cylinder.Y = 2
cylinder.Height = 10
cylinder.Radius = 4.25
" get new x-y coordinate and radius
Console.WriteLine( "The new location, radius " & _
"and height of cylinder are" & vbCrLf & "Center = [" & _
cylinder.ToString() )
" display Cylinder"s diameter
Console.WriteLine( "Diameter is " & _
String.Format("{0:F}", cylinder.Diameter()) )
" display Cylinder"s circumference
Console.WriteLine( "Circumference is " & _
String.Format("{0:F}", cylinder.Circumference()) )
" display Cylinder"s area
Console.WriteLine("Area is " & _
String.Format("{0:F}", cylinder.Area()) )
" display Cylinder"s volume
Console.WriteLine( "Volume is " & _
String.Format("{0:F}", cylinder.Volume()) )
End Sub
End Class
Public Class Cylinder
Inherits Circle
Private mHeight As Double
" default constructor
Public Sub New()
Height = 0
End Sub " New
" four-argument constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer, ByVal radiusValue As Double, _
ByVal heightValue As Double)
" explicit call to Circle constructor
MyBase.New(xValue, yValue, radiusValue)
Height = heightValue " set Cylinder height
End Sub " New
" property Height
Public Property Height() As Double
Get
Return mHeight
End Get
" set Cylinder height if argument value is positive
Set(ByVal heightValue As Double)
If heightValue >= 0 Then
mHeight = heightValue
End If
End Set
End Property " Height
" override method Area to calculate Cylinder area
Public Overrides Function Area() As Double
Return 2 * MyBase.Area + MyBase.Circumference * mHeight
End Function " Area
" calculate Cylinder volume
Public Function Volume() As Double
Return MyBase.Area * mHeight
End Function " Volume
" convert Cylinder to String
Public Overrides Function ToString() As String
Return MyBase.ToString() & "; Height = " & mHeight
End Function " ToString
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 = 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 Point String representation
Return "Center = " & MyBase.ToString() & _
"; Radius = " & mRadius
End Function " ToString
End Class
Public Class Point
" implicitly Inherits Object
" 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 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