当前位置:网站首页>TS interface
TS interface
2022-07-25 23:21:00 【Intern Sen】
TS Interface
An interface is a declaration of a series of abstract methods , Is a collection of method features .
These methods should be abstract , It needs to be implemented by specific classes , Let specific classes execute specific methods .
1. Simple interface instance
// Define an interface User
interface User {
name: string,
age: number,
sing: ()=>string
}
// Define a variable xiaoXu
let xiaoXu: User = {
name: " Xiao Xu ",
age: 18,
sing: ():string => " Xu Qianjin "
}
console.log(xiaoXu.name); // Xiao Xu
console.log(xiaoXu.age); //18
console.log(xiaoXu.sing()); // Xu Qianjin
2. Union types and interfaces
interface RunOptions {
program:string;
commandline:string[]|string|(()=>string);
}
// commandline Is string
var options:RunOptions = {
program:"test1",
commandline:"Hello"
};
console.log(options.commandline)
// commandline It's an array of strings
options = {
program:"test1",
commandline:["Hello","World"]
};
console.log(options.commandline[0]);
console.log(options.commandline[1]);
// commandline It's a function expression
options = {
program:"test1",
commandline:()=>{
return "**Hello World**";}
};
var fn:any = options.commandline;
console.log(fn());
3. Interfaces and arrays
The index value and element of the array can be set to different types in the interface , The index value can be a number or a string .
Set element to string type :
interface namelist {
[index:number]:string
}
var list1:namelist = ["Google","Runoob","Taobao"]; // The same type , correct
var list2:namelist = ["Runoob",1,"Taobao"]; // Error element 1 No string type
4. Interface inheritance
Interface inheritance means that an interface can extend itself through other interfaces .
TS Allow interfaces to inherit multiple interfaces , Use keywords extends.
4.1 Single interface inheritance
interface Person {
age:number
}
interface Musician extends Person {
instrument:string
}
var drummer = <Musician>{
};
drummer.age = 27
drummer.instrument = "Drums"
console.log(" Age : "+drummer.age)
console.log(" A favorite musical instrument : "+drummer.instrument)
4.2 Multi interface inheritance
interface IParent1 {
v1:number
}
interface IParent2 {
v2:number
}
interface Child extends IParent1, IParent2 {
}
var Iobj:Child = {
v1:12, v2:23}
console.log("value 1: "+Iobj.v1+" value 2: "+Iobj.v2)
边栏推荐
- Longitude and latitude and its transformation with coordinate system
- serialization and deserialization
- File contains vulnerability
- BI 系统中为什么会有很多快照表?
- 5 ROS仿真建模(3- rviz+gazebo+控制仿真机器人)
- WordPress controls the minimum and maximum number of words of article comments
- Redis过期键的删除策略[通俗易懂]
- Mongodb features, differences with MySQL, and application scenarios
- Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
- Hj9 extract non duplicate integers
猜你喜欢

类和对象(2)(6个默认成员函数)

WordPress removes the website publishing time

Network Security Learning notes-1 file upload

WebMvcConfigurationSupport

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

firewall 命令简单操作
![[interface performance optimization] reasons for index failure and how to optimize SQL](/img/b9/64058c823c4497ac36bfb62a101816.jpg)
[interface performance optimization] reasons for index failure and how to optimize SQL

Data broker understanding

The fifth article in the series of radar Fundamentals: the function of radar modulation style

通用分页功能
随机推荐
POI special effects Market Research
General paging function
Overview of MES system equipment management (Part 2)
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
[QNX hypervisor 2.2 user manual]9.6 GDB
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
@Autowired annotation required attribute
MES系统设备管理概述(下)
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
Bind class style and bind style style
Custom MVC principle
学习探索-波浪
Design of Butterworth filter and drawing of amplitude frequency characteristic curve
MathType安装和解决不能Crtl+V的问题
Expression of directional signal -- complex exponential signal
Data filtering of MATLAB
CTS test method "suggestions collection"
2021-09-30
CSV generated by PHP cannot completely display the number with leading 0
Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)