class TestClass {
http: Http;
constructor(http: Http){
this.http = http;
}
}
公有,私有于受保护的修饰符
默认为 public
在上面的例子里,我们可以自由访问程序里定义的成员,在TypeScript里,成员默认为public
,你也可以明确的将一个成员标记成public
。
class Animal {
public name: string;
public constructor(theName: string){
this.name = theName;
}
public move(distanceInMeters: number){
console.log(`${
this.name} moved ${
distanceInMeters}m.`);
}
}
还要注意的是,在构造函数的参数上使用public
等同于创建了同名的成员变量