[英]Java casting question
I don't understand why I need to cast member variables to the proper type when their types are already declared. 我不明白为什么已经声明了成员变量的类型时需要将成员变量转换为正确的类型。 For example:
例如:
public class SomeClass extends SomethingElse {
private Funky mFunkyVar;
// a whole bunch of other variables and methods
public void needToCast() {
mFunkyVar = (Funky) new FunkySubClass();
}
}
Am I making some newbie mistake or is this indeed idiomatic Java? 我是在犯一些新手错误,还是这确实是惯用的Java? In the above I'm assuming that FunkySubClass is indeed a subclass of Funky.
在上面,我假设FunkySubClass确实是Funky的子类。
Actually you don't have to cast it! 实际上,您不必强制转换!
Am I making some newbie mistake
我在犯一些新手错误吗
Yeap, but don't worry... after this you wont do it again ;) 是的,但是请放心...之后,您将不会再做;)
See: 看到:
$cat Funky.java
class Funky {
}
class FunkySubClass extends Funky {
}
class SomethingElse {
}
class SomeClass extends SomethingElse {
private Funky aFunkyVar;
//...
public void noNeedToCast() {
aFunkyVar = new FunkySubClass();
}
}
$javac Funky.java
$
No compilation errors. 没有编译错误。
EDIT 编辑
You can cast primitives and object references. 您可以强制转换基元和对象引用。
For primitives you "have" to cast, if you want to narrow the value. 对于基元 ,如果要缩小值,则必须“强制”转换。
For instance: 例如:
int i = 255;
byte b = ( byte ) i;
If you don't cast the compiler will warn you, "hey, you don't really want to do that" 如果您不进行强制转换,编译器会警告您,“嘿,您真的不想这样做”
...
byte b = i;
...
Funky.java:18: possible loss of precision
found : int
required: byte
byte d = i;
Casting is like telling the compiler: "hey don't worry, I know what I'm doing, ok?" 转换就像告诉编译器:“嘿,不用担心,我知道我在做什么,好吗?”
Of course, if you cast, and the value didn't fit, you'll get strange results: 当然,如果您强制转换并且该值不适合,您将得到奇怪的结果:
int i = 2550;
byte b = ( byte ) i;
System.out.println( b );
prints: 印刷品:
-10
You don't need to cast when the type is wider than the other type: 当一个类型比另一个类型宽时,您不需要强制转换 :
byte b = 255;
int i = b;// no cast needed
For references works in a similar fashion. 供参考以类似的方式工作。
You need to cast, when you want to go down into the class hierarchy ( narrow ) and you don't need to cast when you want to go upper in the hierarchy ( like in your sample ). 当您想进入类层次结构时,您需要进行强制转换(狭窄),而当您要在层次结构中进行上级操作时,则不需要进行转换(例如您的样本中)。
So if we have: 因此,如果我们有:
java.lang.Object
|
+ -- Funky
|
+-- FunkySubclass
You only have to cast, when you have a type upper in the hierarchy ( Object or Funky ) in this case. 在这种情况下,仅当在层次结构中具有上层类型(Object或Funky)时,才需要强制转换。 And you don't have to, if you have a type lower in the hierarchy:
如果层次结构中的类型较低,则不必这样做:
void other( Object o ) {
// Cast needed:
Funky f = ( Funky ) o;
FunkySubClass fsc = ( FunkySubClass ) o;
FunkySubClass fscII = ( FunkySubClass ) f;
// Cast not needed:
Object fobj = f;
Object fscobj = fsc;
Funky fparent = fsc;
}
With primitives the JVM know how to truncate the value, but with reference no. 使用基元,JVM知道如何截断该值,但是参考号为。 So, if the casted value is not of the target type, you'll get a
java.lang.ClassCastException
at runtime. 因此,如果强制转换的值不是目标类型,则在运行时将获得
java.lang.ClassCastException
。 That's the price you have to pay, when you tell the compiler "I know what I'm doing" and you don't. 当您告诉编译器“我知道我在做什么”而您却没有时,这就是您必须付出的代价。
I hope this is clear enough and don't confuse you. 我希望这足够清楚,不要让您感到困惑。
:) :)
If FunkySubClass is really a subclass of Funky you don't need the cast. 如果FunkySubClass确实是Funky的子类,则不需要强制转换。
If you show us the error you're getting we could help more. 如果您向我们显示错误,您将会得到我们的更多帮助。
无需“ (Funky)
”,作为FunkySubClass
是,一个 Funky
。
You don't need to explicitly cast to the base class. 您无需显式转换为基类。 For example, it is actually more idiomatic not to cast - a common case is:
例如,实际上不撒播是比较惯用的-常见的情况是:
List<String> somelist=new ArrayList<String>();
You would only need to cast if FunkySubClass was (improperly named and) in fact the parent class of Funky. 仅当FunkySubClass实际上是(不正确地命名为)Funky的父类时,才需要强制转换。 Assigning a child class to a parent object is fine.
将子类分配给父对象很好。
new FunkySubClass()
returns an object of type FunkySubClass, not Funky. new FunkySubClass()
返回类型为FunkySubClass的对象,而不是Funky。 Hence you should explicitly tell Java to treat it as a Funky object 因此,您应该明确告诉Java将其视为Funky对象
It is the right-hand side which is being converted 它是正在转换的右侧
You don't need to cast it. 您无需强制转换。 What makes you think you do?
是什么让您觉得自己呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.