3.类-定义及修饰符

类的定义

class Person {

    name:string; // 属性 (刚写完会报错,需要在构造函数中执行赋值)

    constructor(n:string) {
        this.name = n; // 
    }

    run():void {
        console.log(this.name + '正在跑')
    }
}

let p = new Person('hyj');
console.log(p);
p.run();

类的继承

class People extends Person {
    constructor(n:string) {
        super(n); // 超类
    }
}

let peo = new People('hyj2');
console.log(peo)

类的修饰符

typescript中定义属性时提供的修饰符

class Person1 {
    public name:string;  //  共享
    protected sex:string; // 保护
    private age:number; // 私有

    constructor(n:string, s:string, a:number) {
        this.name = n;
        this.sex = s;
        this.age = a;
    }

    speakName():void {
        console.log(this.name);
    }

    speakSex():void{
        console.log(this.sex);
    }

    speakAge():void {
        
        console.log(this.age)
    }
}

class People1 extends Person1 {
    constructor(n:string, s:string, a:number) {
        super(n, s, a);
    }

    speakSex1():void{
        console.log(this.sex);
    }

    speakAge1():void {
        
        // console.log(this.age) // roperty 'age' is private and only accessible within class 'Person1'.
    }

}


let p1 = new Person1('person', '男生', 18);
let peo1 = new People1('people1', '女生', 19);

public: 共享

在类里面、子类、类外面都可以访问 (默认共有public)

class Animal {
    // 公共
    public name:string;
    constructor(n:string) {
        this.name = n;
    }

    // 公共
    public eat() {
        // A
        console.log(this.name, 'A')

    }
}


class Dog extends Animal {
    constructor(n:string) {
        super(n);
    }

    // B
    play() {
        console.log(this.name, 'B')
    }
}


// C


let kele = new Dog('可乐');
// A
kele.eat();
// B
kele.play();
// C
console.log(kele.name, 'C')

protected: 保护类型

在类里面、子类里面可以访问、在类外面无法访问

class Animal {
    // 保护
    protected name:string;
    constructor(n:string) {
        this.name = n;
    }

    // 公共
    public eat() {
        // A
        console.log(this.name, 'A')

    }
}


class Dog extends Animal {
    constructor(n:string) {
        super(n);
    }

    // B
    play() {
        console.log(this.name, 'B')
    }
}


// C


let kele = new Dog('可乐');
// A
kele.eat();
// B
kele.play();
// C :编译报错 属性“name”受保护,只能在类“Animal”及其子类中访问, 不能在类外面访问
console.log(kele.name, 'C')

private:私有

在类里面可以访问,子类、类外面无法访问

lass Animal {
    // 私有
    private name:string;
    constructor(n:string) {
        this.name = n;
    }

    // 公共
    public eat() {
        // A  只能在当前定义的这个类里面使用
        console.log(this.name, 'A')

    }
}


class Dog extends Animal {
    constructor(n:string) {
        super(n);
    }

    // B
    play() {
        // 错误  属性“name”为私有属性,只能在类“Animal”中访问,不能在子类及类外面访问
        // console.log(this.name, 'B')
    }
}


// C


let kele = new Dog('可乐');
// A
kele.eat();
// B
// kele.play();
// C : 属性“name”为私有属性,只能在类“Animal”中访问, 不能在子类及类外面访问
// console.log(kele.name, 'C')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容