class修饰符的使用及区别

public、private、protected、static、abstract

public:可以继承、实例化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Person {
    public name: string;
    constructor(thename: string) {
        this.name = thename;
    }
}
class Jack extends Person {
    age: number;
    constructor(name: string, age: number) {
        super(name)
        this.age = age;
    }
    say() {
        console.log(`my name is ${this.name}, age ${this.age}`);
    }
}
let p1 = new Person('tom');
console.log(p1.name); // tom
let j1 = new Jack('jacker', 10);
j1.say(); // my name is jacker age 10

  private 私有属性只能在基类中访问,不能在实例、派生类中访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Person {
    private name: string;
    constructor(thename: string) {
        this.name = thename;
    }
    sayname() {
        console.log(`my name is ${this.name}`);
    }
}
class Jack extends Person {
    age: number;
    constructor(name: string, age: number) {
        super(name)
        this.age = age;
    }
    say() {
        // 只能在Person中访问
        // console.log(`my name is ${this.name}, age ${this.age}`); // error
    }
}
let p1 = new Person('tom');
p1.sayname(); // tom
// console.log(p1.name); // tom // error 只能在Person中访问
let j1 = new Jack('jacker', 10);
j1.sayname(); // jacker

 

   protected 受保护的,可以被继承,在派生类中可以访问,子类、父类都不能实例访问

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Person {
    protected name: string;
    constructor(thename: string) {
        this.name = thename;
    }
    sayname() {
        console.log(`my name is ${this.name}`);
    }
}
class Jack extends Person {
    constructor(name: string) {
        super(name)
    }
    say() {
        // 只能在Person中访问
        console.log(`my name is ${this.name}`);
    }
}
let p1 = new Person('tom');
p1.sayname(); // tom
console.log(p1.name); // tom // error 只能在Person、子类中访问
let j1 = new Jack('jacker');
j1.say(); // jacker
console.log(j1.name); // error 只能在Person、子类中访问

 

  // static 只能通过基类、子类访问,实例不能访问

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Person {
    static myName: string;
    constructor(name: string) {
        Person.myName = name;
    }
    sayname() {
        return Person.myName;
    }
}
class Jack extends Person {
    constructor() {
        super('jacker');
    }
}
let p1 = new Person('tom');
p1.myName; // error Person上不存在myName属性
console.log(p1.sayname());// tom
// 在类的外部访问
console.log(Person.myName); // tom
let j1 = new Jack();
// 子类实例访问基类方法
console.log(j1.sayname()); // jacker
j1.myName // error Jack 上不存在myName属性
// 子类访问静态属性
console.log(Jack.myName); // jacker

 

   // abstract 抽象类中的抽象方法不包含具体实现并且必须在派生类中实现

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
abstract class Person {
    sayname() {
        console.log('my name is sayname');
    }
    // 抽象方法不具体实现
    abstract say(): void;
}
class Jack extends Person {
    // 子类必须实现父类抽象方法
    say() {
        console.log('my name is jacker');
    }
}
// let p1 = new Person(); // 抽象类不可以被实例化
let j1 = new Jack();
j1.sayname();
j1.say()

 

  

 

 

 

 

class修饰符的使用及区别

public、private、protected、static、abstract

public:可以继承、实例化

  1.  
    // public可以继承、实例化
  2.  
    class Person {
  3.  
    public name: string;
  4.  
    constructor(thename: string) {
  5.  
    this.name = thename;
  6.  
    }
  7.  
    }
  8.  
    class Jack extends Person {
  9.  
    age: number;
  10.  
    constructor(name: string, age: number) {
  11.  
    super(name)
  12.  
    this.age = age;
  13.  
    }
  14.  
    say() {
  15.  
    console.log(`my name is ${this.name}, age ${this.age}`);
  16.  
    }
  17.  
    }
  18.  
    let p1 = new Person('tom');
  19.  
    console.log(p1.name); // tom
  20.  
    let j1 = new Jack('jacker', 10);
  21.  
    j1.say(); // my name is jacker age 10

private 私有属性只能在基类中访问,不能在实例、派生类中访问

  1.  
    class Person {
  2.  
    private name: string;
  3.  
    constructor(thename: string) {
  4.  
    this.name = thename;
  5.  
    }
  6.  
    sayname() {
  7.  
    console.log(`my name is ${this.name}`);
  8.  
    }
  9.  
    }
  10.  
    class Jack extends Person {
  11.  
    age: number;
  12.  
    constructor(name: string, age: number) {
  13.  
    super(name)
  14.  
    this.age = age;
  15.  
    }
  16.  
    say() {
  17.  
    // 只能在Person中访问
  18.  
    // console.log(`my name is ${this.name}, age ${this.age}`); // error
  19.  
    }
  20.  
    }
  21.  
    let p1 = new Person('tom');
  22.  
    p1.sayname(); // tom
  23.  
    // console.log(p1.name); // tom // error 只能在Person中访问
  24.  
    let j1 = new Jack('jacker', 10);
  25.  
    j1.sayname(); // jacker

  protected 受保护的,可以被继承,在派生类中可以访问,子类、父类都不能实例访问

  1.  
    class Person {
  2.  
    protected name: string;
  3.  
    constructor(thename: string) {
  4.  
    this.name = thename;
  5.  
    }
  6.  
    sayname() {
  7.  
    console.log(`my name is ${this.name}`);
  8.  
    }
  9.  
    }
  10.  
    class Jack extends Person {
  11.  
    constructor(name: string) {
  12.  
    super(name)
  13.  
    }
  14.  
    say() {
  15.  
    // 只能在Person中访问
  16.  
    console.log(`my name is ${this.name}`);
  17.  
    }
  18.  
    }
  19.  
    let p1 = new Person('tom');
  20.  
    p1.sayname(); // tom
  21.  
    console.log(p1.name); // tom // error 只能在Person、子类中访问
  22.  
    let j1 = new Jack('jacker');
  23.  
    j1.say(); // jacker
  24.  
    console.log(j1.name); // error 只能在Person、子类中访问

// static 只能通过基类、子类访问,实例不能访问

  1.  
    class Person {
  2.  
    static myName: string;
  3.  
    constructor(name: string) {
  4.  
    Person.myName = name;
  5.  
    }
  6.  
    sayname() {
  7.  
    return Person.myName;
  8.  
    }
  9.  
    }
  10.  
    class Jack extends Person {
  11.  
    constructor() {
  12.  
    super('jacker');
  13.  
    }
  14.  
    }
  15.  
    let p1 = new Person('tom');
  16.  
    p1.myName; // error Person上不存在myName属性
  17.  
    console.log(p1.sayname());// tom
  18.  
    // 在类的外部访问
  19.  
    console.log(Person.myName); // tom
  20.  
    let j1 = new Jack();
  21.  
    // 子类实例访问基类方法
  22.  
    console.log(j1.sayname()); // jacker
  23.  
    j1.myName // error Jack 上不存在myName属性
  24.  
    // 子类访问静态属性
  25.  
    console.log(Jack.myName); // jacker

 // abstract 抽象类中的抽象方法不包含具体实现并且必须在派生类中实现

  1.  
    abstract class Person {
  2.  
    sayname() {
  3.  
    console.log('my name is sayname');
  4.  
    }
  5.  
    // 抽象方法不具体实现
  6.  
    abstract say(): void;
  7.  
    }
  8.  
    class Jack extends Person {
  9.  
    // 子类必须实现父类抽象方法
  10.  
    say() {
  11.  
    console.log('my name is jacker');
  12.  
    }
  13.  
    }
  14.  
    // let p1 = new Person(); // 抽象类不可以被实例化
  15.  
    let j1 = new Jack();
  16.  
    j1.sayname();
  17.  
    j1.say();
  18.  

posted on   ygunoil  阅读(1141)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示