(二)typescript中class类

在 TypeScript 中,你可以使用类(class)来更加精确地定义属性和方法的访问控制。这些包括静态属性/方法、私有属性/方法、公共属性/方法、保护属性/方法,以及继承。下面通过示例来展示这些概念在 TypeScript 中的使用。

1. 静态属性和方法

静态属性和方法属于类本身而不是类的实例。这意味着你可以在不实例化类的情况下直接访问它们。

typescript 复制代码
class MyClass {
  static staticProperty: string = "class level property"; // 静态属性
  static staticMethod(): string { // 静态方法
    return 'I am a static method';
  }
}

console.log(MyClass.staticProperty); // 访问静态属性
console.log(MyClass.staticMethod()); // 调用静态方法

2. 私有属性和方法

私有属性和方法只能在类的内部访问,不允许从外部或任何子类中访问。

typescript 复制代码
class Example {
  private privateProperty: string = "I am private"; // 私有属性

  private privateMethod(): string { // 私有方法
    return 'This is a private method';
  }

  public getPrivateMethod(): string {
    return this.privateMethod(); // 内部访问私有方法
  }
}

const obj = new Example();
console.log(obj.getPrivateMethod()); // 正确访问
// console.log(obj.privateMethod()); // 错误,外部不能访问私有方法
// console.log(obj.privateProperty); // 错误,外部不能访问私有属性

3. 公共属性和方法

公共属性和方法是默认的访问级别,在 TypeScript 中不必显式声明为 public,它们可以从类的内部、实例以及子类中自由访问。

typescript 复制代码
class PublicExample {
  public publicProperty: string = "I am public"; // 公共属性

  public publicMethod(): string { // 公共方法
    return 'This is a public method';
  }
}

const example = new PublicExample();
console.log(example.publicProperty); // 访问公共属性
console.log(example.publicMethod()); // 调用公共方法

4. 保护属性和方法

保护属性和方法可以在类及其子类中访问,但不能从类的外部访问。

typescript 复制代码
class ProtectedExample {
  protected protectedProperty: string = "I am protected"; // 保护属性

  protected protectedMethod(): string { // 保护方法
    return 'This is a protected method';
  }
}

class ChildExample extends ProtectedExample {
  useProtectedMethod(): string {
    return this.protectedMethod(); // 子类访问保护方法
  }
}

const child = new ChildExample();
console.log(child.useProtectedMethod()); // 正确访问
// console.log(child.protectedMethod()); // 错误,外部不能访问保护方法

5. 继承

继承允许一个类从另一个类接收属性和方法,这是重用代码的有效方式。

typescript 复制代码
class Parent {
  public parentMethod(): string {
    return 'Method from Parent';
  }
}

class Child extends Parent {
  public childMethod(): string {
    return 'Method from Child';
  }
}

const childInstance = new Child();
console.log(childInstance.parentMethod()); // 子类实例访问继承的方法
console.log(childInstance.childMethod()); // 子类实例访问自己的方法

通过上述示例,你可以看到在 TypeScript 中如何利用类的特性来进行更细粒度的访问控制。这有助于构建更安全、可维护和模块化的大型应用程序。

相关推荐
hanbr1 天前
C++ 初涉
开发语言·c++
Дерек的学习记录1 天前
C++:入门基础(下)
开发语言·数据结构·c++·学习·算法·visualstudio
小小前端--可笑可笑1 天前
Vue / React 单页应用刷新 /login 无法访问问题分析
运维·前端·javascript·vue.js·nginx·react.js
云小逸1 天前
【nmap源码解析】Nmap 核心技术深度解析:从源码到实战
开发语言·网络·windows·nmap
小林敲代码77881 天前
记一次 Vue 项目首屏优化:从 7.1s 到 0.9s,深挖 Gzip 的力量
前端·javascript·vue.js
前路不黑暗@1 天前
Java项目:Java脚手架项目的公共模块的实现(二)
java·开发语言·spring boot·学习·spring cloud·maven·idea
人道领域1 天前
Spring核心注解全解析
java·开发语言·spring boot
云深麋鹿1 天前
标准库中的String类
开发语言·c++·容器
Highcharts.js1 天前
什么是向量图表?如何用 Highcharts 快速创建一个笛卡尔坐标图/矢量图?
javascript·开发文档·highcharts·图表开发·向量图·矢量图表·笛卡尔坐标图
脱离语言1 天前
Jeecg3.8.2 前端经验汇总
开发语言·前端·javascript