在Java中转换变量 [英] Casting variables in Java

查看:116
本文介绍了在Java中转换变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有人可以告诉我如何铸造工程?我明白何时我应该做,但不是真的如何工作。对于原始数据类型,我部分理解,但是当涉及到投射对象时,我不明白它是如何工作的。

I wonder if anyone could tell me how casting works? I understand when I should do it, but not really how it works. On primitive data types I understand partially but when it comes to casting objects I don't understand how it works.

如果一个类型Object的对象突然被抛出 $ (只是一个例子),然后得到所有的方法?

How can a object with the type Object just suddenly be cast to, let's say, MyType (just an example) and then get all the methods?

推荐答案

在Java中进行转换并不奇怪,它告诉编译器A类型的对象实际上是更具体的类型B,因此可以访问B上的所有方法,除此以外。在执行转换时,你不会执行任何类型的魔法或转换,你基本上告诉编译器相信我,我知道我在做什么,我可以保证,这一对象在这一行实际上是一个<插入在这里铸造类型> ;.例如:

Casting in Java isn't magic, it's you telling the compiler that an Object of type A is actually of more specific type B, and thus gaining access to all the methods on B that you wouldn't have had otherwise. You're not performing any kind of magic or conversion when performing casting, you're essentially telling the compiler "trust me, I know what I'm doing and I can guarantee you that this Object at this line is actually an <Insert cast type here>." For example:

Object o = "str";
String str = (String)o;

上面是很好,不是魔术,存储在o中的对象实际上是一个字符串,因此我们可以转换为一个字符串没有任何问题。

The above is fine, not magic and all well. The object being stored in o is actually a string, and therefore we can cast to a string without any problems.

有两种方法可能会出错。首先,如果你在完全不同的继承层次结构中在两种类型之间进行转换,那么编译器会知道你是愚蠢的,并阻止你:

There's two ways this could go wrong. Firstly, if you're casting between two types in completely different inheritance hierarchies then the compiler will know you're being silly and stop you:

String o = "str";
Integer str = (Integer)o; //Compilation fails here

其次,如果他们在同一个层次结构,将在运行时抛出 ClassCastException

Secondly, if they're in the same hierarchy but still an invalid cast then a ClassCastException will be thrown at runtime:

Number o = new Integer(5);
Double n = (Double)o; //ClassCastException thrown here

这本质上意味着你违反了编译器的信任。你告诉它,你可以保证对象是一个特定的类型,而不是。

This essentially means that you're violated the compiler's trust. You've told it you can guarantee the object is of a particular type, and it's not.

为什么你需要铸造?好吧,从你开始只需要它从一个更一般的类型到一个更具体的类型。例如, Integer 继承自 Number ,因此如果要存储 Integer 作为一个数字然后那就OK了(因为所有的整数都是数字。)然而,如果你想反过来,你需要一个cast - 不是所有数字是整数(以及整数,我们有 Double Float Byte Long 等等)。即使你的项目或JDK中只有一个子类,有人可以很容易地创建另一个子类,即使你认为这是一个单一的,明显的选择也不能保证!

Why do you need casting? Well, to start with you only need it when going from a more general type to a more specific type. For instance, Integer inherits from Number, so if you want to store an Integer as a Number then that's ok (since all Integers are Numbers.) However, if you want to go the other way round you need a cast - not all Numbers are Integers (as well as Integer we have Double, Float, Byte, Long, etc.) And even if there's just one subclass in your project or the JDK, someone could easily create another and distribute that, so you've no guarantee even if you think it's a single, obvious choice!

关于使用铸造,你仍然看到在一些库中需要它。 Pre Java-5它在集合和各种其他类中被大量使用,因为所有集合都在添加对象,然后投射你收回的结果。然而,随着泛型的出现,许多铸造的用途已经消失了 - 它已经被提供了一个更安全的替代的泛型替代,没有潜在的ClassCastExceptions(事实上,如果你使用泛型干净,它编译没有警告,你有一个保证,你永远不会得到ClassCastException。)

Regarding use for casting, you still see the need for it in some libraries. Pre Java-5 it was used heavily in collections and various other classes, since all collections worked on adding objects and then casting the result that you got back out the collection. However, with the advent of generics much of the use for casting has gone away - it has been replaced by generics which provide a much safer alternative, without the potential for ClassCastExceptions (in fact if you use generics cleanly and it compiles with no warnings, you have a guarantee that you'll never get a ClassCastException.)

这篇关于在Java中转换变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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