(二)typescript中class类
创始人
2024-11-14 21:34:45
0

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

1. 静态属性和方法

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

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. 私有属性和方法

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

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,它们可以从类的内部、实例以及子类中自由访问。

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. 保护属性和方法

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

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. 继承

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

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 中如何利用类的特性来进行更细粒度的访问控制。这有助于构建更安全、可维护和模块化的大型应用程序。

相关内容

热门资讯

WPK透视辅助!wepoker... WPK透视辅助!wepoker有没有挂,wepoker有没有插件(其实是有挂)1、完成wepoker...
wpk透视辅助!智星菠萝有挂吗... wpk透视辅助!智星菠萝有挂吗,wepoker辅助真的假的(本来有挂);1、用户打开应用后不用登录就...
wpk透视辅助!wepoker... wpk透视辅助!wepoker私人辅助器,pokemmo修改器手机版(本来存在有挂)1)pokemm...
wpk透视辅助!hhpoker... wpk透视辅助!hhpoker透视方法,德州局透视脚本免费版下载手机版(原来存在有挂)1、德州局透视...
微扑克透视辅助!wepoker... 微扑克透视辅助!wepoker辅助真的假的,hhpoker怎么开透视(原来真的是有挂)wepoker...
微扑克透视辅助!we-poke... 微扑克透视辅助!we-poker辅助,hhpoker辅助器(一直存在有挂)hhpoker辅助器透视方...
WPK透视辅助!pokemmo... WPK透视辅助!pokemmo手机版修改器,wpk辅助(一贯真的是有挂)小薇(辅助器软件下载)致您一...
微扑克透视辅助!大菠萝手游辅助... 微扑克透视辅助!大菠萝手游辅助,hhpoker底牌透视脚本(好像存在有挂)1、hhpoker底牌透视...
WPK透视辅助!德扑之心免费透... WPK透视辅助!德扑之心免费透视,wpk俱乐部有没有辅助(本来真的是有挂)1、wpk俱乐部有没有辅助...
微扑克透视辅助!哈糖大菠萝挂,... 微扑克透视辅助!哈糖大菠萝挂,impoker辅助(竟然是有挂)1、哈糖大菠萝挂免费辅助多个强度级别选...