简体   繁体   English

有没有办法在最终方法中返回当前的 class 类型?

[英]Is there a way to return current class type in a final method?

I have the following situation in java;我在java有如下情况; There is a class that implements a method that is meant to be final but I need the returning type of such method to be exactly that of the class that it calling it.有一个 class 实现了一个 final 方法,但我需要这种方法的返回类型恰好是它调用它的 class 的返回类型。 Since I want the method to be final I can't modify the return data type in the subclasses since that would be illegal.因为我希望方法是最终的,所以我不能修改子类中的返回数据类型,因为那是非法的。

Is there a way to archive this without sacrificing finality and having to override such method for all subclasses that implement it?.有没有一种方法可以在不牺牲最终性并且不必为实现它的所有子类覆盖这种方法的情况下存档它? Even if the method was not final it would still be faster to implement it this way.即使该方法不是最终的,以这种方式实现它仍然会更快。

The code would be something like this:代码将是这样的:

class Parent
{
    public currentClass finalMethod() {...}
}

class Children extends Parent {}

public static void main(String args[])
{
    Children c = new Children();
    System.out.print(c.finalMethod().getClass().getName()); // Would print Children
}

Tried to use reflection and generics to no avail.尝试使用反射和 generics 无济于事。 Examples:例子:

class Parent
{
    public this.getClass() finalMethod() {...} //Many Errors
    public <extends Parent> finalMethod() {...} // Returns Parent even if called by Child
    public Class<? extends Parent>[] getVecinos() // Returns Class<? extends Parent> thus can't use polymorphism which is the use case
}

I know I could use casting but still not the best solution.我知道我可以使用铸造,但仍然不是最好的解决方案。

The default behavior is that you'll get what you're looking for, as long as finalMethod does not generate new instances using other types.默认行为是你会得到你正在寻找的东西,只要finalMethod不使用其他类型生成新实例。

To use your code:要使用您的代码:

class Parent {
    public final Parent finalMethod() {
        return this;
    }
}

With that, c.finalMethod().getClass().getName() returns Children .这样, c.finalMethod().getClass().getName()返回Children That's because this inside finalMethod() is the same object that was created using new Children() and getClass() returns the runtime class of the object.这是因为finalMethod()内部的this与使用new Children()创建的 object 相同,而getClass()返回 object 的运行时 class。

That uses inheritance and as long as you can work with Parent as the return type, you should be fine.它使用 inheritance,只要您可以使用Parent作为返回类型,就应该没问题。 If, however, your objective is to call methods specific to Children on the return value of finalMethod() , you may need to make Parent generic.但是,如果您的目标是在finalMethod()的返回值上调用特定于Children的方法,您可能需要使Parent通用。 Something like this:是这样的:

class Parent<C extends Parent<C>> {
    public final Parent<C> finalMethod() {
        //whatever
        return this;
    }
}
class Children extends Parent<Children> {
}

And that would make the following code valid, still producing the same output:这将使以下代码有效,仍然产生相同的 output:

Parent<Children> c = new Children(); //or Children c = new Children();
System.out.print(c.finalMethod().getClass().getName());

This has the advantage of allowing you to make static reference to Child -specific methods without prior casting.这样做的好处是,您可以 static 引用Child特定方法,而无需事先转换。 But it's not the right solution unless it's okay to make Parent generic.但这不是正确的解决方案,除非可以使Parent通用。 It's also not fail-safe.它也不是万无一失的。

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

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