10 # 类:继承和成员修饰符

本文详细介绍了JavaScript中的类实现,包括实例属性与原型属性的区别,构造函数、继承机制,以及成员修饰符(public、private、protected和readonly)的作用。还探讨了静态成员的使用和构造函数修饰符的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类的基本实现

  1. 类的成员属性都是实例属性,而不是原型属性,类的成员方法都是原型方法。
class Dog {
    constructor(name: string) {
        this.name = name;
    }
    name: string;
    run() {}
}

console.log(Dog.prototype);
let dog = new Dog("wangwang");
console.log(dog);
  1. 实例的属性必须具有初始值或者在构造函数中赋值,否则会报错。
class Dog {
    constructor(name: string) {
        // this.name = name;
    }
    name: string = "wangwang";
    run() {}
}

类的继承

class Husky extends Dog {
    constructor(name: string, color: string) {
        super(name);
        // this 需在 super 之后
        this.color = color;
    }
    color: string;
}

类的成员修饰符

1. public

公有成员,类的所有属性默认都是 public,对所有人可见

class Dog {
    constructor(name: string) {
        this.name = name;
    }
    // 显示声明
    public name: string;
    run() {}
}

2. private

类的私有成员,只能在类的本身调用,不能被类的实例调用,也不能被子类调用。

class Dog {
    constructor(name: string) {
        this.name = name;
    }
    name: string;
    private privateMethod() {}
}

let dog = new Dog("wangwang");
// 下面会报错:属性“privateMethod”为私有属性,只能在类“Dog”中访问。
console.log(dog.privateMethod());

class Husky extends Dog {
    constructor(name: string, color: string) {
        super(name);
        // this 需在 super 之后
        this.color = color;
        // 下面会报错:属性“privateMethod”为私有属性,只能在类“Dog”中访问。
        this.privateMethod();
    }
    color: string;
}

给构造函数添加 private, 这个类不能被实例化,也不能被继承。

class Dog {
    private constructor(name: string) {
        this.name = name;
    }
    name: string;
    run() {}
    private privateMethod() {}
}

// 下面报错:类“Dog”的构造函数是私有的,仅可在类声明中访问。
let dog = new Dog("wangwang");

// 下面报错:无法扩展类“Dog”。类构造函数标记为私有。
class Husky extends Dog {
    constructor(name: string, color: string) {
        super(name);
        this.color = color;
    }
    color: string;
}

3. protected

受保护成员,只能在类的本身和子类中调用,不能被类的实例调用。

class Dog {
    constructor(name: string) {
        this.name = name;
    }
    name: string;
    protected protectedMethod() {}
}
let dog = new Dog("wangwang");
// 下面报错:属性“protectedMethod”受保护,只能在类“Dog”及其子类中访问。
console.log(dog.protectedMethod());

class Husky extends Dog {
    constructor(name: string, color: string) {
        super(name);
        this.color = color;
        // 子类调用不报错
        this.protectedMethod();
    }
    color: string;
}

给构造函数添加 protected, 这个类不能被实例化,只能被继承,相当于声明了一个基类。

4. readonly

只读属性,一定要被初始化,不能被修改。

class Dog {
    constructor(name: string) {
        this.name = name;
    }
    name: string;
    run() {}
    readonly legs: number = 4;
}

构造函数的参数也可以添加修饰符,作用就是自动将参数变为实例的属性,可以省略在类中的定义

class Husky extends Dog {
    constructor(name: string, public color: string) {
        super(name);
        this.color = color;
    }
    // 下面这个就可以省略
    // color: string;
}

5. static

类的静态成员,只能通过类名来调用,不能被类的实例调用。也可以被继承。

class Dog {
    constructor(name: string) {
        this.name = name;
    }
    name: string;
    run() {}
    static food: string = "bones"
}

let dog = new Dog("wangwang");
// 可以
console.log(Dog.food); // bones
// 下面报错
console.log(dog.food);

class Husky extends Dog {
    constructor(name: string, public color: string) {
        super(name);
        this.color = color;
    }
}

// 可以
console.log(Husky.food); // bones
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯小默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值