class类通过extends继承父类

      class Person {
        constructor(name) {
          this.name = name
        }
        publicFn() {
          console.log('公共方法')
        }
      }
      class Student extends Person {
        constructor(name, score) {
          // this.name = name
          super(name)
          this.score = score
        }
        introduce() {
          console.log(`我是${this.name},考了${this.score}分`)
        }
      }
      class Teacher extends Person {
        constructor(name, subject) {
          // this.name = name
          super(name)
          this.subject = subject
        }
        teach() {
          console.log(`我是${this.name},教${this.subject}`)
        }
      }
      const stu = new Student('小明', 100)
      const teac = new Teacher('王老师', '语文')
      console.log(stu)
      stu.introduce()
      stu.publicFn()
      console.log(teac)
      teac.teach()
      teac.publicFn()

 

posted @ 2021-08-20 17:37  吴小明-  阅读(74)  评论(0编辑  收藏  举报