JAVA中动态加载类-异常

发布于 2024-09-28 20:36:32 字数 1351 浏览 2 评论 0原文

伙计们, 我在 Java 动态类加载方面遇到了一个略有不同的问题。我必须将一个对象(假设是 A 类的对象 A1)传递给 B 类的另一个对象 B1 的构造函数,以便 A1 的详细信息存储在 B1 中。 B1 不知道它正在接收什么样的对象。所以它只知道 A1 是一个对象。

现在我在类 B 中有函数,它接受对象 C1 并检查它是否等于对象 A1。所以基本上我必须检查

  1. 对象 C1 是否也是 A 类类型。(实际上我还没有超过这一点。)
  2. 如果对象内的值相同,

我尝试了一个解决方案:

当将对象 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

  1. If object C1 is also of the type class A. ( Actually I am not past this point. )
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぶ宁プ宁ぶ 2024-10-05 20:36:32

instanceof 运算符仅适用于静态加载的类。对于动态加载的类,请使用 Class 类上的 isInstance 方法。

if (class11.isInstance(C1)) {
    // do something
}

instanceof operator works only with staticly loaded classes. For dynamically loaded classes, use the isInstance method on the Class class.

if (class11.isInstance(C1)) {
    // do something
}
过度放纵 2024-10-05 20:36:32

使用 Class.isInstance() 代替 instanceof 运算符:

if (c.isInstance(C1)) {
    [...]
}

注意:

  • instanceof 运算符只能用于类型(在编译时已知)。
  • isInstance() 方法在 java.lang.Class 对象上调用。

Use Class.isInstance() instead of the instanceof operator:

if (c.isInstance(C1)) {
    [...]
}

Note:

  • The instanceof operator can only be used on types (which are known at compile time).
  • The isInstance() method is called on java.lang.Class objects.
猥︴琐丶欲为 2024-10-05 20:36:32

instanceof 构造需要类型,而不是对类对象的引用:

if ( C1 instanceof MyClass ) {
  // ...
}

您可以使用:

if (c.isnstance(C1)) {
  // ...
}

instanceof cunstruction requires type, not reference to class object:

if ( C1 instanceof MyClass ) {
  // ...
}

You may use:

if (c.isnstance(C1)) {
  // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文