Java 中可以轉換引用變數嗎?
Java 提供了各種資料型別來儲存各種資料值。它提供了 7 種基本資料型別(儲存單個值),即 boolean、byte、char、short、int、long、float、double 以及引用資料型別(陣列和物件)。
Java 中的型別轉換
將一種基本資料型別轉換為另一種稱為型別轉換。
示例
import java.util.Scanner; public class TypeCastingExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer value: "); int i = sc.nextInt(); long num = i; System.out.println("Value of the given integer: "+num); } }
輸出
Enter an integer value: 421
引用資料型別中的型別轉換
是的,您可以將一個(類)型別的引用(物件)轉換為另一個型別。但是,這兩個類之一必須繼承另一個類。
例如,假設我們有一個名為 Person 的類,它有兩個例項變數 name 和 age,以及一個例項方法 displayPerson(),該方法顯示 name 和 age。
public class Person{ private String name; private int age; public Person(String name, int age){ this.name = name; this.age = age; } public void displayPerson() { System.out.println("Data of the Person class: "); System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); } }
另一個名為 Student 的類擴充套件了 Person 類,除了繼承的 name 和 age 之外,它還有兩個變數 branch 和 student_id。它有一個方法 displayData(),該方法顯示所有四個值。
public class Student extends Person { public String branch; public int Student_id; public Student(String name, int age, String branch, int Student_id){ super(name, age); this.branch = branch; this.Student_id = Student_id; } public void displayStudent() { System.out.println("Data of the Student class: "); System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); System.out.println("Branch: "+this.branch); System.out.println("Student ID: "+this.Student_id); } public static void main(String[] args) { .............. } }
將子類變數轉換為超類型別
現在,在 main 方法中,您可以分別建立這兩個類的物件,並將超類物件直接轉換為子類。
public static void main(String[] args) { //Creating an object of the Student class Student student = new Student("Krishna", 20, "IT", 1256); //Converting the object of Student to Person Person person = new Person("Krishna", 20); //Converting the object of student to person person = (Student) student; person.displayPerson(); }
輸出
Data of the Person class: Name: Krishna Age: 20
簡而言之,超類引用變數可以持有子類物件。但是,使用此引用,您只能訪問超類成員,如果您嘗試訪問子類成員,則會生成編譯時錯誤。
示例
public static void main(String[] args) { Person person = new Student("Krishna", 20, "IT", 1256); person.displayStudent(); }
輸出
Student.java:33: error: cannot find symbol person.dispalyStudent(); ^ symbol: method dispalyStudent() location: variable person of type Person 1 error
將超類變數轉換為子類
同樣,您可以嘗試將超類變數轉換為子類,為此,與前面的情況不同,您需要使用強制轉換運算子。
示例
public static void main(String[] args) { //Creating an object of the Student class Student student = new Student("Krishna", 20, "IT", 1256); //Converting the object of Student to Person Person person = new Person("Krishna", 20); //Converting the object of person to student student = (Student) person; student.displayPerson(); student.displayStudent(); }
對於此引用,兩個類的成員都可用,並且程式成功編譯。
但是,當您嘗試執行它時,會引發如下所示的異常:
Exception in thread "main" java.lang.ClassCastException: ther.Person cannot be cast to ther.Student at ther.Student.main(Student.java:41)
要解決此問題,首先需要使用子類物件建立超類引用,然後使用強制轉換運算子將此(超)引用型別轉換為子類型別。
示例
public static void main(String[] args) { //Converting the object of Student to Person Person person = new Student("Krishna", 20, "IT", 1256); //Converting the object of person to student Student student = (Student) person; student.displayPerson(); student.displayStudent(); }
輸出
Data of the Person class: Name: Krishna Age: 20 Data of the Student class: Name: Krishna Age: 20 Branch: IT Student ID: 1256
廣告