Chain methods for a subclass object, methods returning it from both classes

I have 2 classes:

public class A {
    private final List<String> list;

    public A() {        
        list = new ArrayList<String>();
    }   

    public A first(String s) {      
        list.add(s);
        return this;
    }   

    public A second() {
        System.out.println(list);
        return this;
    }
}

public class B extends A {
    public B bfisrt() {
        System.out.println("asd");
        return this;
    }
}

and the class with the main, the following code in the main work

B b = new B();

b.first("unu")
    .second();   

b.bfirst(); 

but I would like to bind methods from both classes on the same object. Is it possible? how

B b = new B();

b.first("unu")
    .second()   
    .bfisrt();  
+4
source share
4 answers

Let me break it.

public A first(String s){       
    list.add(s);
    return this;
}   

The return type of the method is A, therefore, the call new B().first("hi")returns an object of type A. Therefore, when I tried to compile, I expected errors by saying incompatible types.

You could, like label comments, override the method and do the same, but return B:

public B first(String s){  
    super.first( s );     
    return this;
}   

or even

public B first(String s) {  
    return (B)super.first(s);     
}   

Kesheva , , A, B.

B b = new B();
((B)b.first("unu").second()).bfisrt();

, , , .

, .

public abstract class A {
    public <Unknown extends A> Unknown first(String s) {
        System.out.println("in here");
        return (Unknown)this;
    }
}

public class B extends A { }

public static void main(String[] args) {
   //compiles without overriding the method or manually casting. 
    B b = new B().first("hi").first("hello");
}

fooobar.com/questions/1549886/... , .

:, newacct, , , , . :

B b = new B().first("hi").first("hello"); 
// above compiles and works. You assign 'B b' to a `new B()`

class C extends A { }
C c = new B().first("hi"); 
// ClassCastException, but you can see that instantly. 'C c' gets assigned to 'new B()'
+4

. A B, .

public class B extends A{

    public B bfisrt(){
        System.out.println("asd");
        return this;
    }
    public B first(String s){  
        super.first( s );     
        return this;
    }   
    public B second(){
        super.second();
        return this;
    }
}

( !), futz , .

+1

You can try pouring

B b=new B(); ((B)b.first("unu").second()).bfisrt();

Hope this helps, Kesava.

0
source

Found a solution:

public abstract class X<T extends X<T>> {

  public abstract T self();

  public T xFirst(){
      System.out.println("xxx");
      return self();
  } 
}  



public class Y extends X<Y>{    

   public Y yfirst(){
        System.out.println("yyy");  
        return self();
    }

  @Override
  public Y self() {     
        return this;
    }
   }  

and in the main chain can be used without inheritance

 new Y().xFirst().yfirst().xFirst().yfirst();
0
source

All Articles