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();
}
}廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP