【问题标题】:How to store a class name in any variable and use it with instanceof operator如何将类名存储在任何变量中并将其与 instanceof 运算符一起使用
【发布时间】:2020-05-08 05:44:01
【问题描述】:
  // store the class name in varibale className
    DataType className = ValueOfclassName;
    if(a instanceof className)
            {
                System.out.println("This is instance");
            }

【问题讨论】:

标签: java instanceof classname


【解决方案1】:

嗯,instanceof 大约是 Class,但类名是 String。你可以了解更多here

无论如何,这可以回答你的问题吗?

A i = new A();
if (i instanceof A) {

}

如果你想要一个类的字符串表示,你可以使用:

A.class.getName()

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 2022-11-17
    • 2013-08-13
    • 2012-10-15
    • 2020-07-23
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多