是否有可能在Java中获得未实例化变量的类型? [英] Is it possible to get the type of an uninstantiated variable in Java?

查看:121
本文介绍了是否有可能在Java中获得未实例化变量的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取在Java中声明(但未实例化)的变量的类型? 例如:

Is it possible to get the type of a variable that is declared (but not instantiated) in Java? for example:

public class Foo {

     FooTwo foo2;
     FooTwo foo3;
     FooThree foo4;

     public static void main(String[] args) {
         if (foo2.getClass() == foo3.getClass()) {
             System.out.println("foo2 is the same type as foo3");
         }
         if (foo3.getClass() == foo4.getClass()) {
             System.out.println("foo3 is the same class as foo4");
         }
     }
}

有输出:

foo2 is the same type as foo3

显然,方法getClass()不适用于未实例化的变量,因此此代码不起作用.我假设我要查找的信息存储在变量(指针?)中的某个位置,以确保类型安全,并且可以访问该信息. 是否可以实现此比较?

obviously the method getClass() does not work on an uninstantiated variable, so this code does not function. I am assuming that the information I am looking for is stored somewhere in the variable (pointer?) for type safety, and that it may be accessible. Is it possible to Achieve this comparison?

原因: 我有一个带有几个声明的变量的类.这些变量应该指向存储在另一个类的ArrayList中的对象.我正在尝试创建一个方法,该方法将一个已初始化(但未实例化)的变量作为参数,在arraylist中扫描与该已初始化变量的类型匹配的对象,然后将该变量设置为该对象(将变量指向该对象).

the reason: I have a class with several declared variables. These variables are supposed to point to objects stored in an ArrayList in another class. I am trying to create a method that will take an initialized (but uninstantiated) variable as a parameter, scan the arraylist for an object matching the type of the initialized variable, and set the variable to the object (Make the variable point to the object).

ALSO:系统的重点是消除类的构造函数中的耦合.这些对象不能立即实例化,也不能在构造函数中实例化.

ALSO: The point of the system is to remove coupling in the constructor of the class. The objects cannot be instantiated immediately or in the constructor.

推荐答案

首先,您需要了解表达式的 type runtime-type之间的区别的值.例如,考虑以下代码:

To start with, you need to be aware of the difference between the type of an expression and the runtime-type of a value. Consider, for example, the following code:

List<String> list = new ArrayList<String>();
System.out.println(list.getClass());

上面的代码显示的是class java.util.ArrayList,而不是java.util.List<java.lang.String>,因为getClass()返回的是list所引用对象的运行时类型,而不是list本身的类型.

The above code prints class java.util.ArrayList, not java.util.List<java.lang.String>, because getClass() returns the runtime-type of the object that list refers to, not the type of list itself.

因此,对未初始化的变量调用getClass()没有任何意义,因为没有任何值可以返回其运行时类型.

So it doesn't make sense to call getClass() on an uninitialized variable, because there's no value for it to return the runtime-type of.

我正在尝试创建一种方法,该方法将使用已初始化(但未实例化)的变量作为参数[…]并将变量设置为对象(将变量指向该对象).

I am trying to create a method that will that will take an initialized (but uninstantiated) variable as a parameter, […] and set the variable to the object (Make the variable point to the object).

由于Java是一种按值传递的语言,因此它没有公开一种方法来修改作为参数传递的变量.例如,考虑以下方法:

Since Java is a pass-by-value language, it doesn't expose a way to modify a variable that is passed in as a parameter. For example, consider this method:

public void doNothing(Object obj) {
    obj = "obj";
}

以上方法绝对不执行任何操作.它有一个局部变量obj,该变量最初包含传入的任何引用,然后更改为引用字符串"obj".但是此更改没有效果,因为在此之后,实际上没有任何东西使用局部变量obj.特别是,此方法对传入的任何引用都没有任何影响.

The above method does absolutely nothing. It has a local variable obj, which originally contains whatever reference is passed in, and then is changed to refer to the string "obj" instead; but this change has no effect, because nothing actually uses the local variable obj after that point. In particular, this method does not have any effect on whatever reference was passed in.

您可以获得的最接近的结果是使用反射.如果您的方法采用类型Field的实例,它既可以检查字段的声明类型,又可以将字段设置为其选择的值.

The closest you can get is to use reflection; if your method takes an instance of type Field, it can both examine the declared type of the field, and set the field to a value of its choosing.

这篇关于是否有可能在Java中获得未实例化变量的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆