`protected` vs `private`

栏目: ASP.NET · 发布时间: 6年前

内容简介:标识为标识为以 TypeScript 为例,比如一个

private

标识为 private 的属性为私有属性,不能在除自己外的地方进行访问。

protected

标识为 protected 的属性为受保护的属性,与私有属性类似,但还可以在继承类中进行访问。

示例

以 TypeScript 为例,比如一个 Base 类,其中包含一个受保护的年龄属性 age ,以及一个私有的工资 _salary 属性。

class Base {
  constructor(protected age: number, private _salary: number) {}
}

同时定义一个 Derived 类继承自 Base 类:

class Derived extends Base {
  constructor(public name: string, age, salary) {
    super(age, salary);
  }
  get Age() {
    // :white_check_mark: 保护的属性可以从继承类中获取到
    return this.age;
  }

  get Salary() {
      // :rotating_light: Property '_salary' is private and only accessible within class 'Base'.ts(2341)
    return this._salary;
  }
}

然后分别实例化这两个类:

const base = new Base(18, 999);
const derived = new Derived("derived", 20, 1000);

然后通过他们的实例尝试访问这些属性。

// :rotating_light:Property '_salary' is private and only accessible within class 'Base'.ts(2341)
console.log(base._salary);

// :rotating_light:Property 'age' is protected and only accessible within class 'Base' and its subclasses.ts(2445)
console.log(base.age);

对于 base ,其两个属性都无法通过实例进行访问。

// :white_check_mark:
console.log(derived.name);

// :rotating_light: 虽然继承类中可以获取到父类的保护属性,但不能通过实例直接进行访问
console.log(derived.age);
// :white_check_mark: 只能在继承类的实例方法中访问,这里 `Age` 是继承类提供的获取器,该方法里访问并返回了父类的保护属性 `age`
console.log(derived.Age);

对于 derived ,其中 name 因为是 public 公有的,所以可通过实例直接访问。

age 继承自父类 Base ,只能在 Derived 类中的实例方法,比如这里定义的 get Age 获取器中进行访问。但无法通过 Derived 的实例直接访问。

Age 在这里为 Derived 类上面的一个获取器,其默认有 public 属性,通过访问它我们间接在类外面访问到了这个在父类中标识为受保护的属性 age


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Is Parallel Programming Hard, And, If So, What Can You Do About

Is Parallel Programming Hard, And, If So, What Can You Do About

Paul E. McKenney

The purpose of this book is to help you understand how to program shared-memory parallel machines without risking your sanity.1 By describing the algorithms and designs that have worked well in the pa......一起来看看 《Is Parallel Programming Hard, And, If So, What Can You Do About 》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码