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

类的基本实现

  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

猜你喜欢

转载自blog.csdn.net/kaimo313/article/details/134938970
今日推荐