我们可以在 Java 中强制转换引用变量吗?
java 8object oriented programmingprogramming更新于 2025/9/21 10:20:17
Java 提供各种数据类型来存储各种数据值。它提供 7 种原始数据类型(存储单个值),即布尔值、字节、字符、短整型、整型、长整型、浮点型、双精度型和引用数据类型(数组和对象)。
Java 中的强制转换
将一种原始数据类型转换为另一种原始数据类型称为类型强制转换。
示例
import java.util.Scanner;
public class TypeCastingExample {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("输入一个整数值:");
int i = sc.nextInt();
long num = i;
System.out.println("给定整数的值:"+num);
}
}
输出
输入一个整数值: 421
在引用数据类型中进行转换
是的,您可以将一种(类)类型的引用(对象)转换为另一种类型。但是,两个类中的一个应该继承另一个。
例如,假设我们有一个名为Person的类,它有两个实例变量name和age,以及一个显示name和age的实例方法displayPerson()。
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("Person 类的数据:");
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) {
//创建 Student 类的对象
Student student = new Student("Krishna", 20, "IT", 1256);
//将 Student 的对象转换为 Person
Person person = new Person("Krishna", 20);
//将 student 的对象转换为 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) {
//创建 Student 类的对象
Student student = new Student("Krishna", 20, "IT", 1256);
//将 Student 的对象转换为 Person
Person person = new Person("Krishna", 20);
//将 person 的对象转换为 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) {
//将 Student 对象转换为 Person
Person person = new Student("Krishna", 20, "IT", 1256);
//将 person 对象转换为 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

