Providing upcasting in java explicitly

前端 未结 1 779
你的背包
你的背包 2021-01-28 01:30
class P {
    void a() {
        System.out.println(\"a\");
    }
    void b() {
        System.out.println(\"b\");
    }
}

class C extends P {
    void c() {
        S         


        
相关标签:
1条回答
  • 2021-01-28 01:51
    P p = c;
    

    Don't cast at all. A C is a P; no explicit conversion is required. With no cast, you make it clear that this operation cannot fail; with a cast, you force the reader to consider and discard the possibility that c is not an instance of P.

    0 讨论(0)
提交回复
热议问题