Way to make Java parent class method return object of child class

后端 未结 7 461
北海茫月
北海茫月 2020-12-04 22:23

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

相关标签:
7条回答
  • 2020-12-04 22:29
    public class Parent {
        public Parent myMethod(){
            return this;
        }
    }
    public class Child extends Parent {}
    

    And invoke it like

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

    Is this solution is correct? If it is, then, how is it different from the #1 solution?

    0 讨论(0)
  • 2020-12-04 22:31

    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
    
    0 讨论(0)
  • 2020-12-04 22:31

    Simply to demonstrate:

    public Animal myMethod(){
      if(this isinstanceof Animal){
         return new Animal();
      }
      else{
    
         return this.getClass().newInstance();
      }
    }
    
    0 讨论(0)
  • 2020-12-04 22:35

    What are you trying to achieve ? It sounds like a bad idea. A parent class should not know anything about its children. It seems awfully close to breaking the Liskov Substitution Principle. My feeling is that your use case would be better serve by changing the general design, but hard to say without more informations.

    Sorry to sound a bit pedantic, but I get a bit scared when I read such question.

    0 讨论(0)
  • 2020-12-04 22:38

    I know exactly what you mean, in Perl there is the $class variable which means if you call some factory method on a subclass, even if it is not overridden in the subclass, if it instanciates any instances of $class an instance of the subclass will be created.

    Smalltalk, Objective-C, many other languages have a similar facility.

    Alas, there is no such equivalent facility in Java.

    0 讨论(0)
  • 2020-12-04 22:41

    If you are using Kotlin, you can create an extension function

    abstract class SuperClass
    class SubClass: SuperClass()
    
    fun <T : SuperClass> T.doSomething(): T {
        // do something
        return this
    }
    
    val subClass = SubClass().doSomething()
    
    0 讨论(0)
提交回复
热议问题