Person
, có:
Age
: tuổiGreet
: hiển thị câu chào mừng "Hello"SetAge
: gán giá trị cho thuộc tính tuổi Age
Student
kế thừa từ class Person
, có thêm:
ShowAge
: hiển thị thông tin tuổi.Teacher
kế thừa từ class Person
, có thêm:
subject
: môn giảng dạyExplain
: giảng bài cho Sinh viênusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TeacherAndStudentOOP { class Person { protected int age; public void Greet() { Console.WriteLine("Hello"); } public void SetAge(int n) { age = n; } } class Student : Person { public void ShowAge() { Console.WriteLine("My age is: {0} years old", age); } } class Teacher : Person { private string subject; public void Explain() { Console.WriteLine("Explanation begins"); } } class StudentAndTeacherTest { static void Main() { bool debug = false; Person myPerson = new Person(); myPerson.Greet(); Student myStudent = new Student(); myStudent.SetAge(21); myStudent.Greet(); myStudent.ShowAge(); Teacher myTeacher = new Teacher(); myTeacher.SetAge(30); myTeacher.Greet(); myTeacher.Explain(); if (debug) Console.ReadLine(); Console.ReadKey(); } } }
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!