ts 类中的修饰符

一、修饰符是什么

  • 修饰符:类中的 成员的修饰符,主要是描述类中的成员(属性构造方法方法)的 可访问性
  • 类中的成员都有自己的 默认修饰符 public
  • 修饰符有哪几种?
    • public公共的,任何位置都可以访问
    • private私有的,只有 自身内部可以访问,外部、子类中都无法访问该成员数据
    • protected受保护的自身内部和子类中 可以访问,外部无法访问
    • readonly只读,仅可在构造函数中修改
    • static静态的,修饰属性普通方法(非构造函数)
      • 修饰后被称为 静态成员
      • 静态成员 在使用时通过 类名.静态成员 这种语法来调用的,而不是通过 this实例(实例化对象)

二、通过例子来理解以上用法

1、public

外部、内部、子类内部 都可访问

// 定义一个类
class Person {
  public name: string
  constructor(name: string) {
    this.name = name
  }
  eat () {
    // 内部访问
    console.log('eat', this.name)
  }
}

// 子类
class Student extends Person {
  constructor (name: string) {
    super(name)
  }

  play () {
    // 子类内部访问
    console.log('play', this.name)
  }
}

const person = new Person('小明')
// 内部,可访问
person.eat()
// 外部,可访问
console.log('person.name', person.name)

const stu = new Student('小红')
// 子类内部,可访问
stu.eat()
stu.play()
// 外部,可访问
console.log('stu.name', stu.name)

2、private

只有内部 可访问,外部、子类内部都不可访问

// 定义一个类
class Person {
  private name: string
  constructor(name: string) {
    this.name = name
  }
  eat () {
    // 内部访问
    console.log('eat', this.name)
  }
}
// 子类
class Student extends Person {
  constructor (name: string) {
    super(name)
  }
  play () {
    // 子类内部,不可访问
    console.log('play', this.name)
  }
}

// name 为 private 时,只有内部可访问,外部、子类内部都不可访问
const person2 = new Person('小明')
// 内部,可访问
person2.eat()
// 外部:不可访问
console.log('person2.name', person2.name)

const stu2 = new Student('小红')
// 子类内部,不可访问
stu2.eat()
stu2.play()
// 外部:不可访问
console.log('stu2.name', stu2.name)

3、protected

内部子类内部 可访问,外部不可访问

// 定义一个类
class Person {
 protected name: string
 constructor(name: string) {
   this.name = name
 }
 eat () {
   // 内部访问
   console.log('eat', this.name)
 }
}
// 子类
class Student extends Person {
 constructor (name: string) {
   super(name)
 }
 play () {
   // 子类内部, 可访问
   console.log('play', this.name)
 }
}

const person3 = new Person('小明')
// 内部,可访问
person3.eat()
// 外部:不可访问
console.log('person3.name', person3.name)

const stu3 = new Student('小红')
// 子类内部,可访问
stu3.eat()
stu3.play()
// 外部:不可访问
console.log('stu2.name', stu3.name)

4、readonly

只读,仅在 构造函数 中可以被修改

class Person {
  // 可读可写
  name: string

  // 只读
  readonly sex: string
  readonly age: number = 16

  constructor (name: string, sex: string) {
    // 构造函数中,可以修改被设置为 readonly 的属性
    this.name = name
    this.sex = sex
    this.age = 18
  }

  sayName () {
    // 可读可写
    this.name = '猪猪'
  }

  saySex () {
    // 类中的普通方法,不能修改被设置为 readonly 的属性(编译报错)
    // this.sex = '中性'
  }
}

5、static

修饰 属性普通方法,使用时通过 类名 调用

class Person {
  // 普通属性
  nameOwn: string
  // 静态属性
  static gender: string = '女'

  // 构造函数前不能添加 static
  constructor (name: string) {
    this.nameOwn = name

    // 访问静态属性,需要通过 【类名.属性】访问
    console.log(this.nameOwn, Person.gender)
  }

  // 普通方法
  sayHi () {
    console.log('hello你们好')
  }

  // 静态方法
  static sayStatic () {
    console.log(Person.gender)
  }
}
// 实例化对象
const per = new Person('佩奇')
// 访问普通方法,通过实例
per.sayHi()
// 访问静态方法,要通过 【类名.方法】访问
Person.sayStatic()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeScript中类的修饰符有以下几种: 1. public public是默认修饰符,即不写修饰符时默认为public。public修饰的属性或方法可以在类的内部和外部使用。 2. private private修饰的属性或方法只能在类的内部使用,无法在类的外部使用。 3. protected protected修饰的属性或方法可以在类的内部和子类中使用,但无法在类的外部使用。 4. readonly readonly修饰的属性只能在初始化时或构造函数中赋值,之后就无法再修改。 示例代码: ```typescript class Person { public name: string; // 公共属性 private age: number; // 私有属性 protected gender: string; // 保护属性 readonly id: number; // 只读属性 constructor(name: string, age: number, gender: string, id: number) { this.name = name; this.age = age; this.gender = gender; this.id = id; } public sayHello() { // 公共方法 console.log(`Hello, my name is ${this.name}.`); } private getAge() { // 私有方法 return this.age; } protected getGender() { // 保护方法 return this.gender; } } class Student extends Person { public getGenderAndAge() { console.log(`My gender is ${this.getGender()} and age is ${this.getAge()}.`); } } const person = new Person('Tom', 18, 'male', 1001); console.log(person.name); // Tom // console.log(person.age); // 无法访问 // console.log(person.gender); // 无法访问 // person.id = 1002; // 无法修改 person.sayHello(); // Hello, my name is Tom. const student = new Student('Jerry', 17, 'female', 1002); // console.log(student.age); // 无法访问 // console.log(student.gender); // 无法访问 console.log(student.name); // Jerry console.log(student.id); // 1002 student.getGenderAndAge(); // My gender is female and age is undefined. ``` 在上面的示例代码中,我们定义了一个Person类,其中包含了公共属性、私有属性、保护属性、只读属性、公共方法、私有方法和保护方法。这些属性和方法都有不同的修饰符,可以在不同的场景下使用。同时,我们还定义了一个Student类,继承自Person类,可以访问Person类中的保护属性和保护方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值