当前位置:网站首页>Interface in TS
Interface in TS
2022-07-03 04:08:00 【Next to the left hand is the right hand】
Interface
Interface understanding
An interface is the state of an object ( attribute ) And behavior ( Method ) An abstract description of ; It's a norm , constraint , Ability
interface Iperson {
readonly name: string; // name Is a read-only property
age: number;
sex?: string; // sex It's an optional property
}
const introduce = function (person: Iperson) {
if (person.age < 18) {
person.age = 20;
}
person.sex = "male";
return person;
};
console.log(introduce({ name: " Zhang San ", age: 16 }));
Variables cannot be modified and used const , Attribute cannot be modified and used readonly
Interfaces can inherit from other interfaces
interface IStr{
str:string
}
interface INum{
num:number
}
interface Icom extends IStr,INum{}
application
Constraints on normal objects
interface IPerson{
name:string;
age:number
gender:string
}
const person:IPerson={name:"luca",age:20,gender:"male"}
Use interfaces to represent function types
To represent function types using interfaces , We need to define a call signature for the interface ; It's like a function definition that specifies the types of parameters and return values . Each parameter in the parameter list needs a name and type
Define an interface , Specify the parameter type and return value type of the function
interface IFun {
(str1: string, str2: string): boolean;
}
const isExist: IFun = function (str1: string, str2: string) {
return str1.includes(str2);
};
isExist(" I will kill you ", " volume ");
Class implementation interface ( Use interface constraint classes )
Class can implement an interface , You can also implement multiple interfaces ( A class is constrained by multiple interfaces ), The content in the interface needs to be really implemented ( Functions in the class need
It should be realized 、 There are content )
interface IFly {
fly(); // This method has no implementation
}
// Define a class , Use IFly Interface to constrain this class
class Person implements IFly {
fly() {
console.log("i belive i can fly"); // Implementation of interface content
}
}
const person = new Person();
person.fly();
interface IBelieve {
believe();
}
interface several extends IFly, IBelieve {}
// class Person2 implements IFly, IBelieve {
class Person2 implements several {
believe() {
console.log("i believe what i believe");
}
fly() {
console.log("i can fly");
}
}
const person2 = new Person2();
person2.fly();
person2.believe();
边栏推荐
- JMeter starts from zero (III) -- simple use of regular expressions
- nodejs基础:浅聊url和querystring模块
- [brush questions] most elements (super water king problem)
- Write it down once Net travel management background CPU Explosion Analysis
- Interaction free shell programming
- 2022 polymerization process examination questions and polymerization process examination skills
- 【毕业季·进击的技术er】职场人的自白
- Deep dive kotlin synergy (19): flow overview
- What is the correct way to compare ntext columns with constant values- What's the right way to compare an NTEXT column with a constant value?
- When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker
猜你喜欢

nodejs基础:浅聊url和querystring模块

Role of JS No

Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
![[brush questions] connected with rainwater (one dimension)](/img/21/318fcb444b17be887562f4a9c1fac2.png)
[brush questions] connected with rainwater (one dimension)

有监督预训练!文本生成又一探索!

CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强

Basic MySQL operations

js实现在可视区内,文字图片动画效果

pytorch是什么?pytorch是一个软件吗?

The latest activation free version of Omni toolbox
随机推荐
Is pytorch difficult to learn? How to learn pytorch well?
Supervised pre training! Another exploration of text generation!
QSAR model establishment script based on pytoch and rdkit
The 10th China Cloud Computing Conference · China Station: looking forward to the trend of science and technology in the next decade
JS realizes lazy loading of pictures
leetcode:297. 二叉树的序列化与反序列化
CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强
以两列的瀑布流为例,我们应该怎么构建每一列的数组
[NLP]—sparse neural network最新工作简述
[Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard
MySQL create table
深潜Kotlin协程(二十):构建 Flow
Taking two column waterfall flow as an example, how should we build an array of each column
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
Analysis of the reason why the server cannot connect remotely
[mathematical logic] predicate logic (judge whether the first-order predicate logic formula is true or false | explain | example | predicate logic formula type | forever true | forever false | satisfi
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数