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

发布于 2025-01-17 03:36:50 字数 955 浏览 0 评论 0原文

我在java中有以下情况;有一个类实现了一个最终方法,但我需要该方法的返回类型与其调用它的类完全相同。由于我希望该方法是最终方法,因此我无法修改子类中的返回数据类型,因为这是非法的。

有没有一种方法可以在不牺牲最终性并且不必为实现它的所有子类覆盖此类方法的情况下存档此方法?即使该方法不是最终的,以这种方式实现它仍然会更快。

代码将是这样的:

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
}

尝试使用反射和泛型但无济于事。示例:

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 have the following situation in 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. 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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2025-01-24 03:36:50

默认行为是,只要 finalMethod 不使用其他类型生成新实例,您就会获得所需的内容。

使用您的代码:

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

这样,c.finalMethod().getClass().getName() 返回Children。这是因为 finalMethod() 中的 this 与使用 new Children()getClass() 创建的对象是同一个对象> 返回对象的运行时类。

它使用继承,只要您可以使用 Parent 作为返回类型,就应该没问题。但是,如果您的目标是在 finalMethod() 的返回值上调用特定于 Children 的方法,则可能需要使 Parent 通用。像这样的东西:

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

这将使以下代码有效,仍然产生相同的输出:

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

这样做的优点是允许您对 Child 特定的方法进行静态引用,而无需事先进行转换。但这不是正确的解决方案,除非可以使 Parent 通用。它也不是万无一失的。

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.

To use your code:

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

With that, c.finalMethod().getClass().getName() returns 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.

That uses inheritance and as long as you can work with Parent as the return type, you should be fine. If, however, your objective is to call methods specific to Children on the return value of finalMethod(), you may need to make Parent generic. 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:

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. But it's not the right solution unless it's okay to make Parent generic. It's also not fail-safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文