Downcasting in Java

后端 未结 11 2109
自闭症患者
自闭症患者 2020-11-22 03:15

Upcasting is allowed in Java, however downcasting gives a compile error.

The compile error can be removed by adding a cast but would anyway break at the runtime.

11条回答
  •  盖世英雄少女心
    2020-11-22 03:22

    Downcasting transformation of objects is not possible. Only

    DownCasting1 _downCasting1 = (DownCasting1)((DownCasting2)downCasting1);
    

    is posible

    class DownCasting0 {
        public int qwe() {
            System.out.println("DownCasting0");
            return -0;
        }
    }
    
    class DownCasting1 extends DownCasting0 {
        public int qwe1() {
            System.out.println("DownCasting1");
            return -1;
        }
    }
    
    class DownCasting2 extends DownCasting1 {
        public int qwe2() {
            System.out.println("DownCasting2");
            return -2;
        }
    }
    
    public class DownCasting {
    
        public static void main(String[] args) {
    
            try {
                DownCasting0 downCasting0 = new DownCasting0();
                DownCasting1 downCasting1 = new DownCasting1();
                DownCasting2 downCasting2 = new DownCasting2();
    
                DownCasting0 a1 = (DownCasting0) downCasting2;
                a1.qwe(); //good
    
                System.out.println(downCasting0 instanceof  DownCasting2);  //false
                System.out.println(downCasting1 instanceof  DownCasting2);  //false
                System.out.println(downCasting0 instanceof  DownCasting1);  //false
    
                DownCasting2 _downCasting1= (DownCasting2)downCasting1;     //good
                DownCasting1 __downCasting1 = (DownCasting1)_downCasting1;  //good
                DownCasting2 a3 = (DownCasting2) downCasting0; // java.lang.ClassCastException
    
                if(downCasting0 instanceof  DownCasting2){ //false
                    DownCasting2 a2 = (DownCasting2) downCasting0;
                    a2.qwe(); //error
                }
    
                byte b1 = 127;
                short b2 =32_767;
                int b3 = 2_147_483_647;
    //          long _b4 = 9_223_372_036_854_775_807; //int large number max 2_147_483_647
                long b4 = 9_223_372_036_854_775_807L;
    //          float _b5 = 3.4e+038; //double default
                float b5 = 3.4e+038F; //Sufficient for storing 6 to 7 decimal digits
                double b6 = 1.7e+038;
                double b7 = 1.7e+038D; //Sufficient for storing 15 decimal digits
    
                long c1 = b3;
                int c2 = (int)b4;
    
                //int       4 bytes     Stores whole numbers from -2_147_483_648 to 2_147_483_647
                //float     4 bytes     Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits
                float c3 = b3; //logic error
                double c4 = b4; //logic error
    
    
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    
    }
    

提交回复
热议问题