Java 抽象方法中声明的实现的动态返回类型

Java 抽象方法中声明的实现的动态返回类型,java,generics,abstract,Java,Generics,Abstract,我正在寻找一种方法,使抽象方法中的返回类型成为调用该方法的实现之一 换句话说,我想这样写: public class GenericClass { public <T extends GenericClass> T returnMyself() { return (T)this; // Compiler warning, unsafe cast } } public class Implem1 extends GenericClass {} public cla

我正在寻找一种方法,使抽象方法中的返回类型成为调用该方法的实现之一

换句话说,我想这样写:

public class GenericClass {

  public <T extends GenericClass> T returnMyself() {
    return (T)this; // Compiler warning, unsafe cast
  }
}

public class Implem1 extends GenericClass {}

public class Implem2 extends GenericClass {}

public class Main {

  public static void main(String[] args) {
    Implem1 implem1 = new Implem1();
    Implem1 again1 = implem1.returnMyself(); // Works fine, the type is inferred by the type of again1, I think
    Implem1 again2 = implem1.<Implem1>returnMyself(); // Works fine, the type is explicitly asked by <Implem1>
    Implem2 again3 = implem1.returnMyself(); // Works fine while it shouldn't. 
  }

}
公共类GenericClass{
公共交通工具{
返回(T)this;//编译器警告,不安全强制转换
}
}
公共类Implem1扩展了GenericClass{}
公共类Implem2扩展了GenericClass{}
公共班机{
公共静态void main(字符串[]args){
Implem1 Implem1=新的Implem1();
Implem1 again1=Implem1.returnSelf();//工作正常,我认为类型是由again的类型推断出来的
Implem1 again2=Implem1.returnimf();//工作正常,类型由
Implem2 again3=implem1.returnSelf();//工作正常,但不应该。
}
}
我要寻找的是一种声明方法的方法,以便在编译时,returnIlf()只能返回调用它的实现的类型(在我的示例中,implem1的类型是implem1),并确保调用的代码不会错误/混合这些类型

我搜索了很多,但在任何地方都找不到我的答案(有些主题看起来很相似,但需要更一般的情况,而不是显式地调用方法的实现类型)

有些答案是正确的,但总是意味着要覆盖每个实现类中的方法,这在我这方面可能会很麻烦并且容易出现错误。理想情况下,我正在寻找一种在抽象类中只需编写一次的方法


感谢任何帮助/回答:D

因为您希望在子类中编写这些方法,所以这似乎是可能的。只要新返回类型是旧返回类型的扩展,就可以重写方法并使用不同的返回类型:


您可以这样做:

public class Parent {
    public Parent returnMyself() {
        return this;
    }
}

public class Child extends Parent {
    public Child returnMyself() {
        return this;
    }
}

这没有问题,因为如果将
子实例
存储在
父变量
中,则需要
父变量
返回类型为
returnimf()
。即使它实际上是一个
Child
对象,由
returnimf()
返回的
Child
也扩展了
Parent

既然您在问题描述中提到了抽象,那么遵循一个类似于Java类的模型如何,其中泛型类型在类定义中

public abstract class GenericClass<T extends GenericClass> {

    public abstract T returnMyself();
}

public class Implem1 extends GenericClass<Implem1> {

    @Override
    public Implem1 returnMyself() {
        return this;
    }
}

public class Implem2 extends GenericClass<Implem2> {

    @Override
    public Implem2 returnMyself() {
        return this;
    }
}

public class Main {

    public static void main(String[] args) {
        Implem1 implem1 = new Implem1();
        Implem1 again1 = implem1.returnMyself(); // Works fine
        Implem1 again2 = implem1.returnMyself(); // Works fine
        Implem2 again3 = implem1.returnMyself(); // Does not compile 
    }

}
公共抽象类GenericClass{
公开摘要不返回本人();
}
公共类Implem1扩展了GenericClass{
@凌驾
公开执行1返回我自己(){
归还这个;
}
}
公共类Implem2扩展了GenericClass{
@凌驾
公开执行2返回我自己(){
归还这个;
}
}
公共班机{
公共静态void main(字符串[]args){
Implem1 Implem1=新的Implem1();
Implem1 again1=Implem1.returnSelf();//工作正常
Implem1 again2=Implem1.returnSelf();//工作正常
Implem2 again3=implem1.RETURNIMPLE();//不编译
}
}

虽然这样做很有效,但它需要重写每个子类来编写几乎相同的代码片段(返回类型除外)。我希望能够在抽象类中一劳永逸地编写它。。。但我想这在Java中是不可能的?接受答案是因为这是最接近我问题的答案,尽管我希望有一种更智能的方式:(无论如何,谢谢你!确切地说,我不想在每个子类中都再次编写它:DYou可以返回一个对象:P我不确定你能做你想做的事。谢谢,这正是我想要的。噢,懒惰的日子里,你只是为了一些代码而不是通过javadocs阅读stackflow,喜欢它:P