是否可以访问子类成员并从基类执行子函数?
例如:
String strNumber=editText.getText().toString().trim();
if(TextUtils.isEmpty(strNumber) || Integer.parseInt(strNumber)>100){
// show your error message
}
这可以完成class ChildClass extends BaseClass {
protected childMember = 'someName';
constructor() {
super();
super.runBaseFn();
}
runChildFn() {
console.info('child fn');
}
}
class BaseClass {
runBaseFn() {
if (child.childMember === 'someName') {
child.runChildFn();
}
}
}
中的代码吗?或者任何类似的解决方案?
答案 0 :(得分:1)
您需要在父类中声明:
class BaseClass {
protected childMember: string;
runBaseFn() {
if (this.childMember === 'someName') {
this.runChildFn();
}
}
protected runChildFn() {}
}
class ChildClass extends BaseClass {
constructor() {
super();
this.childMember = 'someName';
super.runBaseFn();
}
runChildFn() {
console.info('child fn');
}
}
您可以制作父类abstract
:
abstract class BaseClass {
protected childMember: string;
runBaseFn() {
if (this.childMember === 'someName') {
this.runChildFn();
}
}
protected abstract runChildFn();
}