使Java父类方法返回子类的对象的方法 [英] Way to make Java parent class method return object of child class

查看:407
本文介绍了使Java父类方法返回子类的对象的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有什么优雅的方法可以让Java方法位于子类的父类返回对​​象中,当从子类对象调用此方法时?在不使用其他接口和额外方法的情况下实现此功能,并且在不使用类转换,辅助参数等的情况下使用它。



更新:



对不起,我不太清楚。



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



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

解决方案

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

  public class Parent< T> {

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






$ b

如果你喜欢,这可能是抽象的,那么一些孩子指定泛型返回类型的对象(这意味着您无法从ChildA访问childBMethod):

  public class ChildA extends Parent< ; ChildA> {

public ChildA childAMethod(){
System.out.println(this.getClass()。getCanonicalName());
返回此;
}
}

公共类ChildB扩展父< ChildB> {

public ChildB childBMethod(){
return this;
}
}

然后你像这样使用它

  public class Main {

public static void main(String [] args){
ChildA childA =新的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


Is there any elegant way to make Java method located within parent class return object of child class, when this method is called from child class object?

I want to implement this without using additional interfaces and extra methods, and to use this without class casts, auxiliary arguments and so on.

Update:

Sorry that I was not so clear.

I want to implement method chaining, but I have problems with methods of parent class: I lose access to child class methods, when i call parent class methods... I suppose that I'v presented the core of my idea.

So the methods should return this object of this.getClass() class.

解决方案

If you're just looking for method chaining against a defined subclass, then the following should work:

public class Parent<T> {

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

which could be abstract if you like, then some child objects that specify the generic return type (this means that you can't access childBMethod from ChildA):

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;
  }
}

and then you use it like 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();
  }
}

the output will be

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

这篇关于使Java父类方法返回子类的对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆