NenTang.vn
.NETLập trình căn bản C#
Làm quen Hướng đối tượng trong C#
Bài tập thực hành
Thời lượng: 32 phútĐọc khoảng: 2 phút
Từ viết tắt
Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
Mô tả bài toán
- Create a new project, and 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.
Cách giải quyết
Tạo các class sau:- Class
Person, có:- Thuộc tính (property):
Age: tuổi
- Phương thức (method):
Greet: hiển thị câu chào mừng "Hello"SetAge: gán giá trị cho thuộc tính tuổiAge
- Thuộc tính (property):
- Class
Studentkế thừa từ classPerson, có thêm:- Phương thức (method):
ShowAge: hiển thị thông tin tuổi.
- Phương thức (method):
- Class
Teacherkế thừa từ classPerson, có thêm:- Thuộc tính (property):
subject: môn giảng dạy
- Phương thức (method):
Explain: giảng bài cho Sinh viên
- Thuộc tính (property):
Source code
csharp

