在 Java 中使用任何类型的变量并检查 this 的类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16461705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:01:19  来源:igfitidea点击:

Use variable to any type and check type of this in Java?

javaandroidvariablesobjectvar

提问by ephramd

I have serious problems when implementing a java library for use in Android. I need global variables, I've solved this by applying Singleton.

在实现用于 Android 的 Java 库时,我遇到了严重的问题。我需要全局变量,我通过应用单例解决了这个问题。

But now I need to use variables without specifying the type. As a solution I found using Object o.

但现在我需要使用变量而不指定类型。作为我使用 Object o 找到的解决方案。

Object o, how I check the type of o?

对象 o,我如何检查 o 的类型?

o.isArray () // Ok is type Array

o.isArray () // Ok is type Array

But to know if it is int, double, ...?

但是要知道它是int,double,...?

Another solution to using Object or variable of any type?

使用任何类型的对象或变量的另一种解决方案?

For example:

例如:

public String arrayToString (Object o) {
    if (o.getClass (). isArray ()) {
        Arrays.toString return ((Object []) o);
        Else {}
        o.toString return ();
    }
}

Object [] a = (Object []) LIB.getConf ("test");
a_edit [0] = "new value";
a_edit [1] = 2013;

x.arrayToString ("test") / / return test
x.arrayToString (1989) / / return 1989
x.arrayToString (a) / / return [new value, 2013]

thanks you,

感谢您,

回答by Konstantin Yovkov

Use the instanceofoperator.

使用instanceof运算符。

For example:

例如:

if (o instanceof Integer) {
   //do something
} else if (o instanceof String) {
   //do something else
} else if (o instanceof Object[]) {
   //or do some other thing
} else if (o instanceof SomeCustomObject) {
   //....
}

回答by Malf

The Java language provides an operator "instanceof" to check the runtime type of an object. You could check if an object is of any type, simple doing the following:

Java 语言提供了一个运算符“instanceof”来检查对象的运行时类型。您可以检查对象是否属于任何类型,只需执行以下操作:

    if (o instanceof String) {
      // The Object is an instance of a String
    } 
    else if (o instanceof Double) {
      // The Object is an instance of a Double
    } 
    // And so on..

Another idea is to use the getClass, it works in a similar manner:

另一个想法是使用 getClass,它以类似的方式工作:

  if (o1.getClass().equals(o2.getClass())) {  
    // The objects have the same class type
  } 

回答by Stephen C

There is no type in Java that is a supertype of both reference types and primitive types. But there is an alternative. Each primitive type has a corresponding immutable wrapper type; e.g.

Java 中没有任何类型既是引用类型又是原始类型的超类型。但还有一个选择。每个原始类型都有一个对应的不可变包装器类型;例如

  • boolean -> Boolean
  • int -> Integer
  • char -> Character
  • 布尔值 -> 布尔值
  • int -> 整数
  • 字符 -> 字符

So you can wrap an intas an Integerand then assign the resulting value to a variable of type Object. (And in fact, modern versions of Java provide syntactic sugar that does the "boxing" and "unboxing" automatically; see http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

因此,您可以将 an 包装int为 an Integer,然后将结果值分配给类型为 的变量Object。(事实上​​,现代版本的 Java 提供了自动执行“装箱”和“拆箱”的语法糖;参见http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Once you've assigned a value to a variable of type Objectyou can find out what its real type is using the instanceofoperator ... or by using getClass().getName()to find out the type's name.

一旦你给一个类型的变量赋值了,Object你就可以使用instanceof运算符getClass().getName()来找出它的真实类型……或者通过使用来找出类型的名称。

回答by xtraclass

IN ADDITION JUST HAVE A LOOK AT

另外看看

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#isPrimitive()

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#isPrimitive()

"There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double."

“有九个预定义的 Class 对象来表示八种基本类型和 void。它们是由 Java 虚拟机创建的,并且与它们所表示的基本类型具有相同的名称,即 boolean、byte、char、short、int、long 、浮动和加倍。”