我有点困惑:我可以覆盖一个setter/getter但仍然使用super setter/getter吗?如果是 - 如何?
使用案例:
class A {
void set value(num a) {
// do something smart here
}
}
class B extends A {
void set value(num a) {
// call parent setter and then do something even smarter
}
}
Run Code Online (Sandbox Code Playgroud)
如果这不可能,那么如何仍然保留API但扩展新类中的逻辑.代码的用户已经使用了instance.value = ...所以我不想将其更改为方法调用是可能的.
请帮忙:)
只需要调用super.value = a
class A {
void set value(String value) {
print(value.toUpperCase());
}
}
class B extends A {
void set value(String value) {
super.value = value;
print(value.toLowerCase());
}
}
void main() {
B b = new B();
b.value = "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式访问父级super.:
class B extends A {
void set value(num a) {
super.value = a;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
857 次 |
| 最近记录: |