简体   繁体   中英

Why can't an implementing method have an argument

For this:

import java.awt.EventQueue;
import javax.swing.JFrame;

public class SwingExample11 extends JFrame implements Runnable {
    public SwingExample11() {
        initUI();
    }   
    private void initUI() {
        setTitle("SwingExample1");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }   
    @Override
    public void run(SwingExample11 ex) {
        ex.setVisible(true);
    }   
    public static void main(String[] argv) {
        SwingExample11 ex = new SwingExample11();
        EventQueue.invokeLater(ex);
    }   
}

I get:

6: error: SwingExample11 is not abstract and does not override abstract method run() in Runnable
public class SwingExample11 extends JFrame implements Runnable {
^
16: error: method does not override or implement a method from a supertype
@Override
^

When I remove the argument and the code block from run() it compiles, but obviously doesn't do anything.

When I implement an interface I thought I could write the method however I wanted? Is it because its an abstract method? I thought all interface methods were abstract by default, and anyway, I thought if you @Override a method then you can do what you want with it.

I think I am confused with abstract methods. Where am I going wrong please, and what will make me understand things more clearly?

In java method signature consists of method name and its argument types. This is the basics of method overloading (same method name, but different argument types).

run(void) is not the same as run(SwingExample11).

This is because at runtime JVM will look for a method with name run and takes no arguments. Since you have only defined a method with name "run" and takes one argument of type "SwingExample11", there are no method with name "run" taking no arguments.

您不能在实现时更改方法签名。您可以提供自己的方法实现,前提是方法签名保持不变。

A method's signature consists of 3 parts, return type, method name, parameters and throws clause.

When you are implementing a method from an interface, you must override every method. This means that the methods signatures must match up. Here when you added an extra parameter,

@Override
public void run(SwingExample11 ex) {
    ex.setVisible(true);
}  

your method is no longer the method that you need to implement, and is a completely new method.

EDIT: Thanks to the comments for correcting me

Think of the way inheritance works. If you implement the run method from the interface Runnable , anyone calling this run method on the interface itself should execute the run method you had implemented.

So if there's code somewhere Runnable r = new SwingExample11() and then r.run() this is meant to execute the run method in your SwingExample11 class, without any parameter, but your overridden method actually expects a parameter - what should be it's value?

Because of this the signature has to be exactly the same, otherwise the whole inheritance concept in the type system would break down (specifically the substitution principle ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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