[英]java - Access variable in child class, given a superclass, from another class
Suppose I have 2 classes ClassBottom1
and ClassBottom2
, that extends classes ClassMiddle1
and ClassMiddle2
respectively, both of which extends another class ClassTop
. 假设我有2个类ClassBottom1
和ClassBottom2
,分别扩展了ClassMiddle1
和ClassMiddle2
类,这两个类都扩展了另一个ClassTop
。 I have 2 separate classes ClassConstruct
and ClassObserve
. 我有2个单独的类ClassConstruct
和ClassObserve
。 ClassObserve
extends KeyAdapter
. ClassObserve
扩展KeyAdapter
。
ClassConstruct: ClassConstruct:
LinkedList<ClassMiddle1> bottom1 = new LinkedList<ClassMiddle1>();
LinkedList<ClassMiddle2> bottom2 = new LinkedList<ClassMiddle2>();
Variables bottom1
and bottom2
contain a list of instances of ClassBottom1
and ClassBottom2
respectively. 变量bottom1
和bottom2
包含的实例列表ClassBottom1
和ClassBottom2
分别。
Suppose ClassBottom1
has a boolean variable called boolVar
evaluating to false
. 假设ClassBottom1
有一个布尔变量,称为boolVar
,其false
。 How would I access this variable from: 我如何从以下位置访问此变量:
ClassObserver: ClassObserver:
private ClassConstruct construct;
public ClassObserver(ClassConstruct construct) {
this.construct = construct;
}
public void keyPressed(KeyEvent e) {
for (int i = 0; i < construct.bottom1.size(); i++) {
construct.bottom1.get(i).boolVar = true; // throws an error, as boolvar does't exist in class (ClassMiddle1)
}
}
How would I solve this problem? 我该如何解决这个问题?
Thank you for your time. 感谢您的时间。
You need to cast it if you are sure that you have the implementation you need. 如果确定您具有所需的实现,则需要强制转换。 But basically the compiler cannot know from the declaration that you ALWAYS will have boolVar
there (if you would always have it it would be in the parent class). 但是,基本上,编译器无法从声明中得知您始终将在其中拥有boolVar
(如果始终拥有它,它将在父类中)。
If you need a specific property and ALL your list items will have a specific type then maybe you should create a list of ClassBottom1 and ClassBottom2 to enforce that only objects having that type and that property are inserted. 如果需要特定的属性,并且所有列表项都具有特定的类型,则可能应该创建ClassBottom1和ClassBottom2的列表,以强制仅插入具有该类型和该属性的对象。 Otherwise it is not enforced and you can get to error. 否则,它不会强制执行,您可能会出错。
That's why we have generics in the first place. 这就是为什么我们首先使用泛型。 Otherwise it would be just lists of Objects (since everything is an object). 否则,它将只是对象列表(因为所有内容都是对象)。
Just putting in code, what other comments have suggested. 只需输入代码,其他注释就会提出建议。
public void keyPressed(KeyEvent e) {
for (int i = 0; i < construct.bottom1.size(); i++) {
ClassMiddle1 m1 = construct.bottom1.get(i);
if(m1 instanceof ClassBottom1)
((ClassBottom1)m1).boolVar = true;
}
}
You just have access to your variable boolVar
directly from an instance of ClassBottom1
or a subclass of ClassBottom1
. 您仅可以直接从boolVar
的实例或ClassBottom1
的子类访问变量ClassBottom1
。
Casting won't be a solution because your ClassMiddle
is not an instance of ClassBottom1
or a subclass of that. ClassMiddle
转换将不是解决方案,因为您的ClassMiddle
不是ClassMiddle
的实例或ClassBottom1
的子类。
// will throw a ClassCastException
((ClassBottom1) construct.bottom1.get(i)).boolVar = true;
Maybe, you can redeclare your list as LinkedList<ClassBottom1>
or rethink about your hierarchy? 也许您可以将列表重新声明为LinkedList<ClassBottom1>
或重新考虑您的层次结构? Then you will be able to access your variable boolVar
. 然后,您将可以访问变量boolVar
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.