In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to define the use of interface interface of typeScript", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to define the use of interface interface of typeScript" article.
1. Interface definition
The role of the interface:
In object-oriented programming, interface is a normative definition, which defines behavior and action specifications.
In programming, the interface plays a role of restriction and standardization.
Interfaces are generally defined using the interface keyword, and the first letter of the name needs to be capitalized. When defining an interface in a project, the name is usually preceded by an uppercase I letter, which can quickly identify that the type is an interface. Such as:
Interface IPerson {name: string age: number sex: string}
The main function of an interface is to regulate objects that are constrained, such as:
Interface IPerson {name: string age: number sex: string} let webPerson: IPerson = {name: "Qianqian", age:18, sex:'girl'}
When using the above IPerson to define an object, all three attributes are required, and if you miss one of them, there will be an error. What if some properties and some objects have and some objects don't? Like a career.
Interface members can also be default, defined with "?", such as:
Interface IPerson {name: string age: number sex: string work?: string}
But in special cases, some people have dietary taboos, most do not, some people have special hobbies, most do not have, at this time this attribute can not be specific, you can add any attribute, use propName to define, such as:
Interface IPerson {name: string age: number sex: string work?: string [propName:string]: any} let webPerson: IPerson = {name: "Qianqian", age:18, sex: 'girl', hobby: "dance", refuse: "Don't eat mutton"}
After you add any attribute, you can add as many special attributes as you want.
2. Interface inheritance
Interfaces, like classes, can inherit from each other. You can copy members from one interface to another, and you can flexibly split the interface into reusable modules. Use the extends keyword when inheriting. Such as:
Interface IPerson {name: string age: number sex: string work?: string [propName:string]: any} interface IAdult extends IPerson {isHaveChildren: boolean isBoss: boolean} let Tom: IAdult = {isBoss: false, isHaveChildren: false, name: 'Qianqian', age: 20, sex: "girl"}
An interface can inherit one or more interfaces, and multiple interfaces are separated by commas. Such as:
Interface IPerson {name: string age: number sex: string} interface IAdult extends IPerson {isBoss: boolean} interface elderly extends IPerson, IAdult {isRetire:boolean}
The inheritance of the interface is the expansion of the interface, the interface extension is to add more constraints, an interface can extend multiple interfaces.
3. Class implementation interface
In typeScript, an interface can inherit a class so that the interface has all the members of the class, and this interface can only refer to an instance of the class or its word class. Such as:
Class Person {name:string; age: number constructor (name:string, age: number) {this.name = name this.age = age}} interface IAdult extends Person {married:boolean} let Tom: IAdult = {name: "Tom", age: 20, married: true} console.log (Tom)
When an interface inherits a class type, it inherits members of the class but does not include its implementation.
The above is the content of this article on "how to define the use of typeScript's interface interface". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.