public理解 当你在程序中没有指明修饰符时,默认为public,也就是在类内类外都可以访问,我们以下面的例子来解释。 class Person{ name:string sex:string ag
class Person{
name:string
sex:string
age:number // 默认设置为public
constructor(name:string,sex:string,age:number){
this.name = name
this.sex = sex
this.age = age
}
show_name():void{
console.log('类内函数输出的:',this.name)
}
}
let lcs = new Person('lcs','man',21)
console.log('类外输出的:',lcs.name)
lcs.show_name()
class Person{
private name:string // 这里将name改为了private
sex:string
age:number
constructor(name:string,this.name)
}
}
class Teacher extends Person{
salary: number
constructor(name:string,age:number,salary:number){
super(name,sex,age)
this.salary = salary
}
show():void{
console.log('继承类内输出的:',this.name,this.salary.toString())
}
}
let lcs = new Person('lcs',21)
let t = new Teacher('lcs',21,31232)
t.show()
console.log('类外输出的:',lcs.name)
lcs.show_name()
class Person{
protected name:string // 将name改为protected
sex:string
age:number
constructor(name:string,lcs.name)
lcs.show_name()