控制台代码
Module Module1
Sub Main()
Dim objCar As New Car
objCar.Color = "Red"
'颜色
Console.WriteLine("My car is this color:" & objCar.Color)
'原速度
Console.WriteLine("The car's speed is " & objCar.Speed & ".")
'加速了
objCar.Accelerate(5)
If objCar.IsMoving = True Then
Console.WriteLine("The car is moving.")
Else
Console.WriteLine("The car is Stopped")
End If
'减速了
objCar.Accelerate(-5)
If objCar.IsMoving = True Then
Console.WriteLine("The car is moving.")
Else
Console.WriteLine("The car is Stopped")
End If
'当前车速
Console.WriteLine("The car's speed is " & objCar.Speed & " now.")
'小汽车有几个门呢?
Console.WriteLine("The car have " & objCar.NumberOfDoors & " doors.")
Console.WriteLine()
Dim newCar As New Car
DisplayCarDetails(newCar)
Dim newSportCar As New SportsCar
newSportCar.HousePower = 240
newSportCar.Weight = 1085
DisplaySportCarDetails(newSportCar)
'等待用户输入
Console.ReadLine()
End Sub
'测试构造函数
Sub DisplayCarDetails(ByVal theCar As Car)
Console.WriteLine()
Console.WriteLine("The new Car")
Console.WriteLine("Color:" & theCar.Color)
Console.WriteLine("Number of doors:" & theCar.NumberOfDoors)
Console.WriteLine("Current speed :" & theCar.Speed)
Console.WriteLine("Acceleration rate:" & theCar.CalculateAccelerationRate)
End Sub
'测试子类
Sub DisplaySportCarDetails(ByVal theCar As SportsCar)
Console.WriteLine()
Console.WriteLine("The SportCar")
Console.WriteLine("Color:" & theCar.Color)
Console.WriteLine("Number of doors:" & theCar.NumberOfDoors)
Console.WriteLine("Current speed :" & theCar.Speed)
Console.WriteLine("Sports Car Horsepower:" & theCar.HousePower)
Console.WriteLine("Sports car Weight:" & theCar.Weight)
Console.WriteLine("Power to Weight Ratio:" & theCar.GetPowerToWeightRadio)
Console.WriteLine("Acceleration rate:" & theCar.CalculateAccelerationRate)
End Sub
End Module
--------------------------------------
Car类
Public Class Car
'字段
Public Color As String
Public HousePower As Integer
Private intSpeed As Integer = 0
Private intNumberofDoors As Integer = 5
'只读属性:速度
Public ReadOnly Property Speed As Integer
Get
Return intSpeed
End Get
End Property
'可读写的属性:门数
Public Property NumberOfDoors As Integer
Get
Return intNumberofDoors
End Get
Set(ByVal value As Integer)
intNumberofDoors = value
End Set
End Property
'构造函数
Public Sub New()
'初始化
Color = "White"
intSpeed = 0
intNumberofDoors = 5
End Sub
'加速方法
Public Sub Accelerate(ByVal accelerateBy As Integer)
'调整速度值,增加accelerateBy
intSpeed = intSpeed + accelerateBy
End Sub
'是否移动方法
Public Function IsMoving() As Boolean
If Speed = 0 Then
Return False
Else
Return True
End If
End Function
Public Overridable Function CalculateAccelerationRate() As Double
Return 4.2
End Function
End Class
-----------------------------------------
子类SportsCar
Public Class SportsCar
'继承
Inherits Car
Public Weight As Integer
Public Function GetPowerToWeightRadio() As Double
Return CType(HousePower, Double) / CType(Weight, Double)
End Function
Public Sub New()
Color = "Green"
NumberOfDoors = 2
End Sub
'方法重写
Public Overrides Function CalculateAccelerationRate() As Double
Return 4.2 * GetPowerToWeightRadio()
End Function
End Class
来源:CSDN
作者:ngbshzhn
链接:https://blog.csdn.net/ngbshzhn/article/details/104076292