简体   繁体   English

链接在Java中调用对象和继承

[英]Chained calls to an object and inheritance in Java

When declaring a method for chained calls usually it returns this at the end of the method. 在为链式调用声明方法时,通常会在方法结束时returns this方法。

So I declare: 所以我宣布:

public class Foo {

    public Foo setTitle(String title){
        ...
        return this;
    }

}

And: 和:

public class Bar extends Foo{

      /* OTHER STUFF */
}

If you call new Bar().setTitle("Test") it returns a Foo 's reference. 如果你调用new Bar().setTitle("Test")它会返回一个Foo的引用。

Is possible to declare the method in order to return automatically a Bar 's reference without override the method in Bar for clarity, brevity and maintainability? 为了清晰,简洁和可维护性,是否可以声明方法以自动返回Bar的引用而不覆盖Bar中的方法

Thanks 谢谢

Is possible to declare the method in order to return automatically a Bar's reference without override the method in Bar for clarity, brevity and maintainability? 为了清晰,简洁和可维护性,是否可以声明方法以自动返回Bar的引用而不覆盖Bar中的方法?

No. You could hook up some weird generics - Foo<T extends Foo> or the like - but it wouldn't be very satisfactory. 不。你可以勾选一些奇怪的仿制品Foo<T extends Foo>等 - 但它不会很令人满意。

Basically there's need to be some language support for "this type", where the only valid expressions of that type were null and this . 基本上需要为“this type”提供一些语言支持,其中该类型的唯一有效表达式为nullthis That doesn't exist, so you're left with overriding: 那不存在,所以你留下了压倒一切:

public Bar setTitle(String title) {
    super.setTitle(title);
    return this;
}

Or: 要么:

public Bar setTitle(String title) {
    return (Bar) super.setTitle(title);
}

It's just one of those cases where inheritance ends up being a pain :( 这只是继承最终成为痛苦的案例之一:(

I did try with generics... 我确实尝试过仿制药......

public class Foo<T extends Foo> {

    public T setTitle(String title){
        .....
        return (T) this;
    }
}

public class Bar extends Foo<Bar>{

    /* OTHER STUFF */
}

Seems to work. 似乎工作。

I think its actullay returning Bar Only. 我认为它的actullay返回Bar Only。 Just do the type casting to reference the object as Bar as below: 只需进行类型转换即可将对象引用为Bar ,如下所示:

    Bar bar = (Bar)(new Bar().setTitle("Foo Title"));
    System.out.println(bar.getTitle());//prints Foo Title

Assuming getTitle() method present in Foo returning the title. 假设Foo存在getTitle()方法返回标题。

If you add another method eg getBarTitle in Bar class: 如果你在Bar类中添加另一个方法,例如getBarTitle

      public String getBarTitle(){
          return getTitle()+" of Bar";
      }

then 然后

  System.out.println(bar.getBarTitle());//prints Foo Title of Bar

Explanation: When you are calling setTitle on new Bar() , the method is called on Bar Object and this is representing Bar object not Foo , hence it returns Bar onject only with class type as Foo , which is super class. 说明:当您在new Bar()上调用setTitle时,该方法在Bar Object上调用, this表示Bar对象不是Foo ,因此它只返回Bar onject,类类型为Foo ,这是超类。 In this process, it doesn't change the original class type, which is bar at all, hence the type casting should serve the purpose for you. 在这个过程中,它不会改变原始的类类型,它根本就是条形,因此类型转换应该适合你的目的。

You can use generics with upper bounds: 您可以使用具有上限的泛型:

public class Foo<T extends Foo<T>> {
    public T setTitle() {
        ...
        return (T) this;
    }
}

public class Bar extends Foo<Bar> {
    ....
}

To be precide, it does not return a Foo reference, it returns a Bar reference, you can check by using System.out.println(reference.getClass().getName()). 为了预防,它不返回Foo引用,它返回一个Bar引用,您可以使用System.out.println(reference.getClass()。getName())进行检查。

In the same way when you write, 当你写作时,以同样的方式,

List l=new ArrayList(); 

(forget about LIst being an interface, it is beside the point) (忘记LIst是一个界面,它是旁边的点)

l is actually a reference to ArrayList even if this information is "hidden", but calling l.someMathod() calls ArrayList methods, not List methods - that's how Object Orientation works! l实际上是对ArrayList的引用,即使这个信息是“隐藏”的,但调用l.someMathod()调用ArrayList方法,而不是List方法 - 这就是Object Orientation的工作原理!

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

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