简体   繁体   English

在扩展另一个类的类之外访问变量

[英]Access variables outside a class that extends another class

How can I access variable outside a class that extends an other class that is used in an array list? 如何在扩展了数组列表中使用的其他类的类之外访问变量? I get an error that says that the variable does not exist in the extended class. 我收到一条错误消息,指出扩展类中不存在该变量。 Take a look, I want to access the variable members without having to declare it in my Object class: 看一下,我想访问变量成员而不必在我的Object类中声明它:

public abstract class Object {
    public int x, y;
}

public class House extends Object {
    public int members = 10;
}

// Somewhere else
ArrayList<Object> list = new ArrayList<Object>();
list.add( new House() );
for (Object o : list ) {
    o.members;
}

The problem is that in the RTS I'm writing, my Object class has over 40 variables, just because it doesn't work to declare them only in the sub class and access them from outside. 问题是在我正在编写的RTS中,我的Object类具有40多个变量,仅因为仅在子类中声明它们并从外部访问它们是不起作用的。 Hope you understand. 希望你能理解。 How can I do this? 我怎样才能做到这一点?

You can use instanceof and a cast: 您可以使用instanceof和强制类型转换:

for (Object o : list ) {
    if (o instanceof House) {
        h = (House) o;
        h.members;
    }
}

However, this is often considered bad design; 但是,这通常被认为是不好的设计。 you should consider defining an appropriate method in Object (which should really have another name, as others have pointed out) and override it in House . 您应该考虑在Object定义一个适当的方法( 确实应该有另一个名称,正如其他人指出的那样),并在House重写它。 Then, you may call that method on an Object without knowing what kind of object it refers to, and if it is a House , the correct method will be called. 然后,您可以在不知道对象所引用的对象类型的情况下在Object上调用该方法,如果它是House ,则将调用正确的方法。 (Learning how to do this properly, and when to do it, takes a bit of practice - google polymorphy and overriding .) (学习如何正确执行此操作以及何时执行此操作需要一些练习-谷歌多态重写 。)

First do not name your class Object (see the comments). 首先,不要为您的类Object命名(请参阅注释)。 You cannot access member of an Object in your code, because Object has no field member , House has. 您无法在代码中访问Object member ,因为对象没有字段memberHouse没有。 But an Object does not have to be a House , so it is not guaranteed that it has member . 但是Object不必是House ,因此不能保证它有member

If you're sure that in this case youre Object is always a House , cast it: 如果您确定在这种情况下,您的Object 始终House ,则将其强制转换为:

    ((House) anObject).member;

This way the compiler assumes that you know more than he does about the actual class of the Object and handles it as if it was a House . 这样,编译器假定您比他对Object的实际类了解更多,并且像对待House一样处理它。 You can use instanceof to check if the cast is valid. 您可以使用instanceof检查强制类型转换是否有效。

Hope you understand that you are using the name for your class as 'Object', which is the parent class for all the classes in Java. 希望您理解您将类的名称用作“对象”,这是Java中所有类的父类。 Now in your environment there will be two Object classes one which java provides from java.lang.Object and another one you have created. 现在,在您的环境中将有两个Object类,一个是Java从java.lang.Object提供的,另一个是您创建的。 So when you are trying to access your class object and trying to get the attributes of that, it is actually not your class object rather it is an instance of java.lang.Object and hence you are running into an issue. 因此,当您尝试访问类对象并尝试获取其对象的属性时,它实际上不是您的类对象,而是java.lang.Object的实例,因此您遇到了问题。

You have to cast o to a House. 您必须将o投向房屋。 Eg ((House) o).members 例如((House) o).members

只是像这样转换arraylist的成员

((House)o).members;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM