访问父级子类中静态类的受保护成员 [英] Accessing protected member of static class in subclass of the parent

查看:102
本文介绍了访问父级子类中静态类的受保护成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ExtendedDismaxQParser类具有静态成员类Clause:

The class ExtendedDismaxQParser has a static member class 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
    }
}

尽管类子句受到保护,成员变量foo也受到保护,但您似乎无法访问foo成员变量.

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.

我只想能够检查有关受保护的静态类Clause的成员变量foo的某些信息.我该怎么做(最好是无反射)?

I just want to be able to check something about the member variable foo of the protected static class Clause. 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的评论正确.

aioobe's comment is correct.

与外部类共享超类不足以访问静态内部类的受保护成员.

Sharing a superclass with the outer class isn't enough to get access to protected members of the static inner class.

调用方的类不扩展Clause,并且在不同的包中.为了使protected具有相关性,您必须从Clause的子类中访问foo,或者从与ExtendedDismaxQParser相同的程序包中的类访问它.

The caller's class doesn't extend Clause, and is in a different package. 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.

这篇关于访问父级子类中静态类的受保护成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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