具有相同方法名称的两个接口 - 方法的实现 [英] Two Interface with Same Method Name - Implementation of Methods

查看:158
本文介绍了具有相同方法名称的两个接口 - 方法的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个接口:

  interface IOne {

public void method();

}

 界面ITwo {

public void method();

}

一个具体的类实现了两个接口:

  public class A实现IOne,ITwo {

public void method(){
// some实现
}
}

我的问题是:


  1. method()的单个实现是否适合于两个接口IOne和ITwo?

  2. 如果 1 的答案为是,是否有任何方法可以将这两种方法都集成到一个类中?在这种情况下,我们不必在一个类中实现两个接口。


解决方案

对于1,答案是。为这两个接口的方法提供一个实现方法就可以自动实现了。



对于2,如果你需要有两种方法,那么你的类应该不实现两个接口。但是,你可以使用一个技巧:

  interface IOne {

void method();
}

接口ITwo {

void method();
}

public class A
实现IOne,ITwo {

//匿名内部类实现ony IOne
private final IOne one = new IOne(){

@Override
public void method(){
System.out.println(IOne);
}
};

//匿名内部类实现ony ITwo
私人最终ITwo两个=新的ITwo(){

@Override
public void method() {
System.out.println(ITwo);
}
};

@Override
public void method(){
System.out.println(A);
}

//代表IOne的方法
public void methodIOne(){
this.one.method();
}

//代表ITwo
的方法public void methodITwo(){
this.two.method();
}
}

测试代码:

  A a = new A(); 
a.method(); // A
a.methodIOne(); // IOne
a.methodITwo(); // ITwo

A 不需要实现两个接口。在这种情况下,只需不要在 A 上实现 method()并且只保留匿名内部类。 p>

Suppose I have two interfaces:

interface IOne {

    public void method();

}

and

interface ITwo {

    public void method();

}

A concrete class implements both of the interfaces:

public class A implements IOne, ITwo {

    public void method(){
       //some implementation
    }
}

My questions are:

  1. Does the single implementation of method() suffice for both interfaces IOne and ITwo?
  2. If the answer of 1 is yes, is there any way to get both the methods in a single class? In this case it is not necessary we have to implement both interfaces in a single class.

解决方案

For 1, the answer is yes. It's enough to provide one implementation for the method in the class for both interfaces to be automatically implemented.

For 2, if you need to have both methods, then your class should not implement both interfaces. However, there's a trick you can use:

interface IOne {

    void method();
}

interface ITwo {

    void method();
}

public class A
    implements IOne, ITwo {

    // Anonymous inner class that implements ony IOne
    private final IOne one = new IOne() {

        @Override
        public void method() {
            System.out.println("IOne");
        }
    };

    // Anonymous inner class that implements ony ITwo
    private final ITwo two = new ITwo() {

        @Override
        public void method() {
            System.out.println("ITwo");
        }
    };

    @Override
    public void method() {
        System.out.println("A");
    }

    // Method that delegates to IOne
    public void methodIOne() {
        this.one.method();
    }

    // Method that delegates to ITwo
    public void methodITwo() {
        this.two.method();
    }
}

Testing code:

A a = new A();
a.method(); // A
a.methodIOne(); // IOne
a.methodITwo(); // ITwo

Class A doesn't need to implement both interfaces. In that case, just don't implement method() on A and keep only the anonymous inner classes.

这篇关于具有相同方法名称的两个接口 - 方法的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆