我可以在 Typescript 中从父 class 的构造函数触发子 class 的成员函数吗?
Can I trigger subclass's member function from constructor of parent class in Typescript?
class Person {
contructor() {
this.someSubclassMember();
}
}
class Student {
contructor() {
super();
this.someSubclassMember.bind(this);
}
someSubclassMember() {
}
}
我知道我可以为 somSubclassMember 定义 protected 但我想迭代来自父 class?
的 subclasses 原型
这可行吗?
谢谢
PS:我在coffeescript中看到了它的可行性。这是 coffeescript 编译代码
module.exports = ProviderOS = (function(superClass) {
extend(ProviderOS, superClass);
function ProviderOS() {
this.doInternalGetJobCollection = bind(this.doInternalGetJobCollection, this);
this.doCreateJob = bind(this.doCreateJob, this);
this.doCreateOnetimeJob = bind(this.doCreateOnetimeJob, this);
this.doCreateHourlyJob = bind(this.doCreateHourlyJob, this);
this.doCreateDailyJob = bind(this.doCreateDailyJob, this);
this.doExecuteJob = bind(this.doExecuteJob, this);
this.doGetServerInformation = bind(this.doGetServerInformation, this);
this.getBaseName = bind(this.getBaseName, this);
this.onInit = bind(this.onInit, this);
return ProviderOS.__super__.constructor.apply(this, arguments);
}
在这种情况下,我可以从 super class 访问 subclass 成员。
但是打字稿需要在访问之前调用 super。
我自己解决了。
超类有一个名为(init)的成员函数,子类会覆盖它。这种情况下,超类do init()就是调用子类的init。
从子类中,我可以将它用作子类的构造函数。
class Person {
contructor() {
this.someSubclassMember();
}
}
class Student {
contructor() {
super();
this.someSubclassMember.bind(this);
}
someSubclassMember() {
}
}
我知道我可以为 somSubclassMember 定义 protected 但我想迭代来自父 class?
的 subclasses 原型这可行吗? 谢谢
PS:我在coffeescript中看到了它的可行性。这是 coffeescript 编译代码
module.exports = ProviderOS = (function(superClass) {
extend(ProviderOS, superClass);
function ProviderOS() {
this.doInternalGetJobCollection = bind(this.doInternalGetJobCollection, this);
this.doCreateJob = bind(this.doCreateJob, this);
this.doCreateOnetimeJob = bind(this.doCreateOnetimeJob, this);
this.doCreateHourlyJob = bind(this.doCreateHourlyJob, this);
this.doCreateDailyJob = bind(this.doCreateDailyJob, this);
this.doExecuteJob = bind(this.doExecuteJob, this);
this.doGetServerInformation = bind(this.doGetServerInformation, this);
this.getBaseName = bind(this.getBaseName, this);
this.onInit = bind(this.onInit, this);
return ProviderOS.__super__.constructor.apply(this, arguments);
}
在这种情况下,我可以从 super class 访问 subclass 成员。 但是打字稿需要在访问之前调用 super。
我自己解决了。 超类有一个名为(init)的成员函数,子类会覆盖它。这种情况下,超类do init()就是调用子类的init。 从子类中,我可以将它用作子类的构造函数。