简体   繁体   English

Class.forName强制转换

[英]Class.forName casts

In Java, will using Class.forName in a cast result in the object being cast to a class or being cast to the cast found. 在Java中,将在投射结果中使用Class.forName将对象强制转换为类或者将其强制转换为所发现的强制转换。 As an example, if you did this 例如,如果你这样做了

Object a;
String b = "testing";
a = (Class.forName("java.lang.Object")) b;

Would a be an instance of Class or an instance of Object? 会成为Class的实例还是Object的实例?

Class.forName returns a Class instance. Class.forName返回一个Class实例。 I'm fairly certain your code as quoted doesn't compile, you're trying to use a function call as a cast. 我相当肯定你引用的代码不能编译,你试图使用函数调用作为强制转换。


Update : Just had a thought: If you're asking how to use a dynamically-loaded class in a cast, you basically can't. 更新 :刚才有一个想法:如果你问如何在演员表中使用动态加载的类,你基本上不能。 Casting is (mostly) a compile-time thing. 转换(大多数情况下)是编译时的事情。 Have the dynamically-loaded class implement an interface you can compile into your code and then cast to that, or if you can't do that, use reflection to access the dynamically-loaded class' members. 让动态加载的类实现一个可以编译到代码中然后转换为代码的接口,或者如果你不能这样做,使用反射来访问动态加载的类的成员。

You can cast with the class object's .cast method: 您可以使用类对象的.cast方法进行.cast

Object a;
String b = "testing";
a = Class.forName("java.lang.Object").cast(b);

But you seem to have wrong ideas about casting - in Java, casting does not change any object , it just tells the compiler that the object is of some type, and on runtime, the VM will test if this is really the case (if the compiler can't already prove it). 但是你似乎对于转换有错误的想法 - 在Java中,转换不会改变任何对象 ,它只是告诉编译器该对象是某种类型的,并且在运行时,VM将测试是否真的如此(如果编译器不能证明它)。 If you "cheated", the VM will throw an ClassCastException here (it will not convert your object). 如果您“作弊”,VM将在此处抛出ClassCastException (它不会转换您的对象)。

(It works a bit different if primitive types are involved.) (如果涉及原始类型,它会有所不同。)

The Class object's cast method is a generic variant of this same mechanism. Class对象的cast方法是同一机制的通用变体。 Since it has the return type T (from Class<T> ), this allows generic code to cast an unknown object to some class type, where this class object corresponds to a type variable. 由于它具有返回类型T (来自Class<T> ),因此这允许通用代码将未知对象强制转换为某种类类型,其中此类对象对应于类型变量。 This will not help you here - the return type of Class.forName is Class<?> , which means that its cast method can only return Object . 这对你没有帮助 - Class.forName的返回类型是Class<?> ,这意味着它的强制转换方法只能返回Object

And anyway, casting to java.lang.Object has no effect (it will always succeed), other than hiding the to the compiler that the value has some specialized type. 无论如何,转换为java.lang.Object没有任何效果(它将始终成功),而不是将值隐藏到编译器中具有某种特殊类型。 (This might matter if you have overloaded methods.) (如果您有重载方法,这可能很重要。)

Simply writing 简单写作

Object a;
String b = "testing";
a = b;

has the same effect here. 在这里有同样的效果。

If it is required to cast an instance this is a way to do it 如果需要转换实例,这是一种方法

String className         = "YourClass";
Class someClass          = Class.forName( className );
Constructor constructor  = (Constructor) someClass.getConstructor();
Object someInstance      = constructor.newInstance();

// now you can cast as allways.
(YourClass) (someInstance)

Class.forName始终返回Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM