有没有办法在最终方法中返回当前类类型?
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认行为是,只要
finalMethod
不使用其他类型生成新实例,您就会获得所需的内容。使用您的代码:
这样,
c.finalMethod().getClass().getName()
返回Children
。这是因为finalMethod()
中的this
与使用new Children()
和getClass()
创建的对象是同一个对象> 返回对象的运行时类。它使用继承,只要您可以使用
Parent
作为返回类型,就应该没问题。但是,如果您的目标是在finalMethod()
的返回值上调用特定于Children
的方法,则可能需要使Parent
通用。像这样的东西:这将使以下代码有效,仍然产生相同的输出:
这样做的优点是允许您对
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:
With that,
c.finalMethod().getClass().getName()
returnsChildren
. That's becausethis
insidefinalMethod()
is the same object that was created usingnew Children()
andgetClass()
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 toChildren
on the return value offinalMethod()
, you may need to makeParent
generic. Something like this:And that would make the following code valid, still producing the same output:
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 makeParent
generic. It's also not fail-safe.