|
|
爱跑步的电影票 · TypeScript: ...· 1 月前 · |
|
|
强健的苹果 · How to Parse JSON ...· 1 月前 · |
|
|
不要命的大海 · JSON Type | ...· 1 月前 · |
|
|
酒量大的香烟 · How to parse JSON ...· 1 月前 · |
|
|
飘逸的鞭炮 · Using with TypeScript ...· 1 月前 · |
|
|
热心肠的匕首 · 資策會數位轉型研究院─FIND中心· 3 月前 · |
|
|
留胡子的电影票 · 讯飞智能办公本Air ...· 1 年前 · |
|
|
细心的南瓜 · 转发2024年度国家自然科学基金外国学者研究 ...· 1 年前 · |
|
|
很酷的大蒜 · 2019年海外优秀学者授课项目-复旦大学经济学院· 1 年前 · |
|
|
没有腹肌的山寨机 · 高新区组织召开食品药品安全与高质量发展委员会 ...· 1 年前 · |
在typescript中有没有办法创建匿名类?
我的代码:
export abstract class Runnable {
public abstract run();
}
我正试着做这样的事情:
new Runnable {
runner() {
//implement
}
我该怎么做呢?
发布于 2019-11-09 04:00:10
好的,这里就是路。
抽象类:
export abstract class Runnable {
public abstract run();
}
一个匿名实现:
const runnable = new class extends Runnable {
run() {
// implement here
}();
发布于 2017-03-13 23:15:28
不完全是,但您可以这样做:
abstract class Runnable {
public abstract run();
let runnable = new (class MyRunnable extends Runnable {
run() {
console.log("running...");
})();
runnable.run(); // running...
然而,这种方法的问题在于,解释器在每次使用类时都会对其进行评估,这与编译器只对其进行一次评估的编译语言(如java)不同。
发布于 2017-03-14 01:26:41
如何创建匿名类?
假设您有一个接口
Runnable
和一个抽象类
Task
.when,您在typescript中声明了一个类
Foo
,您实际创建了
Foo
的一个类实例&
Foo
类的构造函数.you可能希望查看
typescript
.Anonymous类中的深度,它被引用为可以使用
new
关键字创建的构造函数,如
{new(...args):type}
。
interface Runnable {
run(): void;
abstract class Task {
constructor(readonly name: string) {
abstract run(): void;
}
创建匿名类通过
class extends ?
扩展超类
test('anonymous class extends superclass by `class extends ?`', () => {
let stub = jest.fn();
let AntTask: {new(name: string): Task} = class extends Task {
//anonymous class auto inherit its superclass constructor if you don't declare a constructor here.
run() {
stub();
let antTask: Task = new AntTask("ant");
antTask.run();
expect(stub).toHaveBeenCalled();
expect(antTask instanceof Task).toBe(true);
expect(antTask.name).toBe("ant");
});
创建匿名类通过
class ?
实现接口/类型。
test('anonymous class implements interface by `class ?`', () => {
let stub = jest.fn();
let TestRunner: {new(): Runnable} = class {
run = stub