字段子类访问 - 可能的最佳方式 [英] Field Subclass Accessing - Best Way Possible

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

问题描述

所以我在处理程序中的首选项时遇到了一些问题.考虑到这将使用的次数,最好使用单一方法.每个变量的 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)

我想弄清楚如何从子类访问具有定义和常量名称的字段.

超类是抽象的,定义在一个列表中,我可以完全访问它.这是我希望该领域的 getter 所在的类.

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 类不包含字段FOO".理想情况下,如果它有效,我会期望它打印出来:

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. 使用 Getter - 希望避免
  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天全站免登陆