调用一个接口从一个接口中

huangapple go评论55阅读模式
英文:

Calling an interface from an interface

问题

我正在处理一个项目,其中包含一些遗留代码,我不确定某些接口如何相互交互。

有两个接口,看起来像这样:

public interface FirstInterface {
    void someFunction();
}

和第二个接口调用第一个接口的:

public interface SecondInterface {
    FirstInterface fi();
}

我对Java不太了解,不确定这是否是某种嵌套接口,或者是否有其他情况。我也不确定 FirstInterface fi() 是调用接口还是创建该类型的方法,如果是这样的话,有什么优势,而不是像 FirstInterface fi 这样做呢?如果有助于解释清楚,还请告诉我,谢谢!

英文:

I'm working on a project which has some legacy code and I'm not sure how certain interfaces interact with each other.

There are two interfaces, that look something like this:

public interface FirstInterface {
     void someFunction();
}

And the second one which calls (?) the first one:

public interface SecondInterface {
     FirstInterface fi();
}

I'm new to Java and I don't understand if this is some form of nested interface or if something else is going on here. I'm also not sure if FirstInterface fi() calls the interface or creates a method of that type and if so what is the advantage over doing something like FirstInterface fi? If it helps, both these interfaces are in different packages.

If I can add any information to make this clearer, please let me know and thank you!

答案1

得分: 2

需要实现接口。因此将会有一个类,比如说FirstClass,定义如下:

class FirstClass implements FirstInterface {
    public void someFunction() {
        System.out.println("你好,世界!");
    }
}

另一个类要实现SecondInterface接口。从语法上可以看出,方法fi() 返回 了一个FirstInterface的实例;它并没有调用它。事实上,你不能调用一个接口(或一个类):你只能调用方法。

为了返回一个FirstInterface的实例,fi()方法必须创建一个实现该接口的类的对象(比如FirstClass),然后返回该对象。作为练习,你可以尝试编写一个类SecondClass及其fi()方法。

英文:

Interfaces need to be implemented. So there will be a class, say FirstClass, defined something like this:

class FirstClass implements FirstInterface {
    public void someFunction() {
        System.out.println("Hello, world!");
    }
}

and another class that implements SecondInterface. Notice from the syntax that the method fi() returns an instance of FirstInterface; it does not call it. In fact, you can't call an interface (or a class): you can only call methods.

To return an instance of FirstInterface, the fi() method must create an object of a class that implements it (such as FirstClass), and return that object. As an exercise, you might want to try writing a class SecondClass and its fi() method.

答案2

得分: 1

Java中的接口用于多态性。关键点在于接口仅提供定义而不提供实现。这些声明也被称为方法签名,因为它们声明了方法的名称、返回类型和参数。

在你的代码中,FirstInterface只声明了一个不返回任何东西的方法。另一方面,SecondInterface声明了一个返回类型为FirstInterface的对象的方法。但正如我所说,这只是声明。这些接口由类来实现,在类中我们定义它们的具体实现。
我认为没有比代码片段更好的了。考虑以下示例:

public interface MyInterface{
    void meathod1();
    int meathod2();
    String meathod3();
}

public interface MyInterface2{
    MyInterface meathod4();
}

public class ClassA implements MyInterface{
    public void meathod1(){        
    } 

    public Integer meathod2(){
        return 0;        
    }

    public String meathod3(){        
        return "Apple";
    }
}

public class ClassB implements MyInterface{
    public void meathod1(){        
    } 

    public Integer meathod2(){
        return 1;        
    }

    public String meathod3(){        
        return "Banana";
    }
}

public class ClassC implements MyInterface2{
    public MyInterface meathod4(){
        MyInterface myInterface = new ClassA();
        return myInterface;
    }
}

在这里,MyInterfaceMyInterface2是两个接口。第一个声明了3个方法,分别返回void、整数和字符串。第二个接口声明了一个返回类型为MyInterface的对象的方法。

MyInterface由两个类ClassAClassB实现。它们都根据自己的需求为方法分配了定义。

MyInterface2ClassC类实现,它创建了一个类型为MyInterface的引用myInterface。因为MyInterfaceClassAClassB类实现,所以它可以指向其中任何一个对象。在上面的代码中,我们创建了一个来自ClassA类的对象,并将其与myInterface关联起来。最后,我们按照方法签名的规定返回它。这与你的代码中发生的情况类似。 调用一个接口从一个接口中

英文:

Interfaces in java are used for polymorphism. The key point is that interfaces are only definitions not implementations. These declarations are also called method signatures as they declares the name, return-type and parameters of the method.

In your code FirstInterface only declares a method which doesn't return anything. On the other hand SecondInterface declares a method which returns an object of type FirstInterface. But as I said, these are only declarations. These interfaces are implemented by Classes where we define their body.
I think nothing is better than a code snippet. So consider this example:

public interface MyInterface{
	void meathod1();
	int meathod2();
	String meathod3();
}

public interface MyInterface2{
	MyInterface meathod4();
}

public class Class A implements MyInterface{
	public void meathod1(){		
	} 

	public Integer meathod2(){
		return 0;		
	}

	public String meathod3(){		
		return "Apple";
	}
}

public class Class B implements MyInterface{
	public void meathod1(){		
	} 

	public Integer meathod2(){
		return 1;		
	}

	public String meathod3(){		
		return "Banana";
	}
}

public class Class C implements MyInterface2{
	public MyInterface meathod4(){
		MyInterface myInterface = new A();
		return myInterface;
	}
}

Here MyInterface and MyInterface2 are 2 interfaces. First one declares 3 methods which return nothing, integer and String respectively. Second interface declares method which returns object of type MyInterface.

MyInterface is implemented by 2 classes namely A and B. Both of them assign definitions to the methods as per their needs.

MyInterface2 is implemented by Class C, which creates an reference myInterface of type MyInterface. Because MyInterface was implemented by classes A and B, it can point to object of either of them. In the code above we made an object from class A and associate it with myInterface. Finally we return it as specified by the method signature. This is similar to what happening in your code. 调用一个接口从一个接口中

huangapple
  • 本文由 发表于 2020年7月21日 21:20:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63015431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定