如何在Java中自动降级类?

huangapple go评论67阅读模式
英文:

How to automatically downcast classes in java?

问题

在Java中,你可以创建一个通用的downcast方法来实现自动向下转型,而不需要显式类型检查。你可以这样定义这个方法:

public <T> T downcast(Parent parent) {
    return (T) parent;
}

然后,你可以像这样使用它:

Boy kid = downcast(father);
Girl kiddette = downcast(mother);

这个方法的泛型类型参数 <T> 允许你在调用时指定所期望的子类类型,而方法内部的强制类型转换 (T) parent 将执行向下转型。请注意,这种方法仍然会进行强制类型转换,但是你不需要在调用时显式进行类型检查(instanceof)。这个方法会根据你所传递的目标类型自动执行向下转型。

英文:

How would you automatically downcast in Java?

Look at this example:

public class Parent {}

public class Boy extends Parent {}

public class Girl extends Parent {}

Parent father = new Boy();
Parent mother = new Girl();

Boy kid = (Boy)father;
Girl kiddette = (Girl)mother;

I know I can downcast manually like in example above, but, how would I do it automatically?

So, what I want is a autocast method, something like this:

public ? downcast(? parent) { return ? parent; }

which I would use like this:

Boy kid = downcast(father);
Girl kiddette = downcast(mother);

So, I do not want to perform any explicit casting in the caller's method. I need downcast to hide the casting within itself. Preferrably, I would like to avoid type checks (instanceof) within the downcast method, so, if downcasting can be generalized, that would be great.

What do I need to place instead of ?, ? and ? to make it work.

I remember that I did this exact same thing 10 or so years ago in Java, and I remember that I was puzzled by the code, but it worked and I did not understand how it works just that it does, but I did not need to do this thing in Java again in the last 10 years, so I forgot what I did. And I remember that I managed to solve it without type checks. So, one line of code that worked for no matter how many child classes parrent class had.

答案1

得分: 2

以下是翻译好的内容:

你可以滥用泛型来实现类似这样的功能:

private static <F, T extends F> T cast(F value) {
    return (T) value;
}

现在你可以这样做:

Parent father = new Boy();
Parent mother = new Girl();
Boy kid = cast(father);
Girl kiddette = cast(mother);

这可以工作是因为编译器从 kid 变量中推断出目标类型。但这也是危险的,正如在 (T) value 上生成的警告所示:“未经检查的转换:'F' 到 'T'”。一般来说,你不应该这样做。你所做的只是欺骗编译器执行通常不允许的强制转换。

英文:

You can abuse generics to achieve something like this:

private static &lt;F, T extends F&gt; T cast(F value) {
    return (T) value;
}

Now you can do

Parent father = new Boy();
Parent mother = new Girl();
Boy kid = cast(father);
Girl kiddette = cast(mother);

This works because the compiler infers the target type from the kid variables. It's also dangerous, as evidenced by the generated warning on (T) value: "Unchecked cast: 'F' to 'T'". In general, you don't want to do this. All you are doing is tricking the compiler into casts that normally wouldn't be allowed.

huangapple
  • 本文由 发表于 2023年6月26日 23:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76558070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定