当前位置:网站首页>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();
边栏推荐
- Write it down once Net travel management background CPU Explosion Analysis
- 因果AI,下一代可信AI的产业升级新范式?
- 2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- 2022年已过半,得抓紧
- How does the pytorch project run?
- TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
- 2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
- Supervised pre training! Another exploration of text generation!
- 类的基础语法
猜你喜欢
In Net 6 project using startup cs
Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
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
MPLS setup experiment
Makefile demo
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
What can learning pytorch do?
How to download pytorch? Where can I download pytorch?
Five elements of user experience
【刷题篇】 找出第 K 小的数对距离
随机推荐
深潜Kotlin协程(十九):Flow 概述
The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units
Analysis of the reason why the server cannot connect remotely
Arduino application development - LCD display GIF dynamic diagram
[learning notes] seckill - seckill project - (11) project summary
[mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
C language hashtable/hashset library summary
Makefile demo
[home push IMessage] software installation virtual host rental tothebuddy delay
Basic syntax of class
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
2022deepbrainchain biweekly report no. 104 (01.16-02.15)
【毕业季·进击的技术er】职场人的自白
[mathematical logic] predicate logic (predicate logic basic equivalent | eliminate quantifier equivalent | quantifier negative equivalent | quantifier scope contraction expansion equivalent | quantifi
服务器无法远程连接原因分析
Error in compiled file: error: unmapped character encoding GBK
Write it down once Net travel management background CPU Explosion Analysis
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
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