简体   繁体   English

为什么需要多个类型铸造?

[英]Why is Multiple Type-Casting Required?

Consider the following hierarchy: 考虑以下层次结构:

TopClass
      |__ MiddleClass
                  |__ BottomClass 

Then the following code is of-course not required: 然后,当然不需要以下代码:

public BottomClass getBottom() {
    return (BottomClass) (MiddleClass) getObject();
}

Where getObject() returns an instance of type BottomClass but has a return type of TopClass . 其中getObject()返回BottomClass类型的实例,但返回类型为TopClass

You could effectivily short-circuit it, and cast directly to BottomClass . 您可以有效地使其短路,然后直接转换为BottomClass


But this code raised my brows: 但是这段代码引起了我的注意:

In the JavaFX source package, class: com.sun.javafx.scene.control.skin.ProgressIndicatorSkin 在JavaFX源程序包中,类: com.sun.javafx.scene.control.skin.ProgressIndicatorSkin

@Override
public StyleableProperty<Paint> getStyleableProperty(ProgressIndicator n) {
    final ProgressIndicatorSkin skin = (ProgressIndicatorSkin) n.getSkin();
        return (StyleableProperty<Paint>)(WritableValue<Paint>)skin.progressColor;
}

Where the interface hierarchy is: 接口层次结构为:

WritableValue<T>
              |__ StyleableProperty<T>

And progressColor is of type StyleableObjectProperty<Paint> , that implements StyleableProperty<Paint> , but is stored in an ObjectProperty<Paint> variable, like this: progressColor的类型为StyleableObjectProperty<Paint> ,实现StyleableProperty<Paint> ,但存储在ObjectProperty<Paint>变量中,如下所示:

private ObjectProperty<Paint> progressColor = new StyleableObjectProperty<Paint>(null)

Any clue what's going on here? 任何线索,这是怎么回事?

The intermediary cast seems unnecessary. 中介演员似乎没有必要。 Direct cast should work. 直接投射应该可以。

However, the direct cast would be a "cross" cast - casting between two types that has no apparent subtype relationship. 但是,直接转换将是“交叉”转换-两种类型之间没有明显子类型关系的转换。

The programmer probably doesn't want that; 程序员可能不希望这样; instead, it's more comfortable for the programmer to upcast to a nearest common supertype, then do a downcast, which feels "safer". 相反,对于程序员来说,转换为最接近的普通超类型然后进行转换(感觉更“安全”)更舒服。


Discussion of casts: 演员表的讨论:

It's always safe to do an "up" cast 进行“上演”总是很安全的

    (Animal)cat

It is allowed to do a "down" cast; 允许进行“向下”转换; the compiler assumes the programmer knows better about the actual runtime type 编译器假定程序员对实际运行时类型了解得更多

    (Cat)animal

It is the "cross" cast that is a problem. 问题是“跨”演员表。 Sometimes it is obvious that the cross-cast is impossible 有时很明显,交叉转换是不可能的

    (Cat)dog   // Cat and Dog are two classes, and no subclass relation

    (List<Cat>) listDog   //  List<Dog> => List<Cat>

    (Runnable)fish    // Fish is a final class that does not implement Runnable

However, if it is not provably incorrect, the compiler would allow it, trusting the programmer 但是,如果不是可证明的错误,则编译器会允许它,并信任程序员

     (Runnable)animal   // Animal class does not implement Runnable; but a subclass may

We can always cast between any two types by going through a common supertype 我们总是可以通过常见的超类型在任何两种类型之间进行转换

     (List<Cat>)(List<?>) listDog

Of course, Object is the common supertype of all types, so we can use it to force any casts 当然,Object是所有类型的通用超类型,因此我们可以使用它来强制进行任何强制类型转换

     (Cat)(Object)dog

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

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