Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Interface and Class usage examples in typescript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "case Analysis of Interface and Class usage in typescript". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

For simplicity, typescript is abbreviated to ts

Interface Interface

Maybe some students are unfamiliar with the interface, and it is difficult to see it at the language level of the weakly typed language, unlike the strongly typed language, which is considered at the language level. But in the ts world, we can see it, to put it bluntly, it is only responsible for defining what is in your object, that is, structure, of course, it cannot be instantiated.

Define

So, how do you define an interface, using the keyword interface, of course?

Interface IA {name: string}

The interface IA is declared above, and there is an attribute name, which is of string type.

In this way, we can declare a variable of type IA and assign an initial value

Var a: IA = {name: 'hello'}

Inherit

Interfaces can also be inherited. If you have an IB interface that also contains the structure of IA, you can simply inherit it and extend your own properties.

Interface IB extends IA {id: number}

Class

Class is similar to an interface, but in addition to definition, it has an implementation, such as assigning a value to a variable, which can be instantiated

Define

The key word defined is class. I believe students who are familiar with es6 have long been used to it.

Class A {a: string = 'xxxxx'}

Class An is defined above, which has a string type an and an initial value of xxxxx assigned to it, so that we can use it like this, instantiate it, and reference the property a

Var a = new A () console.log (A.A)

Of course, we can also modify the attribute a.

Member access modifiers for public, private, protected, etc.

Attributes can be divided into which members and who can access them.

Public, which means anyone can access it.

Private can only be accessed by internal members. To put it bluntly, it can be accessed by its own functions.

Protected protected, except for itself, can be accessed by those who inherit this class.

The above keywords are usually added in front of the attribute. If not, it is public.

The so-called members are not limited to properties, but also include methods, also called functions, but are generally called methods in classes.

Class A {public a ='a 'private b =' b 'protected c =' c'}

Note that no type is specified for the property when declared above, which is legal because the corresponding type can be inferred from the subsequent initial value

Constructor function

When it comes to classes, there is naturally a constructor, this function is quite special, it is called when instantiating, that is, new; strongly typed languages are generally named after the class name of a function, ts is defined and implemented with constructor, of course, strictly speaking, it is stipulated in js.

In fact, the constructor is no different from the ordinary function, it can have parameters, it is implemented in the function body, you can assign the initial value to the attribute, rewrite the above A, and give it to a.

Even if you define any constructor, there will be a default constructor, but it does nothing

Class A {a: string constructor (arg: string) {this.a = arg}}

In ts, the constructor also has a magical function, which is to define and assign the initial value in the constructor parameter, without having to repeat the declaration in the class, but to assign the initial value in the constructor, as follows

Class A {constructor (public a: string)}

Above, we define a string property an in An and assign it the first parameter of the constructor.

Inherit

Similar to interfaces, class inheritance uses the keyword extends

Class B extends A {d: number = 1}

Implementation interface

A class can not only inherit an existing class, but also implement an interface. It is important to note that there must be a corresponding implementation for the properties in the interface.

The key word of the implementation is implements

Class C implements IA {name ='c'}

Of course, you can use both implementation and inheritance at the same time, which is no problem.

Class D extends An implements IA {name ='d'}

Static member

Ordinary members exist separately in each instance, while static members are shared in the class, that is, there is only one copy. Static members are declared using the keyword static

Class A {static sa = 'nnnn'}

Above, we declare a string attribute sa and assign the initial value nnnn. When you need to access it, just call it like the following

This is the end of A.sa 's "Interface and Class usage case Analysis in typescript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report