解释Java中对象的缩窄。


Java提供各种数据类型来存储各种数据值。它提供7种基本数据类型(存储单个值),即boolean、byte、char、short、int、long、float、double,以及引用数据类型(数组和对象)。

类型转换/类型转换 - 将一种基本数据类型转换为另一种称为Java中的类型转换(类型转换)。您可以通过两种方式转换基本数据类型,即扩展和缩窄。

缩窄 - 将较高数据类型转换为较低数据类型称为缩窄。在这种情况下,转换不会自动进行,您需要使用强制转换运算符“( )”显式转换。因此,它被称为显式类型转换。在这种情况下,两种数据类型不必彼此兼容。

示例

import java.util.Scanner;
public class NarrowingExample {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter an integer value: ");
      int i = sc.nextInt();
      char ch = (char) i;
      System.out.println("Character value of the given integer: "+ch);
   }
}

输出

Enter an integer value:
67
Character value of the given integer: C

对象的缩窄

您可以将一个(类)类型的引用(对象)转换为另一个类型。但是,这两个类之一应该继承另一个类。

因此,如果一个类继承了另一个类的属性,则将超类对象转换为子类类型被认为是关于对象的缩窄。

但与扩展不同,在缩窄的情况下,您需要使用强制转换运算符。

示例

在下面的Java示例中,我们有两个类,即Person和Student。Person类有两个实例变量name和age,以及一个实例方法displayPerson(),它显示姓名和年龄。

Student扩展了Person类,除了继承的name和age之外,它还有两个变量branch和student_id。它有一个方法displayData(),它显示所有四个值。

在main方法中,我们分别创建了这两个类的对象,并且我们正在尝试将子类对象转换为超类类型。

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);
   }
}
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) {
      //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

更新于:2019年7月30日

2K+ 浏览量

开启你的职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.