



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Public Class Twix Private calories As Integer Private ingredients As ArrayList
Public Sub New() calories = 580 ingredients = New ArrayList ingredients.Add("Sugar") ingredients.Add("Chocolate") ingredients.Add("Caramel") End Sub
Public Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function End Class Public Class Reeses Private calories As Integer Private ingredients As ArrayList
Public Sub New() calories = 460 ingredients = New ArrayList ingredients.Add("Sugar") ingredients.Add("Chocolate") ingredients.Add("Peanut Butter") End Sub
Public Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function End Class
Dim twixbar As New Twix Dim reesescups As New Reeses Console.WriteLine(twixbar.GetInfo) Console.WriteLine(reesescups.GetInfo)
Calories: 580 Ingredients: Sugar Chocolate Caramel Calories: 460 Ingredients: Sugar Chocolate Peanut Butter
Public Class CandyBar Protected calories As Integer Protected ingredients As ArrayList
Public Sub New() ingredients = New ArrayList ingredients.Add("Sugar") ' All candy bars have sugar End Sub
Public Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function End Class
Public Class ChocolateBar Inherits CandyBar
Public Sub New() ingredients.Add("Chocolate") ' All chocolate bars have choc End Sub End Class
Public Class Twix Inherits ChocolateBar
Public Sub New() calories = 580 ingredients.Add("Caramel") End Sub End Class
Public Class Reeses Inherits ChocolateBar
Public Sub New() calories = 460 ingredients.Add("Peanut Butter") End Sub End Class
Dim twixbar As New Twix Dim reesescups As New Reeses Console.WriteLine(twixbar.GetInfo) Console.WriteLine(reesescups.GetInfo)
In CandyBar Class:
Overridable Function GetInfo() As String Dim sIngred As String Dim i As Integer
sIngred = CStr(ingredients(0)) For i = 1 To ingredients.Count - 1 sIngred = sIngred & " " & CStr(ingredients(i)) Next Return "Calories: " & CStr(calories) & _ " Ingredients: " & sIngred End Function
In Twix Class:
Overrides Function GetInfo() As String Return MyBase.GetInfo() & ". Two for me, none for you" End Function
Dim twixbar As New Twix Dim reesescups As New Reeses Console.WriteLine(twixbar.GetInfo) Console.WriteLine(reesescups.GetInfo)
Calories: 580 Ingredients: Sugar Chocolate Caramel Two for me, none for you Calories: 460 Ingredients: Sugar Chocolate Peanut Butter