Sub Main()
Dim myCar As New Car()
myCar.petName = "Sven"
myCar.currSpeed = 10
'speed up the car a few times and print out the new sate
For i As Integer = 0 To 10
myCar.SpeedUp(5)
myCar.PrintState()
Next
End Sub
Public Class Car
'the state of a car
Public petName As String
Public currSpeed As Integer
'the functionality of the car
Public Sub PrintState()
Console.WriteLine("{0} is going {1} MPH.", petName, currSpeed)
End Sub
Public Sub SpeedUp(ByVal delta As Integer)
currSpeed += delta
End Sub
End Class
Module Module1
Sub Main()
'make a car called chuck going 10 mph
Dim chuck As New Car()
chuck.PrintState()
'make a car call Mary going 0 MPH
Dim mary As New Car("Mary")
mary.PrintState()
'make a car called Daisy going 75 MPH
Dim daisy As New Car("Daisy", 75)
daisy.PrintState()
End Sub
Public Class Car
'the state of a car
Public petName As String
Public currSpeed As Integer
' a custom default constructor
Public Sub New()
petName = "Chuck"
currSpeed = 10
End Sub
'here, currSpeed will receive the default value of an Integer (zero)
Public Sub New(ByVal pn As String)
petName = pn
End Sub
Public Sub New(ByVal pn As String, ByVal cs As Integer)
petName = pn
currSpeed = cs
End Sub
'the functionality of the car
Public Sub PrintState()
Console.WriteLine("{0} is going {1} MPH.", petName, currSpeed)
End Sub
Public Sub SpeedUp(ByVal delta As Integer)
currSpeed += delta
End Sub
End Class
Sub Main()
Dim c As New Motorcycle
c.SetDriverName("Tiny")
c.PopAWheely()
Console.WriteLine("Rider name is {0}", c.driverName)
End Sub
Public Class Motorcycle
Public driverIntensity As Integer
Public driverName As String
'redundant constructor logic below!
Public Sub New()
End Sub
Public Sub New(ByVal intensity As Integer)
ValidateIntensity(intensity)
driverIntensity = intensity
End Sub
Public Sub New(ByVal intensity As Integer, ByVal name As String)
ValidateIntensity(intensity)
driverIntensity = intensity
driverName = name
End Sub
Sub ValidateIntensity(ByRef intensity As Integer)
If intensity > 10 Then
intensity = 10
End If
End Sub
Public Sub PopAWheely()
Console.WriteLine("Yeeeeeeeeee Haaaaaaaaaaaaewwww!")
End Sub
Public Sub SetDriverName(ByVal driverName As String)
Me.driverName = driverName
End Sub
End Class
Public Class Teenager
Public Shared r As New Random()
Public Shared Function GetRandomNumber(ByVal upperLimit As Short) As Integer
Return r.Next(upperLimit)
End Function
Public Shared Function Complain() As String
Dim messages As String() = _
{"Do I have to?", "He started it!", "I'm too tired...", "I hate school!", "You are sooo wrong."}
Return messages(GetRandomNumber(5))
End Function
End Class
Sub Main()
Console.WriteLine("***** Shared Methods *****")
For i As Integer = 0 To 5
Console.WriteLine(Teenager.Complain())
Next
End Sub
Sub Main()
Console.WriteLine("***** Fun with Shared Data *****")
Dim s1 As New SavingsAccount(50)
Dim s2 As New SavingsAccount(100)
'get and set interest rate at object level
Console.WriteLine("Interest rate is: {0}", s1.GetInterestRateObj())
s2.SetInterestRateObj(0.08)
'make new object: this does NOT "reset" the interest rate for this object
Dim s3 As New SavingsAccount(10000.75)
Console.WriteLine("Interest Rate is: {0}", SavingsAccount.GetInterestRate())
Console.ReadLine()
End Sub
Public Class SavingsAccount
Public currBalance As Double
Public Shared currInterestRate As Double = 0.04
Public Sub New(ByVal balance As Double)
currBalance = balance
End Sub
Shared Sub New()
Console.WriteLine("In Shared ctor!")
currInterestRate = 0.04
End Sub
'shared members to get/set interest rate
Public Shared Sub SetInterestRate(ByVal newRate As Double)
currInterestRate = newRate
End Sub
Public Shared Function GetInterestRate() As Double
Return currInterestRate
End Function
'Instance members to get/set interest rate
Public Sub SetInterestRateObj(ByVal newRate As Double)
currInterestRate = newRate
End Sub
Public Function GetInterestRateObj() As Double
Return currInterestRate
End Function
End Class
Public Class Employee
'field data
Private empName As String
Private empID As Integer
Private currPay As Single
Public Sub New()
End Sub
Public Sub New(ByVal name As String, ByVal id As Integer, ByVal pay As Single)
empName = name
empID = id
currPay = pay
End Sub
'methods
Public Sub GiveBonus(ByVal amount As Single)
currPay += amount
End Sub
Public Sub DisplayStats()
Console.WriteLine("Name: {0}", empName)
Console.WriteLine("ID: {0}", empID)
Console.WriteLine("Pay: {0}", currPay)
End Sub
'access (get method)
Public Function GetName() As String
Return empName
End Function
'Mutator (set method)
Public Sub SetName(ByVal name As String)
empName = name
End Sub
End Class
Sub Main()
Console.WriteLine("***** Basic Inheritance *****")
'make a car object
Dim myCar As New Car(80)
myCar.Speed = 50
Console.WriteLine("My car is going {0} MPH", myCar.Speed)
Console.ReadLine()
'now create a minivan object
Dim myVan As New MiniVan()
myVan.Speed = 10
Console.WriteLine("My van is going {0} MPH", myVan.Speed)
Console.ReadLine()
End Sub
Public Class Salespeople
Inherits Employee
Private numberOfSales As Integer
Public Property SalesNumber() As Integer
Get
Return numberOfSales
End Get
Set(ByVal value As Integer)
numberOfSales = value
End Set
End Property
End Class
Public Class Managers
Inherits Employee
Private numberOfOptions As Integer
Public Property StockOptions() As Integer
Get
Return numberOfOptions
End Get
Set(ByVal value As Integer)
numberOfOptions = value
End Set
End Property
End Class
Sub Main()
Console.WriteLine("**** Employee Class Hierarchy ******")
Console.WriteLine()
'make a salesperson
Dim danny As New Salespeople
With danny
.ID = 100
.SalesNumber = 50
.Name = "Dan McCabe"
End With
End Sub
odule Module1
Sub Main()
Console.WriteLine("***** Fun with System.Object *****")
Console.WriteLine()
Dim p1 As New Person("Homer", "Simpson", 50)
Dim p2 As New Person("Homer", "Simpson", 50)
'get stringified version of objects
Console.WriteLine("p1.ToString() = {0}", p1.ToString())
Console.WriteLine("p2.ToString() = {0}", p2.ToString())
'test overridden equals()
Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2))
'test hash codes
Console.WriteLine("Same hash codes?: {0}", p1.GetHashCode() = p2.GetHashCode())
Console.WriteLine()
'change age of p2 and test again
p2.personAge = 45
Console.WriteLine("p1.ToString() = {0}", p1.ToString())
Console.WriteLine("p2.ToString() = {0}", p2.ToString())
Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2))
Console.WriteLine("Same hash codes?: {0}", p1.GetHashCode() = p2.GetHashCode())
Console.ReadLine()
End Sub
End Module
Class Person
'public only for simplicity
'properties and private data would obviously be preferred
Public fName As String
Public lName As String
Public personAge As Byte
Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal age As Byte)
fName = firstName
lName = lastName
personAge = age
End Sub
Public Sub New()
End Sub
Public Overrides Function ToString() As String
Dim myState As String
myState = String.Format("[First Name: {0}; Last Name: {1}; Age: {2}", fName, lName, personAge)
Return myState
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If TypeOf obj Is Person AndAlso obj IsNot Nothing Then
Dim temp As Person = CType(obj, Person)
If temp.fName = Me.fName AndAlso temp.lName = Me.fName AndAlso temp.personAge = Me.personAge Then
Return True
Else
Return False
End If
Return False
End If
End Function
'return a hash code based on the person's ToSTring() Value
Public Overrides Function GetHashCode() As Integer
Return Me.ToString().GetHashCode()
End Function
End Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment