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
只有自身类可以使用

本文详细介绍了Java中的public、protected和private修饰符的区别,以及TypeScript中public和protected的作用范围。通过具体代码示例展示了不同修饰符如何影响类成员的可访问性。
633

被折叠的 条评论
为什么被折叠?



