开发者

"super of super" in extented classes (alter variable in super class of super class)

开发者 https://www.devze.com 2023-04-12 05:57 出处:网络
The question is more Java, but I want to implement it in Android: Suppose there are 3 or more classes extending each other:

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消