首页 > 其他分享 >TS高级类型-class

TS高级类型-class

时间:2024-10-18 09:52:23浏览次数:3  
标签:name TS age move 高级 class void log

创建一个class类

class Person {

     age:number //没有值但是有类型

     name="小明" //有默认值,类型推断是string

     constructor(age:number,name:string){//构造函数没有返回值,返回值不需要加类型
        this.age = age
        this.name= name
     }

//实例方法
    change(num:number):void{

    }

}

const p=new Person

类的继承 

通过 extends 继承

class Animal{
    move():void{
     console.log('走路')
    }
}
class Dog extends Animal{
    name='小狗'
}

const d=new Dog()
d.move()

通过 implements 接口继承

interface Singale{ //接口
    sing():void
}



class person implements Singale{

    sing(){
     console.log('走路')
    } 

    move():void{
     console.log('走路')
    }
}

类的修饰符

可见性修饰符:public(公有的,默认)、protected(受保护的,在所在类和子类中,非实例对象)、private(私有的)

class Animal{

    protected move():void{
     console.log('走路')
    }

    change():void{ //默认public
     console.log('改变')
    }

    private jump:void{ //私有的,只能在当前类中使用
     console.log('跳')
    }

}
class Dog extends Animal{

    name='小狗'

    this.move()//可以访问

    this.change()//可以访问
}

const a=new Animal()
const d=new Dog()

a.move()//报错

d.move()//报错

d.change()//可以访问

只读修饰符 readonly 

只允许在constructor中改变修饰属性,其他地方一律不允许修改;readonly 只能修饰属性不能修饰方法;

readonly 修饰的属性没有类型的时候,则变成字面量类型,任何地方不能被改变。

readonly 也可以在接口或{}中使用

interface IPerson {
readonly name:string
}

class Person implements IPerson {

    readonly age:number=18

    constructor(age:number){

    this.age=age

    }
}

标签:name,TS,age,move,高级,class,void,log
From: https://blog.csdn.net/qu19666/article/details/142997695

相关文章

  • 鸿蒙ArkTS中的资源管理详解
    在鸿蒙应用开发中,资源管理是一个非常重要的话题。ArkTS作为鸿蒙原生开发语言,提供了强大的资源管理功能。本文将深入探讨ArkTS中的资源管理,特别是$r语法的使用注意事项,以及其他实用的资源管理技巧。1.$r语法简介在ArkTS中,$r是一个用于引用资源的特殊语法。它允许开发者......
  • 初识TS-类型多种操作
    类型推论在ts中,没有明确指出类型,ts的类型推论会帮助提供类型类型断言当ts类型太宽泛的时候,可以使用类型断言 as 指定更加具体的类型constalink:HTMLElement=document.getElementById('link')alink.link?//这种写法会报错,HTMLElement类型太宽泛了正确写法//常用写......
  • spring注解解析与configurationClassPostProcessor(1)
    上个章节讲解了spring启动时解析spring.xml的流程,本章主要解析对注解的解析;目前我们常用的是AnnotationConfigApplicationContext,其中MyApp就是启动类ApplicationContextctx=newAnnotationConfigApplicationContext(MyApp.class);this()方法中可以看到reader为Annotated......
  • Android Framework AMS(08)service组件分析-2(startService和StopService关键流程分析)
    该系列文章总纲链接:专题总纲目录AndroidFramework总纲本章关键点总结&说明:说明:上一章节主要解读应用层service组件启动的2种方式startService和bindService,以及从APP层到AMS调用之间的打通。本章节主要关注service组件启动方式的一种:startService启动方式,分析关键API......
  • Citrix NetscalerStoreFront负载均衡 Load Balancing(精华)
    CitrixNetscalerStoreFront负载均衡LoadBalancing(精华)1.依次展开TrafficManagement/LoadBalancing/Servers,点add添加 在此,我的两台storefront已经全部添加进去4.依次展开TrafficManagement/LoadBalancing/Monitors,点击add添加 6.依次展开TrafficMa......
  • ContentSizeFitter
    新建在物体的Inspector面板中添加组件(AddComponent->ContentSizeFitter)演示介绍ContentSizeFitter内容大小适配器内容大小适配器充当布局控制器,可用于控制其自身布局元素的大小。查看实际自动布局系统的最简单方法是向带有文本组件的游戏对象添加内容大小适配器......
  • 【Echarts 实战指南】仪表盘接收动态数据,小白轻松上手
    ECharts仪表盘因其强大的数据可视化功能而被广泛应用于多种场景。性能监控、生产监控、设备监控等等template<template><a-cardshadow="none"style="margin:20px0020px"title=""> <divclass="item"ref="chartContainer8"><......
  • Storefront与NetScaler的集成配置 - part2
    Storefront与NetScaler的集成配置-part2前文介绍了Storefront与NetScaler配置中的StoreFront方面的配置,本章将介绍NetScaler部分的配置。1.从download.citrix.com官方网站下载最新的NetScalerGateway的。对于StoreFront来说,NetSclaer最好使用10.0e和10.1的版本(9.2不支持)。本......
  • 前端必知必会-Bootstrap 5 复选框和单选按钮
    文章目录Bootstrap5复选框和单选按钮单选按钮切换开关总结Bootstrap5复选框和单选按钮如果您希望用户从预设选项列表中选择任意数量的选项,则使用复选框。示例<divclass="form-check"><inputclass="form-check-input"type="checkbox"id="check1"name="......
  • 前端必知必会-Bootstrap 5 表单
    文章目录Bootstrap5表单堆叠表单Textarea表单行/网格(内联表单)表单控件大小禁用和只读纯文本输入颜色选择器总结Bootstrap5表单堆叠表单所有带有.form-control类的文本<input>和<textarea>元素均获得正确的表单样式:示例<formaction="/action_page.ph......