class类的使用

1.我们先定义一个学生 类 输入姓名,得分

       class Student {
            constructor(name, score) {
                this.name = name;
                super(name)
                this.score = score;
            }
            introduce() {
                console.log(`我是${this.name},我考了${this.score}`);
            }
        }
        const student = new Student('gyb', 99)
        student.introduce();  //我是gyb,我考了99
        console.log(student);  //Student {name: "gyb", score: 99}

2.我们定义一个老师类 输入姓名 学科

        class Teacher {
            constructor(name, subject) {
                this.name = name;
                super(name)
                this.subject = subject;
            }
            introduce() {
                console.log(`我是${this.name},我教${this.subject}`);
            }
        }

        const teacher = new Teacher('小马哥', '前端开发')
        teacher.introduce()  //我是小马哥,我教前端开发
        console.log(teacher); //Teacher {name: "小马哥", subject: "前端开发"}
      
  1. 我们发现上面的两个类方法, 老师和学生都用共同的构造属性 this.name = name ,那么我们就可以用class继承父级的够着函数 用extends 和super( )实现继承
    同时继承以后,也可以实现调用父级的方法 hobby
    注意 被继承的类必须先声明再被继承
        class Person {
            constructor (name){
                this.name = name 
            }
            hobby(){
                console.log('我是自愿的');
            }
        }
        class Student extends Person {
            constructor(name, score) {
                // this.name = name;   //继承父级的 这里就不需要了
                super(name)  //用super 继承父级的构造函数
                this.score = score;
            }
            introduce() {
                console.log(`我是${this.name},我考了${this.score}`);
            }
        }
        const student = new Student('gyb', 99)
        student.introduce()
        student.hobby()
        console.log(student);

        class Teacher extends Person {
            constructor(name, subject) {
                // this.name = name;
                super(name)
                this.subject = subject;
            }
            introduce() {
                console.log(`我是${this.name},我教${this.subject}`);
            }
        }
        const teacher = new Teacher('小马哥', '前端开发')
        teacher.introduce()
        console.log(teacher);
        teacher.hobby()

在这里插入图片描述

练习案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
            总结,
            1.类中的构造器不是必须写的,要对实例进行一些初始化的操作才使用到, 如添加指定属性时
            2.如果A类继承B类,且A类总写了构造器,那么A类中的super是必须调用的.
            3.类中所定义的方法,都是放在了类的原型对象上,工实例去使用
        
        */
        // 创建一个Person类
        class Person {
            constructor(name, age) {
                //构造器中的this是谁,  类的实例对象
                this.name = name
                this.age = age
            }
            // 一般方法
            speak() {
                // speak方法放在了哪里?__类的原型对象上,供实例使用
                // 通过Person实例调用speak,speak中的this就是Person实例
                console.log(`我叫${this.name},我今年${this.age}`);
            }

        }

        //创建一个Student类,继承于Person类
        class Student extends Person {
            constructor(name, age, grade) {
                super(name, age)
                this.grade = grade
            }
            //重写从父类继承过来的方法
            speak() {
                console.log(`我叫${this.name},我今年${this.age},我现在是${this.grade}`);
            }
            study(){
                // study方法放在了哪里? 类的原型对象上,供实例使用
                // 通过student实例调用study时,study中的this就是student实例
                console.log('我的目标是学习');
            }

        }
        let p1 = new Person('张三', 18)
        console.log(p1);
        p1.speak()

        let s1 = new Student('李四', 19, '一年级')
        console.log(s1);
        s1.speak()
        s1.study()


    </script>
</body>

</html>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值