来自其他类的Java重写方法,无需继承 [英] Java override method from another class without inheritance
问题描述
我在这里找到了类似的问题:
无需在Java中进行子类化的替代方法
I found a similar question here:
overriding methods without subclassing in Java
但是我有点不同,我有两个类,一个基于GUI,另一个只是修改第一类元素的方法.如果仅编辑基本函数,我不会遇到任何问题,但是现在我想从第二类重写第一类方法中的jbutton而不继承它.我必须从哪里开始?
But mine a little different, I have two classes, 1 GUI based, and the other just methods to modify elements in first class. If it just editing the basic function, I meet no problem, but now I want to override a jbutton in first class method from the second class without inheriting it. Where do I have to start?
我有一个临时解决方案,那就是第二个类扩展JButton,重写我想要的方法,然后将该类添加到我的GUI类中(不管是否有匿名对象).但是我想找到一种方法来找到解决我的问题的方法,甚至可能吗?谢谢:)
I have temporary solution which is that the second class extends JButton, override method I want to, and add that class to my GUI class (anonymous object or not, is doesn't matter). But I want to discover a way to find a way to my question, is it even possible? Thanks :)
修改
这是示例代码:
Edit
Here's sample code:
第一类,因为它只是jframe中的一个按钮,所以我只在构造函数中添加它们:
ButtonOverrider bo=new ButtonOverrider();
->这是替代程序类
button=bo.overridePaintComponent(bo);
//第一次尝试
button=bo.overridePaintComponent();
//第二次尝试
bo.overridePaintComponent(bo);
//第三次尝试
First class, as it only a button in jframe, I only add these in constructor:
ButtonOverrider bo=new ButtonOverrider();
-> this is the overrider class
button=bo.overridePaintComponent(bo);
//first try
button=bo.overridePaintComponent();
//second try
bo.overridePaintComponent(bo);
//third try
这是ButtonOverrider方法:
And here's the ButtonOverrider method:
public JButton ButtonOverrider(JButton button) {
button = new JButton() {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
GradientPaint gp = new GradientPaint(0, 0,
Color.blue.brighter().brighter(), 0, getHeight(),
getBackground().darker().darker());
g2.setPaint(gp);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
super.paintComponent(g);
super.setContentAreaFilled(false);
super.setFocusPainted(false);
super.setBorder(new LineBorder(Color.yellow, 2));
super.setText("Shuro");
}
};
return button;
}
推荐答案
我必须从哪里开始?
Where do I have to start?
具有继承性.这是覆盖有意义的唯一方法.尚不清楚为什么您不想使用继承,但这确实是覆盖方法的唯一方法.不管使用匿名类还是命名类都没有关系,但是必须扩展类以覆盖该方法.这就是Java中重写工作的方式.
With inheritance. That's the only way that overriding makes any sense. It's not clear why you don't want to use inheritance, but that really is the only way of overriding a method. Whether you use an anonymous class or a named one is irrelevant, but it must extend the class in order to override the method. That's simply the way overriding works in Java.
您在更新的问题中显示的代码通过创建一个匿名内部类来使用继承...但是它并不能满足我的期望,因为它创建一个 new 对象,而不是覆盖现有对象上的方法.您的参数值将永远不会被使用.
The code you've shown in your updated question does use inheritance by creating an anonymous inner class... but it doesn't do what I expect you want it to, because it creates a new object rather than overriding the method on the existing object. Your parameter value is never used.
这篇关于来自其他类的Java重写方法,无需继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!