什么是 constructor signature in interface

简介: 什么是 constructor signature in interface

接口中的 constructor signature 不能在类中实现; 它们仅用于定义定义 newable 的现有 JS API. 下面是一个例子:

interface ComesFromString {
    name: string;
}
意思是这个接口代表一个可以使用 `new` 操作符操作的对象。返回的类型是 ComesFromString
interface StringConstructable {
    new(n: string): ComesFromString;
}
class MadeFromString implements ComesFromString {
    constructor (public name: string) {
        console.log('ctor invoked');
    }
}
// 下面函数定义了一个工厂方法。工厂方法的输入就是之前定义的 constructor signature
function makeObj(n: StringConstructable) {
    return new n('hello!');
}
console.log(makeObj(MadeFromString).name);

执行结果:

60.png

以上例子实际上为 makeObj 的函数调用创建了一个 constraint,传入的输入参数必须可以被 new 操作施加,并且构造函数仅包含一个输入参数,类型为 string.

下列代码会引起编译错误:

class Other implements ComesFromString {
    constructor (public name: string, count: number) {
    }
}
makeObj(Other); 

image.png

Argument of type ‘typeof Other’ is not assignable to parameter of type ‘StringConstructable’.(2345)


添加问号将其设置为 optional 参数后,问题消失:

61.png

具有构造签名的接口并不意味着由任何类实现(乍一看,这对于一些具有 C#/Java 背景的开发人员来说可能看起来很奇怪,但这确实是另一种不同的设计思路)。


暂时把它想象成一个带有调用签名的接口(就像Java世界中的@FunctionalInterface)。 它的目的是描述一种函数类型。所描述的签名应该由函数对象满足。但不仅仅是任何高级函数或方法。 它应该是一个知道如何构造对象的函数,一个在使用 new 关键字时被调用的函数。


因此,带有构造签名的接口定义了构造函数的签名。如上图我举过的例子,makeObj 函数只接受构造函数仅仅包含唯一一个输入参数且数据类型为 string.

例子:

interface ClassicInterface { // old school interface like in C#/Java
    method1():string;
    methodN():string;
}
interface Factory { //knows how to construct an object
    // NOTE: pay attention to the return type
    new (myNumberParam: number, myStringParam: string): ClassicInterface
}
class MyImplementation implements ClassicInterface {
    // The constructor looks like the signature described in Factory
    constructor(num: number, s: string) { 
      console.log('in myImplementation:', num, s);
    } // obviously returns an instance of ClassicInterface
    method1() {
      return '1';
    }
    methodN() {
      return '2';
    }
}
class MyOtherImplementation implements ClassicInterface {
    // The constructor looks like the signature described in Factory
    constructor(n: number, s: string) { 
      console.log('in myOtherImplementation:', n, s);
    } // obviously returns an instance of ClassicInterface
    method1() {
      return '3';
    }
    methodN() {
      return '4';
    }
}
// And here is the polymorphism of construction
function instantiateClassicInterface(ctor: Factory, myNumberParam: number, myStringParam: string): ClassicInterface {
    return new ctor(myNumberParam, myStringParam);
}
// And this is how we do it
let iWantTheFirstImpl = instantiateClassicInterface(MyImplementation, 3.14, "smile");
let iWantTheSecondImpl = instantiateClassicInterface(MyOtherImplementation, 42, "vafli");
console.log('done');

输出:

62.png


目录
相关文章
|
8月前
|
Cloud Native Java Go
关于 interface{} 会有啥注意事项?上
关于 interface{} 会有啥注意事项?上
|
2月前
FeignClient【问题】Cannot deserialize value of type``from Object value (token `JsonToken.START_OBJECT`)
FeignClient【问题】Cannot deserialize value of type``from Object value (token `JsonToken.START_OBJECT`)
404 0
|
8月前
|
Cloud Native Go
关于 interface{} 会有啥注意事项?下
关于 interface{} 会有啥注意事项?下
|
9月前
|
JavaScript Java C#
什么是 constructor signature in interface
什么是 constructor signature in interface
|
IDE Java 开发工具
Lombok - Generating equals/hashCode implementation but without a call to superclass
Lombok - Generating equals/hashCode implementation but without a call to superclass
1058 0
|
Java Android开发
The method call() of type XXX must override a superclass
The method call() of type XXX must override a superclass
87 0
Object static methods
Object static methods
56 0
|
Go PHP
【GO】panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
【GO】panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
434 0
【GO】panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
|
Android开发 Kotlin
【错误记录】Kotlin 编译报错 ( Class ‘Xxx‘ is not abstract and does not implement abstract member )
【错误记录】Kotlin 编译报错 ( Class ‘Xxx‘ is not abstract and does not implement abstract member )
656 0
【错误记录】Kotlin 编译报错 ( Class ‘Xxx‘ is not abstract and does not implement abstract member )
|
Java 编译器
Java 9 Private Interface Methods
Java 9 Private Interface Methods