typescript 如何在一个类中实现多个接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46075181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:52:12  来源:igfitidea点击:

How do I implement multiple interfaces in a class?

typescript

提问by Kewin Dousse

I want to implement both the interfaces in the generic class. How do I do that?

我想在泛型类中实现这两个接口。我怎么做?

interface first {
    name: string,
    age: number
}

interface second {
    product: string,
    available: boolean,
    amount: number
}

class generics <T>{

}

回答by Kewin Dousse

Use a ,to separate the interfaces you want to implement. That gives the following class declaration :

使用 a,分隔要实现的接口。这给出了以下类声明:

class generics <T> implements first, second

Here is the complete code :

这是完整的代码:

interface first {
    name: string,
    age: number
}

interface second {
    product: string,
    available: boolean,
    amount: number
}

class generics <T> implements first, second {
    name: string;
    age: number;
    product: string;
    available: boolean;
    amount: number;
}

Playground link

游乐场链接

回答by Joshua Samuel

create a specific type in multiple classes with interface.

在具有接口的多个类中创建特定类型。

    interface Calculated{
        calculate(input1: number,input2: number): void;
     }

     class Add implements Calculated{ //addition class
         calculate(input1: number, input2: number){
             console.log("The Addition of" + " " + input1 + "+" + input2 + "=" + (input1 + input2));

         }
     }

     class Subtract implements Calculated{ //subtraction class
         calculate(input1: number, input2: number){
             console.log("The subtraction of" + " " + input1 + "-" + input2 + "=" + (input1 - input2));
         }
     }

     class Mulitiple implements Calculated{ //multiplication class
         calculate(input1: number, input2: number){
             console.log("The multiplication of" + " " + input1 + "*" + input2 + "=" + (input1 * input2));
         }
     }

     class Divide implements Calculated{ //division class
         calculate(input1: number, input2: number){
             console.log("The division of" + " " + input1 + "/" + input2 + "=" + (input1 / input2));
         }
     }  

     class Calculator{ //calculator 
         input1: number;
         input2: number;
         condition: Calculated; //interface type specification

         constructor (input1: number, input2: number, condition: Calculated) {
            this.input1 = input1;
            this.input2 = input2;
            this.condition = condition;  

            const operateObj = this.condition;
            operateObj.calculate(this.input1,this.input2);       
         }
     }

     //calculator calling....

     const calculatorObj = new Calculator(100, 75.0,new Add); //(it should be input1 & input2 & like to you perform)

     // if you need addition use Add
     // if you need subtraction use Subtract
     // if you need multiplication use Mulitiple
     // if you need division use Divide