class-demo.js 674 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. class People {
  2. constructor(name) {
  3. this.name = name
  4. }
  5. eat () {
  6. console.log(`${this.name} eat something`)
  7. }
  8. }
  9. class Student extends People{
  10. constructor(name, number){
  11. super(name)
  12. this.number = number;
  13. }
  14. sayHi() {
  15. console.log(
  16. `姓名 ${this.name}, 学号 ${this.number}`
  17. );
  18. }
  19. }
  20. class Teacher extends People {
  21. constructor(name, major) {
  22. super(name)
  23. this.major = major
  24. }
  25. teach () {
  26. console.log(`${this.name} 教授 ${this.major}`)
  27. }
  28. }
  29. let xialuo = new Student('xialuo', 100);
  30. let wang = new Teacher('王老师', '语文')
  31. console.log(xialuo.name);
  32. console.log(xialuo.number);
  33. xialuo.sayHi()
  34. wang.teach()