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