JAVA中动态加载类-异常
伙计们, 我在 Java 动态类加载方面遇到了一个略有不同的问题。我必须将一个对象(假设是 A 类的对象 A1)传递给 B 类的另一个对象 B1 的构造函数,以便 A1 的详细信息存储在 B1 中。 B1 不知道它正在接收什么样的对象。所以它只知道 A1 是一个对象。
现在我在类 B 中有函数,它接受对象 C1 并检查它是否等于对象 A1。所以基本上我必须检查
- 对象 C1 是否也是 A 类类型。(实际上我还没有超过这一点。)
- 如果对象内的值相同,
我尝试了一个解决方案:
当将对象 A1 存储在 B1 内时,我还存储类“A”的名称,因为对象 B1 只获取对象 A1 而不是类名 A。
public static void testing(Object C1, String s) //testing is the function of B called by B1
{
try{
Class c = Class.forName(s);
if( C1 instanceof c) // This is throwing error stating c cannot be resolved to a type
{
//further checking
}
}catch (Exception e){ System.out.println(e);}
}
我一直在尝试不同的代码来获取类类型以进行 instanceof () 比较,但没有成功。有什么建议吗?
我什至尝试过这个但同样的错误
ClassLoader classLoader = B.class.getClassLoader();
try{
Class class11 = classLoader.loadClass(s);
if ( C1 instanceof class11 )
{
}
}catch (Exception e){ System.out.println(e);}
任何有关如何继续的指示都会非常有帮助!
解决方案是使用: isInstance 而不是 instanceof
此问题还附带其他问题。让我用下面的评论来更新它。现在,我必须比较 C1 和 A1 中的一个值(例如 AGE),该值位于 B1 内。现在我该怎么做?如果我尝试这些函数或值,我会给出编译器错误,因为编译器仍然不知道对象 C1 和 A1 的类类型
而且我可以首先保存 X1 而不是 A1。现在我可能必须检查不同的属性检查,例如地址。这极大地改变了问题的维度。
Guys,
I have got a slightly different problem with Java Dynamic class loading. I have to pass an object( lets say Object A1 of class A) to the constructor of a different object B1 of class B, So that the A1's details are stored inside B1. B1 does not know what kind of object it is receiving. So all it knows is that A1 is an object.
Now I have functions in class B, which accept object C1 and check if it is equal to Object A1. So basically I have to check
- If object C1 is also of the type class A. ( Actually I am not past this point. )
- If the values inside the objects are the same
I tried a solution :
When storing the Object A1 inside B1 , I also store the name of the class "A" since object B1 only gets the object A1 but not the class name A.
public static void testing(Object C1, String s) //testing is the function of B called by B1
{
try{
Class c = Class.forName(s);
if( C1 instanceof c) // This is throwing error stating c cannot be resolved to a type
{
//further checking
}
}catch (Exception e){ System.out.println(e);}
}
I have been trying different code to get the class type to make a instanceof () comparision but I am not successful . Any suggestion ?
I even tried this but same error
ClassLoader classLoader = B.class.getClassLoader();
try{
Class class11 = classLoader.loadClass(s);
if ( C1 instanceof class11 )
{
}
}catch (Exception e){ System.out.println(e);}
Any pointer on how to proceed would be very helpful !
Solution is to use : isInstance instead of instanceof
There is also other issues attached with this problem . Let me update it with my below comment. I, now, have to compare a value , say AGE, in both C1 and A1, which inside B1. Now How do I do that ? If I try the functions or values , I will give me compiler error because the compiler is still oblivious of the class type of the object C1 and A1
Also I could have saved X1 instead of A1 in the first place. Now I might have to check for a different property check like ADDRESS. This changes the dimension of the problem drastically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
instanceof
运算符仅适用于静态加载的类。对于动态加载的类,请使用Class
类上的isInstance
方法。instanceof
operator works only with staticly loaded classes. For dynamically loaded classes, use theisInstance
method on theClass
class.使用
Class.isInstance()
代替instanceof
运算符:注意:
instanceof
运算符只能用于类型(在编译时已知)。isInstance()
方法在java.lang.Class
对象上调用。Use
Class.isInstance()
instead of theinstanceof
operator:Note:
instanceof
operator can only be used on types (which are known at compile time).isInstance()
method is called onjava.lang.Class
objects.instanceof 构造需要类型,而不是对类对象的引用:
您可以使用:
instanceof cunstruction requires type, not reference to class object:
You may use: