简体   繁体   English

关于Java方法和实现多个接口的参数

[英]About Java method & a parameter which implements multiple interfaces

I'm writing a Java application, and have a question. 我正在编写Java应用程序,并且有一个问题。 How can I implement a function with one parameter which should require an argument which implements multiple interfaces? 如何使用一个参数实现一个函数, 参数应该需要一个实现多个接口的参数? For example: 例如:

interface Father
{
}

interface Teacher
{
}

public void foo(FatherAndTeacher person) // How do I do this?
{
    // Do something
}

I know I can use two parameters, such as: 我知道我可以使用两个参数,例如:

public void foo(Father person1, Teacher person2)
{
    // Do something
}

but I think maybe having a single parameter which implements both interfaces would be better. 但我认为也许有一个实现两个接口的参数会更好。

You can use a composite interface by extending both of your Teacher and Father interfaces. 您可以通过扩展教师界面和父亲界面来使用复合界面。

Here's an example: 这是一个例子:

interface Teacher
{
    void teach();
}

interface Father
{
    void makeBadJoke();
}

// ----- Composite interface! Doesn't add any methods.
interface TeacherAndFather extends Teacher, Father
{
    /*This can be empty*/
}

class Bob implements TeacherAndFather
{
    public void teach() { System.out.println("1 + 1 = 2"); }
    public void makeBadJoke() { System.out.println("Knock knock..."); } 
}

class Main
{
    private static void foo(TeacherAndFather foo)
    {
        foo.teach();
        foo.makeBadJoke();
    }

    public static void main (String... args)
    {
        foo(new Bob());
    }
}

Two enforce a parameter to have 2 interfaces you have 2 basic options: 两个强制参数具有2个接口,您有2个基本选项:

  1. Create a common interface that extends both and use that as your parameter type: 创建一个扩展两者的通用接口,并将其用作您的参数类型:

     interface FatherAndTeacher extends Father, Teacher { } 

    The problem with that is that objects that don't implement that interface don't match. 问题在于没有实现该接口的对象不匹配。

  2. Use generics where you can require matching objects to implement both interfaces: 使用泛型,您可能需要匹配的对象才能实现两个接口:

     public <T extends Father & Teacher> void foo(T person) { // Do something } 

    Note that this only works with interfaces, ie you can't do T extends Number & String (2 classes). 请注意,这仅适用于接口,即您不能T extends Number & String (2类)。 It works with one object boundary though, in which case the class must be first: T extends ConcretePerson & Father is ok but T extends Father & ConcretePerson is not (where ConcretePerson is a class). 但是,它只能在一个对象边界上工作,在这种情况下,该类必须是第一个: T extends ConcretePerson & Father还可以,但是T extends Father & ConcretePerson则不能(其中ConcretePerson是类)。

just use Object as your function parameter type: 只需使用Object作为您的函数参数类型:

void doSomething(Object param);

or using generic method: 或使用通用方法:

<T> void doSomething(T param);

or let the two interface both inherit from one same interface. 或让两个接口都从同一个接口继承。

public interface Person {}
public interface Teacher extends Person {}
public interface Father extends Person {}
public class Test {
    public void foo(Person p) {
        //
    }
}

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

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