扩展/缩小转换的实际应用? [英] Real world application of widening / narrowing conversion?

查看:122
本文介绍了扩展/缩小转换的实际应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么你会使用扩大或缩小转换?我已经阅读了很多关于这些但没有人给我一个实际的例子。谢谢!

Can someone please explain why you would ever use widening or narrowing conversion? I've read a lot about these but no one ever gives me a practical example. Thanks!

推荐答案

(Java)扩展和缩小转换与相关类型之间的转换有关。举例来说,抽象(超级)类与其(子)子类之间的关系;让我们使用java.lang.Number类(抽象)和直接子类Integer。我们在这里:

(Java) Widening and Narrowing Conversions have to do with converting between related Types. Take, for example, the relationship between an abstract (super) class and its (child) subclass; let's use the java.lang.Number Class (abstract) and a direct subclass Integer. Here we have:

(superclass)                               Number
                                   __________/\__________
                                  /       |      |       \
(concrete subclasses)          Integer   Long    Float   Double

拓宽转换:如果我们采取特定类型(子类)并尝试将其分配给不太具体的类型(超类)。

Widening Conversion: occurs if we take a specific type (subclass) and attempt to assign it to a less specific type (superclass).

Integer i = new Integer(8);
Number n = i;   // this is widening conversion; no need to cast

缩小转换:当我们采用不太具体的类型(超类)并尝试将其分配给更具体的类型(子类),这需要显式转换。

Narrowing Conversion: occurs when we take a less specific type (superclass) and attempt to assign it to a more specific type (subclass), which requires explicit casting.

Number n = new Integer(5); // again, widening conversion
Integer i = (Integer) n;   // narrowing; here we explicitly cast down to the type we want - in this case an Integer

存在某些问题你需要知道如ClassCastExceptions:

There are certain issues that you need to be aware of such as ClassCastExceptions:

Integer i = new Integer(5);
Double d = new Double(13.3);
Number n;

 n = i; // widening conversion - OK
 n = d; // also widening conversion - OK

 i = (Integer) d;  // cannot cast from Double to Integer - ERROR

 // remember, current n = d (a Double type value)
 i = (Integer) n;  // narrowing conversion; appears OK, but will throw ClassCastException at runtime - ERROR

处理此问题的一种方法是使用如果语句包含 instanceof 关键字:

One way to handle this is to use an if statement with the instanceof keyword:

 if( n instanceof Integer) {
      i = (Integer) n;
 }

为什么要使用它?假设您正在为一些程序制作人员层次结构,并且您有一个名为Person的通用超类,它将名字和姓氏作为参数,子类Student,Teacher,Secretary等。这里您最初可以创建一个通用人员,并将其(通过继承)分配给学生,该学生将在其构造函数中设置studenID的附加变量字段。您可以使用一个方法,将更通用(更宽)的类型作为参数,并处理该类型的所有子类:

Why would you want to use this? Let's say you are making a hierarchy of personnel for some program and you have a generic superclass called "Person" which takes a first and last name as parameters, and subclasses "Student", "Teacher", "Secretary", etc.. Here you can initially create a generic person, and assign it (through inheritance) to, say, a Student which would have an additional variable field for studenID set in it's constructor. You can use a single method that takes the more generic (wider) type as a parameter and handle all subclasses of that type as well:

 public static void main(String[] args) {
     Person p = new Student("John", "Smith", 12345);
     printInfo(p);
 }

 // this method takes the wider Person type as a parameter, though we can send it a narrower type such as Student if we want
 public static void printInfo(Person p) {
     System.out.println("First: " + p.getFirstName());
     System.out.println("Last: " + p.getLastName());
     if (p instanceof Student) {
         System.out.println( (Student)p).getStudentID() );  // we cast p to Student with Narrow Conversion which allows us to call the getStudentID() method; only after ensuring the p is an instance of Student
     }
}

I的实例之后意识到这可能不是处理事情的理想方式,但为了演示,我认为它有助于展示一些可能性。

I realize this may not be the ideal way to handle things, but for the sake of demonstration I thought it served to show some of the possibilities.

这篇关于扩展/缩小转换的实际应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆