简体   繁体   English

为什么执行方法不能有参数

[英]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. 当我从run()删除参数和代码块时,它会编译,但显然不执行任何操作。

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. 我认为默认情况下所有接口方法都是抽象的,无论如何,我想如果您@Override一个方法,那么您可以使用它来做您想做的事情。

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. 在Java中,方法签名由方法名称及其参数类型组成。 This is the basics of method overloading (same method name, but different argument types). 这是方法重载的基础(方法名称相同,但参数类型不同)。

run(void) is not the same as run(SwingExample11). run(void)与run(SwingExample11)不同。

This is because at runtime JVM will look for a method with name run and takes no arguments. 这是因为JVM在运行时将查找名称为run且不带参数的方法。 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. 由于您仅定义了一个名称为“ run”的方法,并且接受了一个类型为“ SwingExample11”的参数,因此没有名称为“ run”的方法且不包含任何参数。

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

A method's signature consists of 3 parts, return type, method name, parameters and throws clause. 方法的签名由三部分组成:返回类型,方法名称,参数和throws子句。

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. 如果从Runnable接口实现run方法,则在接口本身上调用此run方法的任何人都应执行您已实现的run方法。

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? 因此,如果在某处有代码Runnable r = new SwingExample11() ,然后是r.run()这意味着可以在SwingExample11类中执行不带任何参数的run方法,但是您覆盖的方法实际上需要一个参数-它应该是什么值?

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 ). 因此,签名必须完全相同,否则类型系统中的整个继承概念都会崩溃(特别是替代原理 )。

暂无
暂无

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

相关问题 为什么不能实现类将覆盖方法定义为静态方法? - Why can't implementing classes define an overriding method as static? 如果第一个接口定义了为什么我不能在另一个接口中使用实现类作为参数? - Why can't I use an implementing class as an argument in another interface if the first interface defines it? 为什么不能使用私有抽象方法? - Why can't I have an private abstract method? 我可以将方法参数键入“此类”吗? - Can I have a method argument typed “this class”? 当输入类型是接口且参数类型正在实现类时,Java为什么找不到合适的方法? - Why can't Java find a suitable method when the input types are interfaces and the parameter types are implementing classes? 为什么我不能将参数传递给toString()方法,为什么它给出null? - Why can't I pass an argument into a toString() method and why does it give a null? 实现Clonable接口,但不必重写clone()方法 - Implementing the Clonable interface, but don't have to override the clone() method 为什么我不能将类型参数显式传递给通用 Java 方法? - Why can't I explicitly pass the type argument to a generic Java method? 如何返回一个 lambda 函数并通过将其作为参数传递给 Java 8 中的方法来使用? 为什么我不能使用 :: 运算符? - How to return a lambda function and use by passing as an argument to a method in Java 8? Why I can't use :: operator? 为什么不能在Java中使用相同的方法签名的静态和非静态方法? - Why can't you have a static and non-static method with the same method signature in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM