简体   繁体   English

访问父级子类中静态类的受保护成员

[英]Accessing protected member of static class in subclass of the parent

The class ExtendedDismaxQParser has a static member class Clause: 类ExtendedDismaxQParser具有静态成员类Clause:

public class ExtendedDismaxQParser {
    protected static Class Clause {
        protected String foo, bar;
    }
    public Clause getAClause {
        Clause c;
        // misc code that isn't important
        return c;
    }
}

I then extended this class in a different package: 然后,我在另一个包中扩展了该类:

public class SpecialDismaxQParser extends ExtendedDismaxQParser {
    public void whatever() {
        Clause c = super.getAClause();
        boolean baz = c.foo.equals("foobar"); // <-- This doesn't compile
    }
}

It looks like you can't access the foo member variable, despite the fact that the class Clause is protected, and the member variable foo is also protected. 尽管事实是类子句受到保护,成员变量foo也受到保护,但您似乎无法访问foo成员变量。

I just want to be able to check something about the member variable foo of the protected static class Clause. 我只希望能够检查有关受保护的静态类Clause的成员变量foo的某些信息。 How can I do this (preferably without reflection)? 我该怎么做(最好是无反射)?

I would greatly prefer to not have to modify the parent class because it is a part of a library. 我非常希望不必修改父类,因为它是库的一部分。

aioobe's comment is correct. aioobe的评论是正确的。

Sharing a superclass with the outer class isn't enough to get access to protected members of the static inner class. 与外部类共享一个超类还不足以访问静态内部类的受保护成员。

The caller's class doesn't extend Clause, and is in a different package. 调用方的类不扩展Clause,并且在不同的包中。 For protected to be relevant you'd have to access foo from within a subclass of Clause, or access it from a class in the same package as ExtendedDismaxQParser. 为了使protectedprotected相关,您必须从Clause的子类中访问foo,或者从与ExtendedDismaxQParser相同的程序包中的类访问它。

As already been said in the comments and very well explained in the answer by @Nathan Hughes, you can not access the protected field in Clause as it is not the class being extended. 正如评论中已经说过的那样,@ Nathan Hughes的答案很好地解释了这一点,您不能访问Clauseprotected字段,因为它不是正在扩展的类。

The only way to access this field is unfortunately through reflection 不幸的是,访问此字段的唯一方法是通过反射

public class SpecialDismaxQParser extends ExtendedDismaxQParser {
    public void whatever() {
        try {
            Field fooField = Clause.class.getDeclaredField("foo");
            fooField.setAccessible(true);
            Clause c = super.getAClause();
            boolean baz = fooField.get(c).equals("foobar");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

Protected access modifiers allow variables and methods to be accessed within the package only. 受保护的访问修饰符仅允许在包内访问变量和方法。 You have create your class inside the package of your Clause class. 您已经在Clause类的包内创建了类。

Reflection will not do you any good here. 反思在这里对您没有任何好处。

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

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