简体   繁体   English

来自另一个没有继承的类的Java覆盖方法

[英]Java override method from another class without inheritance

I found a similar question here: 我在这里发现了类似的问题:
overriding methods without subclassing in Java 覆盖Java中没有子类化的方法

But mine a little different, I have two classes, 1 GUI based, and the other just methods to modify elements in first class. 但我的有点不同,我有两个类,一个基于GUI,另一个只是修改第一类元素的方法。 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. 如果它只是编辑基本函数,我没有遇到任何问题,但现在我想从第二个类中覆盖第一类方法中的jbutton而不继承它。 Where do I have to start? 我在哪里开始?

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). 我有临时解决方案,第二个类扩展JButton,覆盖我想要的方法,并将该类添加到我的GUI类(匿名对象与否,无所谓)。 But I want to discover a way to find a way to my question, is it even possible? 但我想找到一种方法来找到解决问题的方法,甚至可能吗? Thanks :) 谢谢 :)

Edit 编辑
Here's sample code: 这是示例代码:

First class, as it only a button in jframe, I only add these in constructor: 第一类,因为它只是jframe中的一个按钮,我只在构造函数中添加它们:
ButtonOverrider bo=new ButtonOverrider(); -> this is the overrider class - >这是overrider类
button=bo.overridePaintComponent(bo); //first try //第一次尝试
button=bo.overridePaintComponent(); //second try //第二次尝试
bo.overridePaintComponent(bo); //third try //第三次尝试

And here's the ButtonOverrider method: 这是ButtonOverrider方法:

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? 我在哪里开始?

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. 这就是覆盖Java的工作方式。

EDIT: 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. 永远不会使用您的参数值。

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

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