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

简介: 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


目录
相关文章
|
2月前
|
C++
c++继承类型与多继承
c++继承类型与多继承
20 0
|
6月前
|
存储 编译器 程序员
【C++】类与对象(一)类的定义 访问限定符 类的实例化 this指针
【C++】类与对象(一)类的定义 访问限定符 类的实例化 this指针
|
4月前
|
C++
C++ 类中静态成员和静态成员函数的继承覆盖
C++ 类中静态成员和静态成员函数的继承覆盖
26 0
|
9月前
|
安全 编译器 C++
[C++] 类与对象(中)类中六个默认成员函数(1)上
[C++] 类与对象(中)类中六个默认成员函数(1)上
|
9月前
|
存储 编译器 C++
[C++] 类与对象(中)类中六个默认成员函数(1)下
[C++] 类与对象(中)类中六个默认成员函数(1)下
|
10月前
怎样声明一个类不会被继承,什么场景下会用?
怎样声明一个类不会被继承,什么场景下会用?
44 0
|
11月前
|
编译器
C++11之继承构造函数(using 声明)
C++11之继承构造函数(using 声明)
134 0
|
C++
类的修饰符
类的修饰符
58 0
|
设计模式 安全 Java
Python面向对象、继承的基本语法、单继承和多层继承、子类重写父类的同名方法、子类调用父类的同名方法、继承中的 init 、 多继承、私有权限、类属性、类方法、静态方法、多态、鸭子类型
称为类 B 继承类 A 特点: B类的对象可以使用 A类的属性和方法 优点: 代码复用.重复相同的代码不用多次书写. 名词: 类A: 父类 基类 类B: 子类 派生类 2. 单继承和多层继承[理解] 单继承: 如果一个类只有一个父类,把这种继承关系称为单继承 多继承: 如果一个类有多个父类,把这种继承关系称为多继承 多层继承: C–> B --> A 3. 子类重写父类的同名方法[掌握] 重写: 子类定义和父类名字相同的方法. 为什么重写: 父类中的方法,不能满足子类对象的需求,所以
413 1
Python面向对象、继承的基本语法、单继承和多层继承、子类重写父类的同名方法、子类调用父类的同名方法、继承中的 init 、 多继承、私有权限、类属性、类方法、静态方法、多态、鸭子类型