Z S*_*ith 3 inheritance class-method typescript
我希望能够在 Typescript 中使用基于类的方法 - 即在基类上定义的方法,当在子类上调用时,可以访问调用它的子类。这种行为在 Python 中是可能的,但据我所知,在 Typescript 中没有等价物。在 Typescript 中复制它的最佳方法是什么?
示例(在 Python 中):
这个例子(用 Python 编写)演示了我可能想用它做的事情。
# Base class
class Model:
objects = []
def __init__(self, objId):
self.objId = objId
Model.objects.append(self)
@classmethod
def getById(cls, objId):
# Method can access subclass using "cls" parameter
objects = [obj for obj in cls.objects if obj.objId == objId]
if len(objects) > 0:
return objects[0]
else:
print("error")
# Subclass
class Foo(Model):
def __init__(self, objId, name):
self.objId = objId
self.name = name
Foo.objects.append(self)
Foo(1, "foo")
Foo(3, "bar")
# Call method on subclass
foo = Foo.getById(1)
bar = Foo.getById(3)
print(foo.name) # outputs "foo"
print(bar.name) # outputs "bar"
Foo.getById(2) # outputs "error"
Run Code Online (Sandbox Code Playgroud)
打字稿(不工作):
此示例显示了 typescript 中的粗略等效项,但由于缺少类方法,因此它不起作用。
class Model {
static objects: Model[]
id: number
constructor (id) {
this.id = id
Model.objects.push(this);
}
// Here "cls" should refer to the class on which this method is called
static getById (id): cls {
let item = cls.objects.find(obj => obj.id == id);
if (item === undefined) {
console.log("error");
} else {
return item;
}
}
}
class Foo extends Model {
name: string
constructor (id, name) {
super(id);
this.name = name
Foo.objects.push(this);
}
}
new Foo(1, "foo");
new Foo(3, "bar");
// Here cls === Foo
let foo = Foo.getById(1);
let bar = Foo.getById(3);
console.log(foo.name);
console.log(bar.name);
Foo.getById(2)
Run Code Online (Sandbox Code Playgroud)
显然,这对单个类很容易做到,但我无法找到一种方法可以将这样的方法用于多个类,而无需在每个类上重新声明它。
侧面问题:
有没有办法在每个子类上都有一个“对象”静态属性,每个子类都输入到它们的子类中,而无需手动重新声明它。
class Model {
static objects: Model[]
class Foo extends Model {
static objects: Foo[]
class Bar extends Model {
static objects: Bar[]
Run Code Online (Sandbox Code Playgroud)
本质上,我想要这种行为,但不必在每个子类上单独声明“对象”属性。有没有办法做到这一点?
this静态方法的上下文是调用它的类。您可以覆盖this类型,getById该国getById可以在任何的子类被称为Model(即,与构建的一个亚型构建签名的任何对象Model),并返回该实例类型的类。下面是示例代码,假设所有子类的所有对象都存储在单个Model.objects数组中:
class Model {
static objects: Model[]
id: number
constructor (id) {
this.id = id
Model.objects.push(this);
}
static getById<M extends Model>(
this: { new(...args: any[]): M }, id): M {
let item = Model.objects.find(obj => obj.id == id);
if (item === undefined) {
console.log("error");
} else {
return <M>item;
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这是不合理的,因为有人可以Foo.getById使用对应于 aBar而不是 a的 ID 进行呼叫Foo。
如果你想objects为每个子类创建一个单独的数组,你需要在每个子类中手动初始化数组(没有办法自动化),更改Model构造函数以推送到objects当前类的数组中(你可以引用为this.constructor在声明它之后),然后在声明它必须存在之后更改getById为 use this.objects。
class Model {
static objects: Model[]
"constructor": {
// This implementation is slightly unsound: the element type
// is actually the instance type of the constructor. At least
// the interface provided is safe.
objects: Model[]
};
id: number
constructor (id) {
this.id = id
this.constructor.objects.push(this);
}
static getById<M extends Model>(
this: { new(...args: any[]): M, objects: M[] }, id): M {
let item = this.objects.find(obj => obj.id == id);
if (item === undefined) {
console.log("error");
} else {
return item;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2790 次 |
| 最近记录: |