- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
export class Base {
static getSomething():typeof this //<-- change this?
{
var type = typeof this;
return new this['type']();
}
}
export class Foo extends Base {
publicVar:string = 'getThis!';
}
var foo:Foo = Foo.getSomething();
var success:string = foo.publicVar;
上面的代码返回一个错误,因为编译器说 Foo.getSomething 将返回一个 Base,而不是一个 Foo。我想知道是否有另一种方法可以让我调用编译器知道会返回 Foo 的 Foo 静态函数。
当然我可以在 Foo 中显式实现它,因此在每个扩展 Base 的类中,或者我可以简单地对其进行类型转换:
var foo:Foo = <Foo> Foo.getSomething();
但我想知道是否有一种方法可以不必做这些事情,因为我会很多
最佳答案
如果遵循 Liskov 替换原则,则不需要类型断言。
代码示例 1 - 可替换对象...
module Test {
export class Base {
publicVar: string = 'base';
static getSomething() : Base {
//... read on...
}
}
export class Foo extends Base {
publicVar:string = 'getThis!';
}
}
// No <Foo> type assertion needed
var foo: Test.Foo = Test.Foo.getSomething();
alert(foo.publicVar);
或者,您可以创建一个接口(interface)
,告诉您返回的对象将具有 publicVar 属性并返回该...
代码示例 2 - 接口(interface)
module Test {
export interface IPublicVarable {
publicVar: string;
}
export class Base {
static getSomething() : IPublicVarable {
//... read on...
}
}
export class Foo extends Base {
publicVar:string = 'getThis!';
}
}
// No <Foo> type assertion needed
var foo: Test.IPublicVarable = Test.Foo.getSomething();
alert(foo.publicVar);
这并没有解决您遇到的另一个问题 - var type = typeof this;
不会在运行时为您提供您所期望的。它会给你 Function
而不是 Foo
。
要获得类型名称,您确实需要使用一个实例(如果您使用 Test.Foo
,类型名称再次为 Function
- 这对您来说不是好),所以这是一个不完美的例子,使用两个不同的子类都满足接口(interface),基于我的 Obtaining a Class Name at Runtime example :
module Test {
export class Describer {
static getName(inputClass) {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((<any> inputClass).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
}
static getInstanceOf(inputClass) : Test.IPublicVarable {
var name = Describer.getName(inputClass);
return new Test[name]();
}
}
export interface IPublicVarable {
publicVar: string;
}
export class Base {
}
export class Foo extends Base {
publicVar:string = 'foo class';
}
export class Bar extends Base {
publicVar:string = 'bar class';
}
}
var a: Test.Base = new Test.Foo();
var x: Test.IPublicVarable = Test.Describer.getInstanceOf(a);
alert(x.publicVar);
var b: Test.Base = new Test.Bar();
var y: Test.IPublicVarable = Test.Describer.getInstanceOf(b);
alert(y.publicVar);
关于compiler-construction - 在 typescript 中是否有可能让函数返回扩展它的类的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20996686/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!