TypeScript - 私有变量和受保护变量之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36843357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript - Difference between Private and Protected Variables
提问by Swetabh
What is the difference between private
and protected
variables in TypeScript? Similar questions exist for C#
but I am not sure if the concepts in the two languages are identical. If not, it would be useful to know the differences.
TypeScript 中的private
和protected
变量有什么区别?存在类似的问题,C#
但我不确定这两种语言中的概念是否相同。如果没有,了解这些差异会很有用。
回答by Nitzan Tomer
It's the same as in other OO languages.
Private methods/members are accessible only from inside the class.
Protected methods/members are accessible from inside the class and extending class as well.
它与其他 OO 语言中的相同。
私有方法/成员只能从类内部访问。
受保护的方法/成员也可以从类内部和扩展类访问。
class A {
private x: number;
protected y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
getX(): number {
return this.x;
}
getY(): number {
return this.y;
}
}
class B extends A {
multiply(): number {
return this.x * this.y;
}
}
Notice that in class A
there's access to both (private) this.x
and (protected) this.y
.
But in class B
there's only access to this.y
and this.x
has this error:
请注意,在课堂A
上可以访问 (private)this.x
和 (protected) this.y
。
但是在课堂B
上只能访问this.y
并this.x
出现此错误:
Property 'x' is private and only accessible within class A
属性“x”是私有的,只能在 A 类中访问
(you can see the error in playground)
(您可以在操场上看到错误)
What's important to understand though is that this is only true to typescript.
In javascript those members are accessible to anyone with a reference to the instance.
重要的是要理解,这仅适用于打字稿。
在 javascript 中,任何引用实例的人都可以访问这些成员。
回答by Helping hand
PRIVATEMethod:
私人方法:
When a member is marked private, it cannot be accessed from outside of its containing class.
当成员被标记为private 时,不能从其包含类的外部访问它。
PROTECTEDMethod:
保护方法:
The protectedmodifier acts much like the privatemodifier with the exception that members declared protectedcan also be accessed within deriving classes.
该保护的修饰符的作用很像私人所不同的是议员申报修改的保护,也可以派生类中访问。
There is one more
point to add regarding Protected variables
:
有one more
一点要补充Protected variables
:
when a base class variable is protectedwe cannot use its variable from derivedclass directly.
当基类变量受到保护时,我们不能直接使用派生类中的变量。
For e.g:
例如:
class Car{
protected name: string;
constructor(name: string) { this.name = name; }
}
class Mercedes extends Car{
private noOfWheels: number;
constructor(name: string, noOfWheels: number) {
super(name);
this.noOfWheels= noOfWheels;
}
public getIntro() {
return `Hello, This is my ${this.name} and I have ${this.noOfWheels} wheels.`;
}
}
let myCar= new Mercedes ("COOL Car", 4);
console.log(myCar.getIntro()); //Hello, This is my COOL Car and I have 4 wheels.
console.log(myCar.name); // Error!! , Property 'name' is protected and only accessible within class 'Car' and its subclasses.
we can't use variable namedirectly from outside of Car class, we can still use it from within an instance method of Mercedes because Mercedes derives from Car.
我们不能直接在 Car 类之外使用变量名,我们仍然可以在 Mercedes 的实例方法中使用它,因为 Mercedes 派生自 Car。
回答by vcsjones
protected
works in TypeScript very similarly like it does from C#. The TypeScript release notesdocument it as such:
protected
在 TypeScript 中的工作方式与在 C# 中的工作方式非常相似。该打字稿发行说明文件它是这样:
The new protected modifier in classes works like it does in familiar languages like C++, C#, and Java. A protected member of a class is visible only inside subclasses of the class in which it is declared
类中新的 protected 修饰符的工作方式与在 C++、C# 和 Java 等熟悉的语言中一样。类的受保护成员仅在声明它的类的子类中可见
Whereas private
only lets you have access to the immediate class type. Private members are not visible to subclasses.
而private
只允许您访问直接类类型。私有成员对子类不可见。