java
public
可以被任意包中任意类访问
protected
同一包中的任意类可以访问,包括自身与子类
private
只有自身类可以访问,子类也不可以
Typescript
public
类的外部可以访问
protected
自身类与继承类可以使用
class test {
public name='fu'
protected age=20
private sex = 'boy'
getAll(){
console.log(this.name);
console.log(this.age);
console.log(this.sex);
}
}
class test1 extends test{
getAge(){
console.log(this.name);
console.log(this.age);
}
}
const t = new test()
const t1 = new test1()
console.log(t.name);
console.log(t.getAll());
console.log(t1.getAge());
fu
fu
20
boy
fu
20
private
只有自身类可以使用