VB.Net Tutorial/Class Module/Class Inheritance — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:55, 26 мая 2010
Содержание
- 1 Call base method
- 2 Circle class that inherits from class Point
- 3 Class implicitly Inherits Object
- 4 Class inheritance and polymorphism
- 5 Inherit constant from base class
- 6 Inherit ToString method from Object
- 7 Multilevel inheritance
- 8 Shadows member function from base class
- 9 Simple Inheritance
- 10 Use For Each for Class Hierarchy
Call base method
Option Strict On
Public Class BaseClass
Public Sub MainMethod()
Console.WriteLine("Calling Me.Method1...")
Me.Method1()
Console.WriteLine("Calling MyClass.Method1...")
MyClass.Method1()
End Sub
Public Overridable Sub Method1()
Console.WriteLine("BaseClass.Method1...")
End Sub
End Class
Public Class DerivedClass : Inherits BaseClass
Public Overrides Sub Method1()
Console.WriteLine("DerivedClass.Method1...")
End Sub
End Class
Public Module modMain
Public Sub Main()
Console.WriteLine("Invoking BaseClass.MainMethod")
Dim bc As New BaseClass
bc.MainMethod()
Console.WriteLine()
Console.WriteLine("Invoking DerivedClass.MainMethod")
Dim dc As New DerivedClass
dc.MainMethod()
End Sub
End Module
Invoking BaseClass.MainMethod Calling Me.Method1... BaseClass.Method1... Calling MyClass.Method1... BaseClass.Method1... Invoking DerivedClass.MainMethod Calling Me.Method1... DerivedClass.Method1... Calling MyClass.Method1... BaseClass.Method1...
Circle class that inherits from class Point
Module Tester
Sub Main()
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 " Main
End Module
Public Class Point
Protected mX, mY As Integer
Public Sub New()
X = 0
Y = 0
End Sub " New
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)
X = xValue
Y = yValue
End Sub " New
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
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
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function " ToString
End Class
Public Class Circle
Inherits Point " Circle Inherits from class Point
Private mRadius As Double " Circle"s radius
Public Sub New()
Radius = 0
End Sub " New
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer, ByVal radiusValue As Double)
mX = xValue
mY = yValue
Radius = radiusValue
End Sub " New
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
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
Return "Center = " & "[" & mX & ", " & mY & "]" & _
"; Radius = " & mRadius
End Function " ToString
End Class
X coordinate is 37 Y coordinate is 43 Radius is 2.5 The new location and radius of circle are Center = [2, 2]; Radius = 4.25 Diameter is 8.50 Circumference is 26.70 Area is 56.75
Class implicitly Inherits Object
Module Tester
Sub Main()
Dim point As CPoint
point = New CPoint(2, 5)
Console.WriteLine("X coordinate is " & point.X & _
vbCrLf & "Y coordinate is " & point.Y)
point.X = 10
point.Y = 10
Console.WriteLine("The new location of point is " & point.ToString())
End Sub " Main
End Module
Public Class CPoint
" implicitly Inherits Object
Private mX, mY As Integer
Public Sub New()
X = 0
Y = 0
End Sub
Public Sub New(ByVal xValue As Integer,ByVal yValue As Integer)
X = xValue
Y = yValue
End Sub " New
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
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
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function " ToString
End Class
X coordinate is 2 Y coordinate is 5 The new location of point is [10, 10]
Class inheritance and polymorphism
Imports System.Collections
public class Test
public Shared Sub Main
Dim objShape1 As New Triangle
Dim objShape2 As New Circle
Dim objShape3 As New Square
Dim ShapeEnum As IEnumerator
Dim dblTotalArea As Double
objShape1.SideA = 3
objShape1.SideB = 3.2
objShape1.SideC = 0.94
Console.WriteLine("The triangle"s area is " & objShape1.Area.ToString & ".")
objShape2.Radius = 4
Console.WriteLine("The circle"s area is " & objShape2.Area.ToString & ".")
objShape3.Side = 10.01
Console.WriteLine("The square"s area is " & objShape3.Area.ToString & ".")
Dim aryShapes() As Shape = {objShape1, objShape2, objShape3}
ShapeEnum = aryShapes.GetEnumerator
While ShapeEnum.MoveNext
dblTotalArea += CType(ShapeEnum.Current, Shape).Area
End While
Console.WriteLine("The total area of your shapes is " & dblTotalArea.ToString & ".")
End Sub
End class
Public MustInherit Class Shape
Public MustOverride Function Area() As Double
Public MustOverride Function Perimeter() As Double
End Class
Public Class Square
Inherits Shape
Private mySide As Double
Public Property Side() As Double
Get
Side = mySide
End Get
Set(ByVal Value As Double)
mySide = Value
End Set
End Property
Public Overrides Function Area() As Double
Area = mySide * mySide
End Function
Public Overrides Function Perimeter() As Double
Return (4 * mySide)
End Function
End Class
Public Class Triangle
Inherits Shape
Private mySide1, mySide2, mySide3 As Double
Property SideA() As Double
Get
SideA = mySide1
End Get
Set(ByVal Value As Double)
mySide1 = Value
End Set
End Property
Property SideB() As Double
Get
SideB = mySide2
End Get
Set(ByVal Value As Double)
mySide2 = Value
End Set
End Property
Public Property SideC() As Double
Get
SideC = mySide3
End Get
Set(ByVal Value As Double)
mySide3 = Value
End Set
End Property
Public Overrides Function Area() As Double
Dim dblPerim As Double
dblPerim = Perimeter()
Return (Math.Sqrt(dblPerim * (dblPerim - mySide1) * _
(dblPerim - mySide2) * (dblPerim - mySide3)))
End Function
Public Overrides Function Perimeter() As Double
Return (mySide1 + mySide2 + mySide3)
End Function
Sub New(ByVal SideA As Double, ByVal SideB As Double, ByVal SideC As Double)
MyBase.New()
mySide1 = SideA
mySide2 = SideB
mySide3 = SideC
End Sub
Sub New()
MyBase.New()
End Sub
End Class
Public Class Circle
Inherits Shape
Private m_dblRadius As Double
Public Property Radius() As Double
Get
Radius = m_dblRadius
End Get
Set(ByVal Value As Double)
m_dblRadius = Value
End Set
End Property
Public Overrides Function Area() As Double
Return (Math.PI * m_dblRadius ^ 2)
End Function
Public Overrides Function Perimeter() As Double
Return (2 * Math.PI * m_dblRadius)
End Function
Sub New()
MyBase.New()
End Sub
Sub New(ByVal Radius As Double)
MyBase.New()
m_dblRadius = Radius
End Sub
End Class
The triangle"s area is 26.8715818068085. The circle"s area is 50.2654824574367. The square"s area is 100.2001. The total area of your shapes is 177.337164264245.
Inherit constant from base class
Public Class ConstantBase
Public Const GENERATION As Integer = 1
End Class
Public Class ConstantDerived : Inherits ConstantBase
End Class
Public Class classMain
Public Shared Sub Main()
Dim cd As New ConstantDerived
Console.WriteLine(cd.GENERATION)
End Sub
End Class
1
Inherit ToString method from Object
Option Strict On
Public Class EmptyClass
End Class
Public Module modMain
Public Sub Main()
Dim ec As New EmptyClass
Console.WriteLine(ec.ToString())
End Sub
End Module
EmptyClass
Multilevel inheritance
Class Person
Public Name As String
Public Age As Integer
Public Sub New(ByVal Name As String, ByVal Age As Integer)
Me.Name = Name
Me.Age = Age
End Sub
End Class
Class Player
Inherits Person
Public Sport As String
Public Team As String
Public Sub New(ByVal Name As String, ByVal Age As Integer, _
ByVal Sport As String, ByVal Team As String)
MyBase.New(Name, Age)
Me.Team = Team
Me.Sport = Sport
End Sub
End Class
Class BasketPlayer
Inherits Player
Public PointsPerGame As Double
Public Rebounds As Double
Public Sub New(ByVal Name As String, ByVal Age As Integer, _
ByVal Sport As String, ByVal Team As String, ByVal Points As Double, ByVal Rebounds As Double)
MyBase.New(Name, Age, Sport, Team)
Me.PointsPerGame = Points
Me.Rebounds = Rebounds
End Sub
Public Sub Show()
Console.WriteLine("Player: " & Name)
Console.WriteLine("Age: " & Age)
Console.WriteLine("Sport: " & Sport)
Console.WriteLine("Team: " & Team)
Console.WriteLine("Points: " & PointsPerGame)
Console.WriteLine("Rebounds: " & Rebounds)
End Sub
End Class
Module Module1
Sub Main()
Dim K As New BasketPlayer("AA", 12, "Basketball", "Team", 25.5, 6.3)
K.Show()
End Sub
End Module
Player: AA Age: 12 Sport: Basketball Team: Team Points: 25.5 Rebounds: 6.3
Shadows member function from base class
Option Strict On
Public Class ShadowClass
Private value As String
Public Sub New(value As String)
Me.value = value
End Sub
Public Shadows Function ToString() As String
Return "Value of " & Me.GetType.Name & ": " & Me.value
End Function
End Class
Public Module ShadowTest
Public Sub Main()
Dim sc As ShadowClass = New ShadowClass("The Shadow")
Console.WriteLine(sc.ToString())
Dim obj As Object = New ShadowClass("The Shadow")
Console.WriteLine(obj.ToString())
End Sub
End Module
Value of ShadowClass: The Shadow ShadowClass
Simple Inheritance
Public Class Tester
Public Shared Sub Main
Dim objJohn As New John
objJohn.Walk()
End Sub
End Class
Public Class Person
Public Sub Walk()
Console.WriteLine("Walking...")
End Sub
End Class
Class John
Inherits Person
End Class
Walking...
Use For Each for Class Hierarchy
public class Test
public Shared Sub Main
Dim people As New Collection
people.Add(New Employee("A"))
people.Add(New Customer("B"))
For Each person As Object In people
Console.WriteLine(person.Name)
Next person
End Sub
End class
Public Class Employee
Public Name As String
Public Sub New(ByVal new_name As String)
Name = new_name
End Sub
Public Overridable Function IsManager() As Boolean
Return False
End Function
End Class
Public Class Customer
Public Name As String
Public Sub New(ByVal new_name As String)
Name = new_name
End Sub
End Class
Public Class Manager
Inherits Employee
Public Sub New(ByVal new_name As String)
MyBase.new(new_name)
End Sub
Public Overrides Function IsManager() As Boolean
Return True
End Function
End Class
A B