【问题标题】:how two interface use two method [closed]两个接口如何使用两种方法[关闭]
【发布时间】:2015-05-28 04:42:12
【问题描述】:

如果两个接口方法名相同但参数不同,调用哪一个?如果没有被调用,为什么?

interface a {
    void show(int i);
}

interface b {
    void show();
}

class InterfaceTest implements b, a {
    public void show() {
        System.out.println("this is show");
    }

    public void show(int a) {
        System.out.println("this is show1");
    }

    public static void main(String args[]) {
        InterfaceTest it = new InterfaceTest(); 
        it.show();
        it.show(1);
    }
}

【问题讨论】:

  • 你是什么意思,“哪个被调用?”您可以通过查看在调用方法时传递给该方法的参数来确定调用的是哪一个。
  • 请努力格式化您的问题。
  • 我投票结束这个问题,因为这个问题没有意义。

标签: java interface


【解决方案1】:

方法的签名定义了将调用哪一个。

您的 2 个显示方法的签名不同。第一个不带任何参数,第二个带一个 int 参数。

由于调用方法时要提供参数,所以调用的方法已经定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多