在 Java 中访问 super() 类的私有变量 - JChart2D

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3603405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:31:34  来源:igfitidea点击:

Access a private variable of the super() class in Java - JChart2D

javainheritance

提问by user435262

I have extended a class in Java that has a private variable that I want to get the value of before it is changed. There are no methods to access this variable in the super class. I have tried super().m_zoomArea(the variable is in the ZoomableChartclass of jChart2D). The variable is updated when the mouseDraggedmethod is called. I have overridden this method and would like to get the value of the variable before it is updated.

我在 Java 中扩展了一个类,它有一个私有变量,我想在更改之前获取其值。在超类中没有访问此变量的方法。我试过super().m_zoomArea(变量在ZoomableChartjChart2D 类中)。mouseDragged调用方法时更新变量。我已经覆盖了这个方法,并希望在更新之前获取变量的值。

回答by Jon Skeet

You can't. The whole point of it being privateis that you can't get at the variable. If the class hasn't given any way of finding it out, you can't get it. That may or may not be a design flaw in the class, but unless you use reflection with suitable privileges (which I don'trecommend - you're basically relying on private implementation details) you're going to have to think of an alternative approach.

你不能。它是私有的的全部意义在于你无法获得变量。如果班级没有给出任何找到它的方法,你就无法得到它。这可能是类中的设计缺陷,也可能不是,但是除非您使用具有适当权限的反射(我建议这样做- 您基本上依赖于私有实现细节),否则您将不得不考虑替代方案方法。

回答by dave

You can't access private variables from outside of the class. In order to access it you would have to have it be protected.

您不能从类外部访问私有变量。为了访问它,您必须对其进行保护。

回答by Colin Hebert

You could use reflection but it's a bad idea. A private field is private because the developer doesn't want you to mess with it.

您可以使用反射,但这是一个坏主意。私有字段是私有的,因为开发人员不希望您弄乱它。

I won't give you the way to do it here, but if you reallyknow what you do, follow the links below at your own risks. Again, you shouldn't even think about doing this.

我不会在这里给你做这件事的方法,但如果你真的知道你在做什么,请自担风险按照下面的链接。同样,你甚至不应该考虑这样做。



On the same topic :

在同一主题上:

回答by gpeche

You can do this with the Reflection API (Specifically, see the setAccessible() method). Anyway, this is a hack and may not work if there is a SecurityManager installed in the VM.

您可以使用反射 API 执行此操作(具体请参阅 setAccessible() 方法)。无论如何,这是一种黑客行为,如果 VM 中安装了 SecurityManager,则可能无法正常工作。

回答by Vadeg

You can access private variable of any class, but it's a bad idea, because you're breaking one of the basic principles of OOP - encapsulation.

您可以访问任何类的私有变量,但这是一个坏主意,因为您违反了 OOP 的基本原则之一 - 封装。

But sometimes programmer are forced to break it. Here is the code, which solves your problem:

但有时程序员被迫打破它。这是解决您问题的代码:

Extended class

扩展类

public class ExtZoomableChart
extends ZoomableChart {

public Rectangle2D getZoomArea() {
    try {
        Field field = ZoomableChart.class.getDeclaredField("m_zoomArea");
        field.setAccessible(true);
        Object value = field.get(this);
        field.setAccessible(false);

        if (value == null) {
            return null;
        } else if (Rectangle2D.class.isAssignableFrom(value.getClass())) {
            return (Rectangle2D) value;
        }
        throw new RuntimeException("Wrong value");
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }

}

}

}

and call example:

并调用示例:

public class Main {
    public static void main(String[] args) {
        ExtZoomableChart extZoomableChart = new ExtZoomableChart();

        Rectangle2D d = extZoomableChart.getZoomArea();
        System.out.println(d);
    }
}

You don't need to extend ZoomableChart to get private variable. You can get it value almost from everywhere. But remember - usually it's a bad practice.

您不需要扩展 ZoomableChart 来获取私有变量。您几乎可以从任何地方获得它的价值。但请记住 - 通常这是一种不好的做法。