Java 我可以输入一个方法参数吗;“这个班?”;?

Java 我可以输入一个方法参数吗;“这个班?”;?,java,method-signature,Java,Method Signature,我希望在接口的方法签名中有一个类型为“this class”的参数,这样任何类,例如MyClass,实现它的类都将有一个类型为MyClass的参数的方法{ public interface MyInterface { public thisClass myMethod(thisClass other); ... } 公共thisClass myMethod(thisClass其他); ... }公共类MyClass实现MyInterface{ //必须在实现中引用此类 公共MyC

我希望在接口的方法签名中有一个类型为“this class”的参数,这样任何类,例如
MyClass
,实现它的类都将有一个类型为
MyClass
的参数的方法{
public interface MyInterface {
    public thisClass myMethod(thisClass other);
    ...
}
公共thisClass myMethod(thisClass其他); ... }
公共类MyClass实现MyInterface{
//必须在实现中引用此类
公共MyClass myMethod(MyClass其他){
...
}
}
这是可能的,还是应该将参数与接口绑定,并让每个实现检查它?

公共接口MyInterface{
public interface MyInterface<T> {
   public T myMethod(T other);
   ...
}

public class MyClass implements MyInterface<MyClass> {
   // has to reference this class in the implementation
   public MyClass myMethod(MyClass other){
      ...
   } 
}
公共T-myt法(T-other); ... } 公共类MyClass实现MyInterface{ //必须在实现中引用此类 公共MyClass myMethod(MyClass其他){ ... } }
这在某种程度上强化了T是实现接口的类:

public interface MyInterface<T extends MyInterface<T>> {
   public T myMethod(T other);
   ...
}
公共接口MyInterface{
公共T-myt法(T-other);
...
}

格式不错。我得查资料来源看看你是怎么做到的。太棒了。我看不出你对什么印象深刻,但谢谢。)我以前认为不能在代码块中使用斜体。您使用了pre/code而不是标记缩进。也许没什么让人印象深刻的,但我确实从你身上学到了东西,因此我最真诚的感谢你。你应该检查jdk或谷歌收藏库,了解你正在寻找的模式。我想您可能想要类似的东西。@poly:我刚刚打开了Markdown帮助页面,读到“缩进四个空格以创建转义的
块”,但刚刚看到“
”并使用了它。:)然后我想用星号来强调一些事情,得到了更好的结果(我喜欢这种格式)!所以,我完全是无意的,但非常欢迎你!嗯。它看起来很笨重,但它正是我想要的。我可能会使用这个,谢谢。公共类Foo实现了我的接口{public Bar myMethod(Bar other){//这不是你想要的,是吗?}}我将自己编写实现这个的任何类,所以我会确保我不会这样做。我不是在寻找一种方法来强制执行它,只是想知道在方法中没有if检查是否可行。(尽管我猜这两种情况都归结为先决条件)事实并非如此。考虑(1)抽象类MyPy1实现MyCuffe{};(2) 抽象类MyImpl2实现MyInterface{}。第二个不符合High Five的要求。我添加了一个“稍微”的限定词。我想这已经是High Five想要的了。再一次,我会自己写这些,所以我不需要那种束缚或遭遇。我只是想知道如果不检查方法是否可行。
public interface MyInterface<T extends MyInterface<T>> {
   public T myMethod(T other);
   ...
}