繁体   English   中英

使Java父类方法返回子类对象的方法

[英]Way to make Java parent class method return object of child class

当从子类对象调用此方法时,是否有任何优雅的方法使位于父类中的 Java 方法返回子类的对象?

我想在不使用额外接口和额外方法的情况下实现它,并且在没有类转换、辅助参数等的情况下使用它。

更新:

对不起,我不是很清楚。

我想实现方法链,但是我对父类的方法有问题:当我调用父类方法时,我无法访问子类方法......我想我已经提出了我的想法的核心。

所以这些方法应该返回this.getClass()类的this对象。

如果您只是在寻找针对已定义子类的方法链接,那么以下方法应该有效:

public class Parent<T> {

  public T example() {
    System.out.println(this.getClass().getCanonicalName());
    return (T)this;
  }
}

如果您愿意,这可以是抽象的,然后是一些指定通用返回类型的子对象(这意味着您无法从 ChildA 访问 childBMethod):

public class ChildA extends Parent<ChildA> {

  public ChildA childAMethod() {
    System.out.println(this.getClass().getCanonicalName());
    return this;
  }
}

public class ChildB extends Parent<ChildB> {

  public ChildB childBMethod() {
    return this;
  }
}

然后你像这样使用它

public class Main {

  public static void main(String[] args) {
    ChildA childA = new ChildA();
    ChildB childB = new ChildB();

    childA.example().childAMethod().example();
    childB.example().childBMethod().example();
  }
}

输出将是

org.example.inheritance.ChildA 
org.example.inheritance.ChildA 
org.example.inheritance.ChildA 
org.example.inheritance.ChildB 
org.example.inheritance.ChildB

你想达到什么目的? 这听起来是个坏主意。 父类不应该对其子类一无所知。 这似乎非常接近于打破Liskov 替换原则 我的感觉是,通过更改总体设计,您的用例会得到更好的服务,但如果没有更多信息,就很难说。

抱歉听起来有点迂腐,但当我读到这样的问题时,我有点害怕。

简单演示一下:

public Animal myMethod(){
  if(this isinstanceof Animal){
     return new Animal();
  }
  else{

     return this.getClass().newInstance();
  }
}

您可以调用this.getClass()来获取运行时类。

但是,这不一定是调用该方法的类(它甚至可以在层次结构的更下方)。

并且您需要使用反射来创建新实例,这很棘手,因为您不知道子类具有什么样的构造函数。

return this.getClass().newInstance(); // sometimes works

我确切地知道你的意思,在 Perl 中有$class变量,这意味着如果你在子类上调用一些工厂方法,即使它没有在子类中被覆盖,如果它实例化了$class的任何实例是子$class的实例将被创建。

Smalltalk、Objective-C,许多其他语言都有类似的功能。

唉,Java 中没有这样的等效工具。

如果你使用 Kotlin,你可以创建一个扩展函数

abstract class SuperClass
class SubClass: SuperClass()

fun <T : SuperClass> T.doSomething(): T {
    // do something
    return this
}

val subClass = SubClass().doSomething()
public class Parent {
    public Parent myMethod(){
        return this;
    }
}
public class Child extends Parent {}

并像这样调用它

       Parent c = (new Child()).myMethod();
       System.out.println(c.getClass());

这个解决方案是否正确? 如果是,那么它与#1 解决方案有何不同?

暂无
暂无

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

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