简体   繁体   English

Java:方法签名

[英]Java: Method signature

I want to make an interface Mergeable that declares a method merge() . 我想使一个接口Mergeable声明一个方法merge() A class that implements this will have to implement the method like follows: 实现此目的的类将必须实现如下方法:

SomeObject.java SomeObject.java

public class SomeObject implements Mergeable

    @Override
    public SomeObject merge(SomeObject other) {...}

See that the return and parameter type are set to the implementing class. 请参见将return和parameter类型设置为实现类。 Is there a way to enforce this and how would I set the method signature in the interface? 有没有一种方法可以强制执行此操作,我将如何在接口中设置方法签名?

Look at the Comparable interface for an example. 请看Comparable接口作为示例。 You'll probably have to write something like 您可能需要写类似

interface Mergeable<T> {
   T merge(T other);
}

...and then anytime you want to accept something Mergeable , write ...然后当您想接受可Mergeable ,写

<T extends Mergeable<T>> void somethingWithMergeables(T merge1) {
   ...
}

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

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