Java 链接子类对象上的方法,这些方法从两个类返回

Java 链接子类对象上的方法,这些方法从两个类返回,java,this,subclass,Java,This,Subclass,我有两门课: 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

我有两门课:

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;
    }
}
但是我想在同一个对象上链接来自这两个类的方法。有可能吗?像

B b = new B();

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

我想是的。您必须重写B中A的方法才能使其工作

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;
    }
}
我还没有测试过这个(已经很晚了!),所以仔细检查一下,如果有必要的话再试试。

你可以试试铸造

bb=新的B();
((B)B.first(“联合国大学”).second()).bfisrt()

希望这有帮助, 凯沙瓦。

让我们把它分解一下

public A first(String s){       
    list.add(s);
    return this;
}   
方法的返回类型是
A
,因此调用
new B()。首先(“hi”)
返回类型为
A
的对象。因此,当我试图编译时,我预期会得到一个错误,即
不兼容的类型

您可以像markspace answers一样,重写该方法并执行相同的操作,但返回a
B

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

public B first(String s) {  
    return (B)super.first(s);     
}   
Kesheva的方法要求您在返回类型为
A
时手动强制转换,但您知道它是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");
}
找到的解决方案:

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

好吧,试试看是否可能……谢谢,它与重写一起工作。在我正在处理的这个项目中,超类有很多方法返回这个结果,覆盖它是没有用的。对于
first()
方法实现,我将坚持使用+1的代码,使用泛型,完美的解决方案。我已经根据你的解决方案改变了A中的方法。但我仍然无法将类B的方法链接到B.first(“s”).second()。在eclipse中键入最后一点后,默认的方法建议甚至没有显示类B.Mm的方法,您是对的。这与编译器如何推断类型有关。这是可行的(
bb=new B().classAMethod1().classAMethod2();B.classBMethod()
),但不幸的是,这不可行:
bb=new B().classAMethod1().classAMethod2().classBMethod()
(未知)此
cast几乎就是不安全的定义。你正在铸造一个未知的类型@newacct您是对的,但是如果您遵循构建器模式,则很可能不会得到ClassCastException。请参见编辑
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()'
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;
    }
   }  
 new Y().xFirst().yfirst().xFirst().yfirst();