当前位置:网站首页>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();
边栏推荐
- 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
- The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
- 2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
- [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
- [mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
- Five elements of user experience
- "Final review" 16/32-bit microprocessor (8086) basic register
- [untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions
- [brush questions] connected with rainwater (one dimension)
- 深潜Kotlin协程(二十):构建 Flow
猜你喜欢

105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode

2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =

leetcode:297. 二叉树的序列化与反序列化

【刷题篇】 找出第 K 小的数对距离
![[Apple Photo Album push] IMessage group anchor local push](/img/a7/6a27d646ecba0d7c93f8dac38492a2.jpg)
[Apple Photo Album push] IMessage group anchor local push

pytorch项目怎么跑?

Application of I2C protocol of STM32F103 (read and write EEPROM)

Is pytorch difficult to learn? How to learn pytorch well?

JS realizes lazy loading of pictures

"Final review" 16/32-bit microprocessor (8086) basic register
随机推荐
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
[mathematical logic] predicate logic (first-order predicate logic formula | example)
Appium automated testing framework
Idea shortcut keys
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
Dynamic programming: Longest palindrome substring and subsequence
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
类的基础语法
pytorch是什么?pytorch是一个软件吗?
2.14 simulation summary
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
2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
Arlo's thinking about himself
[learning notes] seckill - seckill project - (11) project summary
What is pytorch? Is pytorch a software?
树莓派如何连接WiFi
Is pytorch open source?
MPLS setup experiment
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍