This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging
🎉 preface
- I’ve learned that before
TypeScript
But at ordinary times the business is still used aboveJavaScript
To develop leads gradually onTypeScript
Out of practice. - Take this opportunity to study together again
TypeScript
Knowledge of. - In the previous article we
TypeScript
After a review of the basic knowledge, it is not found that it is not very difficult. - This article is
TypeScript
The third part of the advanced part is about classes and interfaces. For the basic part, see the article I shared earlier - This article about
1300 +
Word, reading may take ten minutes ~ 🥂
🌋 TypeScript class implementations (implement
)
- In the previous classes we studied, each class can only inherit one class, but is there a case where different classes have common properties or methods?
- Suppose a milk tea shop wants to make a cup
Ice latte
andIce tea with milk
We all need to give itThe sugar
ice
In this case, it may not be convenient or even difficult to complete the method of class and class inheritance. At this time, can we follow the previous studyinterface
The same defines the properties of a class. - We can still choose the interface to define the shape of a class, but the connection between the class and the interface cannot be used
:
To useimplement
The implementation.- To create a
interface
Specify a shape. - Create a class implementation (
implement
) theinterface
- Class needs to include
interface
Properties and methods in
- To create a
Interface makeMilk {addIce():void addSugar():void} class MilkTea implements makeMilk {addIce() {console.log(' MilkTea implements ice '); } addSugar(){console.log(' milk tea implements makeMilk ')}} class Latte implements makeMilk {addIce() {console.log(' milk tea implements '); } addSugar(){console.log(' milk tea with sugar ')}} let milk=new MilkTea; let latte=new Latte; Milk.addice ()// Milk tea with ice milk.addsugar ()// Milk tea with sugar latt.addice ()// Milk tea with ice latt.addsugar ()// Milk tea with ice latt.addsugar ()// Milk tea with sugarCopy the code
- In this example we can see that it takes to make both a milky tea and a latte
The sugar
ice
This operation, and this operation is defined ininterface
In, we can go throughclassimplementation
interfaceTo get these two methods. - And then two more
Milk tea class
andLatte class
To instantiate the method, the instance object can use these methods - Of course a class will do
implementation
Multiple interfacesinterface
interface Ice { addIce():void } interface Sugar { addSugar():void } class MilkTea implements Ice,Sugar { addIce() { Console. log(' milk tea with ice '); } addSugar(){console.log(' milk tea with sugar ')}} let milk=new MilkTea; Milk.addice ()// Milk tea with iceCopy the code
- So here’s one
Milk tea class
At the same timeice
The sugar
The interface.
🗻 TypeScript interfaces inherit interfaces
- Just like a class inherits a class, the same object can inevitably inherit, interface can also inherit interface, connection mode is also used
extends
.
Interface addSugar extends addIce{sugar:string} interface addSugar extends addIce{sugar:string} let milk:addSugar={ice:' less ice ', Sugar :' half-sugar '}Copy the code
- The two interfaces defined in the above example are
addIce
addSugar
And theaddSugar
inheritedaddIce
theice
Property, so in useaddSugar
The shapes of interfaces must be consistent. - in
milk
Variable because of the use ofaddSugar
Interface, so an error will be reported if the property does not fit the shape.
🏖️ TypeScript interfaces inherit classes
- in
TypeScript
We can use interfaces to inherit classes.
Class MilkTea {ice:string sugar:string constructor(ice:string,sugar:string){//constructor is a constructor, To receive the this.ice = ice; this.sugar = sugar; }; } interface doSthing extends MilkTea{} let milk:doSthing={ice:' ice ', sugar:' half-sugar '}Copy the code
- In the example above, our
interface
The interface specifies the shape, which itself has nothing, but it inheritsMilkTea
Of the classice
andsugar
Property, so the use of this interface is specifiedinterface
The variable of theta needs to beice
andsugar
Properties. - As we did
milk
I gave it an interfacedoSthing
, so this variable needs to haveice
andsugar
Properties. - Some students will ask:Well, interfaces and classes are not the same thing, so how can we inherit?Although these two are not the same thing, we are defining one
class
When you define a type, you define a type. - Just take the top one
MilkTea
Class, when we create this class we create a classMilkTea
Type of, we can use as follows.
Class MilkTea {ice:string sugar:string constructor(ice:string,sugar:string){//constructor is a constructor, To receive the this.ice = ice; this.sugar = sugar; }; } let milk:MilkTea={ice:' ice ', sugar:' half sugar '}Copy the code
- There’s no problem with that, so when we
doSthing
Interfaces are actually integrated when they inheritMilkTea
This type, and this type is familiar to you, he isinterface
So if we inherit a class we inherit an interface. - It is worth mentioning that when an interface inherits from a class, it only inherits its instance properties and methods, but not its constructor.
👋 at the end
- This is for the record
TypeScript
I will continue to outputTypeScript
Related knowledge, you can learn together. - If you think this article is helpful to your words might as well 🍉 attention + thumbs up + favorites + comments + forwarding 🍉 support yo ~~😛
🌅 past wonderful
Getting Started with TypeScript basics (3)🎯
Getting Started with TypeScript Basics (IV)🎯
“TypeScript” Getting Started (1)✈️
“TypeScript” Getting Started (2)✈️