当前位置:网站首页>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();
边栏推荐
- [mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
- 在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
- golang xxx. Go code template
- Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
- How to download pytorch? Where can I download pytorch?
- [home push IMessage] software installation virtual host rental tothebuddy delay
- QSAR model establishment script based on pytoch and rdkit
- How to connect WiFi with raspberry pie
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
- 『期末复习』16/32位微处理器(8086)基本寄存器
猜你喜欢
在 .NET 6 项目中使用 Startup.cs
竞品分析撰写
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
Arduino application development - LCD display GIF dynamic diagram
IPv6 foundation construction experiment
『期末复习』16/32位微处理器(8086)基本寄存器
Is pytorch open source?
Bisher - based on SSM pet adoption center
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
How does the pytorch project run?
随机推荐
[set theory] set concept and relationship (set family | set family examples | multiple sets)
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
[daily question] dichotomy - find a single dog (Bushi)
QSAR model establishment script based on pytoch and rdkit
eth入门之简介
Makefile demo
【刷题篇】 找出第 K 小的数对距离
js/ts底层实现双击事件
pytorch怎么下载?pytorch在哪里下载?
Supervised pre training! Another exploration of text generation!
2022年已过半,得抓紧
[brush questions] most elements (super water king problem)
What is pytorch? Is pytorch a software?
China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
CVPR 2022 | Dalian Technology propose un cadre d'éclairage auto - étalonné pour l'amélioration de l'image de faible luminosité de la scène réelle
pytorch项目怎么跑?
The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
[set theory] set concept and relationship (set represents | number set | set relationship | contains | equality | set relationship property)
Is pytorch open source?