`protected` vs `private`

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() {
    // ✅ 保护的属性可以从继承类中获取到
    return this.age;
  }

  get Salary() {
      // ? 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);

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

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

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

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

//
console.log(derived.name);

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

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

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

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

相关资源

转载于:https://www.cnblogs.com/Wayou/p/private_vs_protected.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值