codice:
Public Class Car
public color As String
Private _speed As Integer
Private _numberOfDoors = 5
Public HorsePower As Integer
ReadOnly Property speed() As Integer
Get
Return speed
End Get
End Property
Sub Accelerate(ByVal accelerateBY As Integer)
_speed += accelerateBY
End Sub
Property NumberOfDoors() As Integer
Get
Return _numberOfDoors
End Get
Set(ByVal Value As Integer)
If Value >= 2 And Value <= 5 Then
_numberOfDoors = Value
MsgBox(Value)
End If
End Set
End Property
Public Function IsMoving() As Boolean
If _speed = 0 Then
Return False
Else
Return True
End If
End Function
Overridable Function CalculateAcceleratorRate() As Double
Return 4.2
End Function
Sub New()
color = "White"
_speed = 0
_numberOfDoors = 5
End Sub
End Class
codice:
Module Module1
Sub main()
Dim myCar As Car
myCar = New Car
myCar.Accelerate(0)
If myCar.IsMoving Then
Console.WriteLine("The car is moving")
Else
Console.WriteLine("The car is not moving")
End If
Console.WriteLine(myCar.color)
Console.ReadLine()
DisplayCarDetails(myCar)
End Sub
Function DisplayCarDetails(ByVal mycar As Car)
mycar.HorsePower = 240
Console.WriteLine("Color " & mycar.color)
Console.WriteLine("number of doors " & mycar.NumberOfDoors)
Console.WriteLine("Current speed " & mycar.speed)
Console.ReadLine()
End Function
End Module
codice:
Public Class SportsCar
Inherits Car
Public Weight As Integer
Function GetPowerToWeightRatio()
Return CType(HorsePower, Single) / CType(Weight, Single)
End Function
Overrides Function CalculateAcceleratorRate() As Double
Return 4.2 * GetPowerToWeightRatio()
End Function
Sub New()
color = "Green"
NumberOfDoors = 2
End Sub
End Class