Exercise
Classes Student + Teacher
Objetive
Write a Visual Basic (VB.Net) program that include in it the class Person that you just created.
Create a class "Student" and another class "Teacher", both descendants of "Person".
The class "Student" will have a public method "GoToClasses", which will write on screen "I’m going to class."
The class "Teacher" will have a public method "Explain", which will show on screen "Explanation begins". Also, it will have a private attribute "subject", a string.
The class Person must have a method "SetAge (int n)" which will indicate the value of their age (eg, 20 years old).
The student will have a public method "ShowAge" which will write on the screen "My age is: 20 years old" (or the corresponding number).
You must create another test class called "StudentAndTeacherTest" that will contain "Main" and:
Create a Person and make it say hello
Create a student, set his age to 21, tell him to Greet and display his age
Create a teacher, 30 years old, ask him to say hello and then explain.
Code
Imports System
Namespace December_19th
Class Teacher
Inherits Person
Private subject As String
Public Sub Explain()
Console.WriteLine("Explanation begins")
End Sub
End Class
End Namespace
Namespace December_19th
Class StudentAndTeacherTest
Private Shared Sub Main()
Dim debug As Boolean = False
Dim myPerson As Person = New Person()
myPerson.Greet()
Dim myStudent As Student = New Student()
myStudent.SetAge(21)
myStudent.Greet()
myStudent.ShowAge()
Dim myTeacher As Teacher = New Teacher()
myTeacher.SetAge(30)
myTeacher.Greet()
myTeacher.Explain()
If debug Then Console.ReadLine()
End Sub
End Class
End Namespace
Namespace December_19th
Class Student
Inherits Person
Public Sub ShowAge()
Console.WriteLine("My age is: {0} years old", age)
End Sub
End Class
End Namespace
Namespace December_19th
Class Person
Protected age As Integer
Public Sub Greet()
Console.WriteLine("Hello")
End Sub
Public Sub SetAge(ByVal n As Integer)
age = n
End Sub
End Class
End Namespace