SCJP:不能加宽然后盒子,但你可以盒子然后加宽 [英] SCJP: can't widen and then box, but you can box and then widen

查看:143
本文介绍了SCJP:不能加宽然后盒子,但你可以盒子然后加宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在攻读SCJP考试,我遇到了一个我无法真正解决的问题。

I'm studying for the SCJP exam and I ran into an issue I can't really wrap my head around.

这本书说你无法拓宽然后框,但你可以盒子然后加宽。
不能框的示例是期望Long的方法,并且使用字节调用该方法。

The book says you can't widen and then box, but you can box and then widen. The example for not being able to box is a method expecting a Long and the method being invoked with a byte.

他们的解释是:


考虑一下......如果它先尝试打包
,则该字节将被转换为字节
。现在我们回到
试图将一个Byte扩大到一个Long,然后
当然,IS-A测试失败。

Think about it…if it tried to box first, the byte would have been converted to a Byte. Now we're back to trying to widen a Byte to a Long, and of course, the IS-A test fails.

但这听起来像是盒子然后加宽而不是加宽然后装箱给我。

But that sounds like box and then widen and not widen and then box to me.

任何人都可以澄清整个盒子并加宽和加宽对我而言,因为现在这本书在这个问题上并不十分清楚。

Could anyone clarify the whole box and widen vs widen and box for me because as it stands the book isn't exactly clear on the issue.

编辑:
澄清:我在谈论第252页和259的SCJP sun认证程序员为java 6书。
http:// books .google.be / books?id = Eh5NcvegzMkC& pg = PA252#v = onepage& q& f = false

推荐答案

不允许扩大拳击的原因可能是由于以下原因(SCJP书的第249页):

The reason why "widening then boxing" is not allowed may be because of the following reason (page 249 of the SCJP book):


< Java 5的设计者认为最重要的规则应该是
预先存在的代码应该像以前那样运行,所以既然
扩展能力已经存在,那么通过
扩展调用的方法不应该输给依赖于
拳击的新创建的方法

Java 5's designers decided that the most important rule should be that preexisting code should function the way it used to, so since widening capability already existed,a method that is invoked via widening shouldn't lose out to a newly created method that relies on boxing

这篇关于SCJP:不能加宽然后盒子,但你可以盒子然后加宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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