当前位置: 首页 > news >正文

ts中interface自定义结构约束和对类的约束

一、interface自定义结构约束对后端接口返回数据

// interface自定义结构 一般用于较复杂的结构数据类型限制 如后端返回的接口数据// 首字母大写;用分割号隔开
interface Iobj{a:number;b:string
}
let obj:Iobj = {a:1,b:'2'
}// 复杂类型 模拟后端返回的接口数据
interface Ilist{list:{id:number;name:string}[]
}
interface Idata{message:string;ok:number;data:Ilist
}
let data:Idata = {message:'成功',ok:200,data:{list:[{id:1,name:'张三'},{id:1,name:'张三'}]}
}

二、接口请求参数限制

 

三、继承 

interface Iab{OK:number;name:string
}
interface Ia extends Iab{children:[]
}
let AB:Ia = {OK:200,name:'zhang',children:[]}

四、类中的使用

1,类的写法(必须限制useName,useAge的类型,否则报错

class Person{// 必须限制useName,useAge的类型,否则报错useName:string;useAge:numberconstructor(name,age){this.useName = namethis.useAge = age}run():string{return this.useName}
}
let p1 = new Person('张三',18)

2、修饰符

2-1、readonly只读

interface Obj1{readonly num:number;name:string
}
let obj1:Obj1 = {num:1,name:'lisi'
}
obj1.name='zhangsan'
// obj1.num = 12 不能修改num 只读 

2-2、public(默认值  公开的  都能访问)

class Person{public useName:string;useAge:numberconstructor(name,age){this.useName = namethis.useAge = age}run():string{return this.useName}
}
let p1 = new Person('张三',18)
console.log(p1.useName,p1.useAge) 都能访问其中属性

2-3、protected (受保护的,只能在当前类和当前类的子类内部使用)

class Person{useName:string;protected useAge:numberconstructor(name,age){this.useName = namethis.useAge = age}run():string{return this.useName}}let p1 = new Person('张三',18)
console.log(p1.useName,p1.run()) p1.useName,p1.run都能访问,不能访问p1.useAge只能在当前类的子类内部使用
class Person{protected useName:string;run():string{return this.useName}
}class Children extends Person{run(){return this.useName;}}let p2 = new Children();console.log(p2.run()) p2.run()能访问 p2.useName却不能访问

2-4、private (私有的 只能在当前类的内部使用)

class Person{private useName:string;useAge:number;run():string{return this.useName}sun(){return this.useAge}
}class Children extends Person{// run(){//     return this.useName;//    } 私密的  子类不能继承父级的run方法sun(){return this.useAge} //没加private 可以继承父级的sun方法}let p2 = new Children();

五、抽象类 abstract

1,不完成具体功能

2,不能new

3,可以继承,如果继承就必须完成类中的抽象方法

class Person{run(){return 11}
}
// 如果使用抽象类 该run方法就不能有返回值 就只能按如下写
abstract class Person{abstract run():void}
//  可以继承 必须完成类中的抽象方法
abstract class Person{abstract run():void}class Child extends Person{run(): void {}
}

六、implements (对类的约束 可以累加)

interface Is1{name:string
}
interface Is2{age:number
}class Person implements Is1,Is2{name:string;age:number
}

http://www.lryc.cn/news/115316.html

相关文章:

  • Oracle单实例升级补丁
  • 力扣初级算法(二分查找)
  • 探索未来:直播实时美颜SDK在增强现实(AR)直播中的前景
  • SQL 单行子查询 、多行子查询、单行函数、聚合函数 IN 、ANY 、SOME 、ALL
  • 【第一阶段】kotlin的range表达式
  • 网络防御(5)
  • gradle 命令行单元测试执行问题
  • 剑指Offer12.矩阵中的路径 C++
  • 金鸣识别将无表格线的图片转为excel的几个常用方案
  • 刚刚更新win11,记事本消失怎么处理?你需要注意些什么?
  • 【QT】 QTabWidgetQTabBar控件样式设计(QSS)
  • 【个人记录】CentOS7 编译安装最新版本Git
  • 【Linux】计算机网络的背景和协议分层
  • 代理模式:静态代理+JDK/CGLIB 动态代理
  • gps虚拟定位 AnyGo for Mac 中文
  • LLM reasoners 入门实验 24点游戏
  • 【LeetCode 算法】Maximum Absolute Sum of Any Subarray 任意子数组和的绝对值的最大值-前缀和
  • 怎么建立大型语言模型
  • docker简介和安装
  • 记录问题: servlet获取项目包绝对路径
  • C语言文件操作基本方法
  • SQL 相关子查询 和 不相关子查询、Exists 、Not Exists、 多表连接(包含自连接)
  • 项目规范 编写规范(范例)
  • MongoDB数据库操作及操作命令
  • Linux命令(62)之tee
  • 搭建Repo服务器
  • 安卓:MMKV——键值存储库
  • 使用Python将图像转换为PDF:一次性解决您的批量转换需求
  • Vue——webpack
  • springboot房地产管理java购房租房二手房j客户sp源代码mysql