The question is more Java, but I want to implement it in Android:
Suppose there are 3 or more classes extending each other:
class A {
...
int color;
...
}
class B extends A {
...
}
class C extends B {
...
// I want to a开发者_开发知识库lter color of class A inside here
}
Is this possible without setting a super.color = 4
in C
and setting another color=4
and super.color = 4
in B
??
e.g. is it possible to write something like super(super(color=3))
within class C ?
...
// I want to alter color of class A inside here
...
Non-private fields are inherited to the subclasses. You can just do color = 3
in C
and it will affect A.color
.
It isn't possible through the constructors - you can only invoke the immediate super constructor.
But you can reassign the field with color=3
after the call the super()
精彩评论