Java 中的向上裝箱和向下裝箱是什麼?
型別強制轉換是將一個數據型別轉換為另一種資料型別。
向上強制轉換 - 將子類型別轉換為超類型別稱為向上強制轉換。
示例
class Super { void Sample() { System.out.println("method of super class"); } } public class Sub extends Super { void Sample() { System.out.println("method of sub class"); } public static void main(String args[]) { Super obj =(Super) new Sub(); obj.Sample(); } }
向下強制轉換 - 將超類型別轉換為子類型別稱為向下強制轉換。
示例
class Super { void Sample() { System.out.println("method of super class"); } } public class Sub extends Super { void Sample() { System.out.println("method of sub class"); } public static void main(String args[]) { Super obj = new Sub(); Sub sub = (Sub) obj; sub.Sample(); } }
廣告