【问题标题】:How to get the type of object reference of instance?如何获取实例的对象引用类型?
【发布时间】:2014-01-26 02:19:09
【问题描述】:

给定一个接口、一个抽象类和一个具体类

interface Interface {
}

abstract class AbstractClass {
}

class C extends AbstractClass implements Interface {
}

我像这样实例化我的具体类C 的两个实例

Interface a = new C();

AbstractClass b = new C();

System.out.println(getObjectReferenceName(a));// return some.package.Interface

System.out.println(getObjectReferenceName(b));// return some.package.AbstractClass

/* 它返回对象引用的类名 */

String getObjectReferenceName(Object o){
    // todo
    return "class name";
}

如何获取引用类型的类名?

那是——

a 的对象引用是 some.package.Interface

b 的对象引用是 some.package.AbstractClass

【问题讨论】:

  • ab字段,方法参数,局部变量,...?
  • a 和 b 是参考变量。您可以使用 instanceof 运算符来检查引用类型。
  • @SaurabhSharma OP 想要变量的声明类型,而不是运行时类型。
  • 请注意,当您真正想要的是“类型”时,您会一直提到“名称”。请将其称为“类型”,以免人们混淆!
  • 你的AbstractClass 没有实现你的Interface,这是故意的吗?

标签: java types classname object-reference


【解决方案1】:

想得到名字试试

System.out.println(a.getClass().getInterfaces()[0]);
System.out.println(b.getClass().getSuperclass());

输出:

interface testPackage.InterfaceClass
class testPackage.AbstractClass

如果你想检查一个对象是否是一个类的实例,试试instanceof

编辑:

如果你想获得声明变量的类型,你可以使用反射。如果这些是字段,换句话说,如果它们被声明为类字段,没有局部变量,则此方法有效。

System.out.println(Test.class.getDeclaredField("a").getType()); // Test is the name of the main' method class
System.out.println(Test.class.getDeclaredField("b").getType());

【讨论】:

    【解决方案2】:

    a.getClass().getInterfaces() 为您提供包含 Class 类对象的数组,这些对象代表对象 a 类实现的所有接口。

    即a.getClass().getInterfaces()[0].getName() 为您提供InterfaceClass

    b.getClass().getSuperclass().getName() 为您提供AbstractClass

    【讨论】:

      【解决方案3】:

      这似乎是一个愚蠢的答案,但是

      // right here
      //  |
      //  v
      
      Interface a = new C();
      

      您还添加了一个假设方法,该方法返回引用类型的名称,但

      static String getReferenceType(Object o) {
      
          // the reality is o is always Object
          return Object.class.getName();
      }
      

      实际上并不存在需要根据引用类型执行一些程序逻辑的情况,因为您总是知道引用的类型。它在你自己的声明中。需要调用方法或 instanceof 运算符的情况是当你有它时:当你不知道对象的 实际 类型时。

      【讨论】:

      • @SSaMKJ 你误会了。你知道a 是一个Interface 引用,因为你的声明说Interface a。您无需调用方法即可找出答案。它在声明中。
      • 哦...对不起。它返回 java.lang.Object
      • 我需要这个程序逻辑。因为,我正在制作一个带有反射构造函数的类。可能具有相同的参数长度,但具有实现和扩展的构造函数类型不同。无论如何,感谢您的考虑和回复。
      • 你不需要任何特别的东西来获得具有特定参数的构造函数。有一种方法:Class#getConstructor(Class... paramaterTypes).
      • 你好像不认为。我现在遇到了问题。用 parameterTypes 构造...所以我需要引用对象的具体类型。
      【解决方案4】:

      您可以通过 java.lang.Class 访问方法,然后调用它们来获取不同的信息。如果你想知道基于哪个类来实例化一个对象,你可以使用 referenceVariableName.getClass();要获得它的超类,referenceVariableName.getClass().getSuperclass() 来做到这一点;要知道接口,那么您可以使用referenceVariableName.getClass().getInterfaces()[0](可能是第一个接口,因为接口很多)。

      【讨论】:

        【解决方案5】:

        试试这个:

        public class ObjectReferenceName {
           Interface a = new C();
           AbstractClass b = new C();
           C c =new C();
           static String getObjectReferenceName(String fieldName) throws NoSuchFieldException{
                return ObjectReferenceName.class.getDeclaredField(fieldName).getType().getName();
           }
        
           public static void main(String[] args) throws NoSuchFieldException {
               System.out.println(ObjectReferenceName.getObjectReferenceName("a"));
               System.out.println(ObjectReferenceName.getObjectReferenceName("b"));
               System.out.println(ObjectReferenceName.getObjectReferenceName("c"));
           }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2010-11-27
          • 2013-11-10
          • 2011-10-26
          • 2020-06-28
          • 1970-01-01
          • 2011-05-25
          • 1970-01-01
          • 2023-03-23
          • 1970-01-01
          相关资源
          最近更新 更多