简体   繁体   English

两个接口要求以相同的名称实现方法

[英]Two interfaces require to implement the method with the same name

If two interfaces require to implement the method with the same name, then the method() is called twice. 如果两个接口要求用相同的名称实现该方法,则method()会被调用两次。 I need 2 methods implemented for 2 different interfaces, how can I implement both of them to do different things? 我需要为2个不同的接口实现2种方法,如何才能同时实现这两种方法以完成不同的工作?

public class MainClass implements BarObj.BarInt, FooObj.FooInt{

    MainClass(){

    }

    void trigger()
    {
        new BarObj(this);
        new FooObj(this);
    }

    @Override
    public void method() {
        System.out.println("I DONT KNOW WHICH METHOD");
    } 

    public static void main(String[] args) {
        new MainClass().trigger();
    }
}

public class BarObj {
    interface BarInt
    {
        void method();
    }
    public BarObj(BarInt _barInt)
    {
        _barInt.method();
    }
}


public class FooObj {
    interface FooInt
    {
        public void method();
    }
    public FooObj(FooInt _fooInt)
    {
        _fooInt.method();
    }
}

You can't 你不能

But to solve your problem you can do next. 但是要解决您的问题,您可以下一步进行。

  1. Remove implements BarObj.BarInt, FooObj.FooInt 删除implements BarObj.BarInt, FooObj.FooInt
  2. Remove method method 删除method方法
  3. Change trigger method 变更trigger方式

It should look like this 它应该看起来像这样

void trigger()
{
    new BarObj(new BarObj.BarInt(){
        @Override
        public void method() {
            System.out.println("NOW I DO KNOW WHICH METHOD");
            System.out.println("I'm bar");
        }
    });
    new FooObj(new FooObj.FooInt(){
        @Override
        public void method() {
            System.out.println("NOW I DO KNOW WHICH METHOD");
            System.out.println("I'm foo");
        }
    });
}

It use anonymous class you can google for more detail. 它使用匿名类,您可以在Google上搜索更多详细信息。

You can not have two methods with same name and same argument type and length in one class. 在一个类中,不能有两个名称相同,参数类型和长度相同的方法。 When you implement two interfaces that have methods with same name, you need to give the implementation to only one method that acts as to be implemented for both interfaces because the methods in interfaces are empty and will never be executed. 当实现两个具有相同名称的方法的接口时,由于接口中的方法为空并且永远不会执行,因此您只需将实现仅赋予一个要为两个接口都实现的方法。 What will be executed is your class method that has been Overrided from the interfaces. 将执行的是已从接口重写的类方法。

暂无
暂无

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

相关问题 使用相同的方法签名但不同的返回类型实现两个接口 - Implement two interfaces with the same method signature but different return type 两个具有相同方法名称的接口,当我覆盖时会发生什么? - Two interfaces with same method name, what happens when I override? 当一个类实现两个接口时,接口具有相同的方法名称,但返回类型不同,为什么它不起作用? - When a class implements two interfaces, interfaces have same method name but different return type why it wont work? 我应该在同一个类中实现这两个接口吗? - Should I implement the two interfaces in the same class? 两个通用接口相互需求,如何正确实现它们? - Two Generic Interfaces require each other, how to proper implement them? 如何在Java中使用方法参数来实现多个接口? - How can I require a method argument in Java to implement multiple interfaces? 如何从不同的接口实现相同的接口方法 - Retrofit 和 Twitter - How to implement same Interface method from different Interfaces - Retrofit and Twitter 实现从具有不同返回类型的两个接口继承的类方法 - Implement a class method inherited from two interfaces with different return types 名称冲突,覆盖失败,在实现具有相同擦除的两个接口的类上 - Name Clash, override fail, on a class implementing two interfaces with same erasure 如果两个接口包含相同的默认方法会发生什么? - What happens, if two interfaces contain the same default method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM