现场访问子类 - 最好的方式 [英] Field Subclass Accessing - Best Way Possible

查看:147
本文介绍了现场访问子类 - 最好的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我碰到了一点用preferences在程序处理的问题。考虑的时候,这量将被使用,具有一个单一的方法将是最好的。一个为每个变量的getter可能导致大得多的文件。

So I've run into a bit of a problem dealing with preferences in a program. Considering the amount of times this will be used, having a single method would be best. A getter for each variable could potentially lead to a substantially larger file.

(5场* 200到300班=大量的空间浪费)

(5 fields * 200 to 300 classes = lots of wasted space)

我试图找出如何访问一个字段与子类中的定义常量名。

超类是抽象的,在列表中定义的,我有充分的机会来了。这是我想为外地的吸气剂是在类中。

The super class is abstract, defined in a list and I have full access to it. This is the class where I would like the getter for the field to be in.

我想它的工作会是这样的方式:

The way I would like it to work would be something like this:

import java.lang.reflect.Field;

public class Test {

    public Test() {
        final B b = new B();
        final C c = new C();
        System.out.println(b.getFoo());
        System.out.println(c.getFoo());
    }

    public static void main(String[] args) {
        new Test();
    }

    public class A {

        public String getFoo() {
            try {
                Field f = this.getClass().getField("FOO");
                f.setAccessible(true);
                return (String) f.get(null);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

    }

    public class B extends A {

        private static final String FOO = "$HOME.DIR/lib/res/image1.png";

    }

    public class C extends A {

        private static final String FOO = "$HOME.DIR/lib/res/image2.png";

    }

}

果然,没有工作。类 A 不包含字段'富'。理想情况下,如果它工作,我本来期望它打印出来:

Expectedly, that did not work. The class A does not contain the field 'FOO'. Ideally, if it had worked I would have expected it to print out:

$ HOME.DIR / lib中/ RES / image1.png结果
  $ HOME.DIR / lib目录/ RES / image2.png

$HOME.DIR/lib/res/image1.png
$HOME.DIR/lib/res/image2.png

我已经(到目前为止)看出,这是有可能的途径:

The ways I have (so far) seen that this would be possible:


  1. 反射

  2. 使用吸气 - 想避免

  3. 使用注释

使用注释是一种方法,我可以看到它是可能的,但我一般不喜欢他们的整体概念,但如果它是唯一的方法我当然会接受它。

Using annotations was one way I could see it being possible, yet I generally do not like the overall concept of them, but if it is the only way I would of course accept it.

感谢您阅读,希望你能提供洞察力。

Thank you for reading this, hopefully you can provide insight.

推荐答案

您能描述一下你更高层次的问题更详细,因为它听起来可能有一些设计变更,你可以做来缓解这个问题。访问一个子类的领域形成一个父类通过反射似乎只是一般一个坏主意。

Can you describe your higher level problem in more detail, since it sounds like there may be some design changes that you could make to alleviate this issue. Accessing the fields of a subclass form a parent class via reflection just seems like a bad idea in general.

然而,随着中说,你需要使用 getDeclaredField 为了访问私有字段,而不是 getfield命令通过反射。

However, with that said, you need to use getDeclaredField instead of getField in order to access a private field by reflection.

例如

public String getFoo() {
        try {
            Field f = this.getClass().getDeclaredField("FOO");
            f.setAccessible(true);
            return (String) f.get(null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
}

这篇关于现场访问子类 - 最好的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆