Typescript 类的属性/方法的默认可见性是什么
在Typescript中,默认情况下,Typescript类中所有属性或方法的可见性都是“public”。一个被标记为public的方法可以从任何地方访问,它没有限制。可见性有三种类型: public 、 private 和 protected 。
示例1:具有public成员可见性的类
公共函数和属性可以从任何地方访问。默认情况下,所有方法和属性都是“public”。在这个例子中,我们创建了一个名为student的类,它有两个属性和一个方法。我们使用public关键字来表示函数具有公共可见性,但是默认情况下所有属性和方法都是公共的,我们不需要指定。
Javascript
class Student {
name: string;
id: number;
constructor(name: string, id: number) {
this.name = name;
this.id = id;
}
public student_details() {
console.log(
"my name is : " +
this.name +
" my roll no is : " +
`${this.id.toString()}`
);
}
}
let obj = new Student("rahul", 367236);
obj.student_details();
输出: 已创建一个对象,并调用了student_details()方法。
my name is : rahul my roll no is : 367236
示例2:具有受保护成员可见性的类
具有“受保护”可见性的成员或属性只能在其类或其子类中访问。在这个例子中,在声明函数和属性’name’之前使用了 protected 关键字。类classRepresentative是从类Student派生而来的。当我们试图从外部访问“name”时,会返回一个错误,因为我们只能在该类或其子类中使用和访问它。名字也在student_name()函数中使用,但不会引发错误,因为它在类内部。由于student_details()是受保护的,所以不能从外部访问。
JavaScript
class Student {
protected name: string;
id: number;
constructor(name: string, id: number) {
this.name = name;
this.id = id;
}
protected student_details() {
console.log(
"my name is : " +
this.name +
" my roll no is : " +
`${this.id.toString()}`
);
}
student_name() {
console.log("My name is : " + self.name);
}
}
class classRepresentative extends Student {
detail() {
console.log("I am the class representative, my name is :"
+ this.name);
}
}
let obj = new Student("rahul", 367236);
let obj1 = new classRepresentative("samuel", 287636);
obj.name; // Error as name is protected
obj.student_name(); // No error
obj.student_details(); // Error
obj1.detail(); // No error
obj1.student_details(); //Error
输出:
error TS2445: Property 'name' is protected and only accessible within
class 'Student' and its subclasses.
obj.name; // error as name is protected
~~~~
two.ts:131:5 - error TS2445: Property 'student_details' is protected and only accessible
within class 'Student' and its subclasses.
obj.student_details(); //error
~~~~~~~~~~~~~~~
two.ts:133:6 - error TS2445: Property 'student_details' is protected and only accessible
within class 'Student' and its subclasses.
obj1.student_details(); //error
~~~~~~~~~~~~~~~
示例3:具有私有成员可见性的类
Private 与protected类似,但它还阻止子类访问成员。派生类无法改善其可见性,因为私有成员对派生类不可见。在前面的示例中,派生类在detail()函数中使用this.name属性,并且没有引发错误,因为该属性可以在子类中使用,但是当其可见性设置为private时,无法在子类或派生类中访问。
JavaScript
class Student {
private name: string;
id: number;
constructor(name: string, id: number) {
this.name = name;
this.id = id;
}
private student_details() {
console.log(
"my name is : " +
this.name +
" my roll no is : " +
`${this.id.toString()}`
);
}
student_name() {
console.log("My name is : " + self.name);
}
}
class classRepresentative extends Student {
detail() {
console.log("I am the class representative, my name is :"
+ this.name);
}
}
let obj = new Student("rahul", 367236);
let obj1 = new classRepresentative("samuel", 287636);
obj.name; // Error as name is private
obj.student_name(); // No error
obj.student_details(); // Error
obj1.detail(); // No error
obj1.student_details(); // Error
输出:
error TS2341: Property 'name' is private and only accessible within class 'Student'.
console.log("I am the class representative, my name is :" + this.name);
~~~~
two.ts:161:5 - error TS2341: Property 'name' is private and only accessible
within class 'Student'.
obj.name; // error as name is protected
~~~~
two.ts:163:5 - error TS2341: Property 'student_details' is private and only accessible
within class 'Student'.
obj.student_details(); //error
~~~~~~~~~~~~~~~
two.ts:165:6 - error TS2341: Property 'student_details' is private and only accessible
within class 'Student'.
obj1.student_details(); //error
~~~~~~~~~~~~~~~
参考: https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers