Java 未定义对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19592722/
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-08-12 18:36:21  来源:igfitidea点击:

Java Undefined Object

javaobjectundefined

提问by Aaron Priesterroth

I got an arraylist and a method to add an object to the arraylist. Currently im using overloaded methods to differentiate between different kinds of object. Is there a way to use an undefined object as a parameter for the method and differentiate inside of the method what kind of an object it is?

我得到了一个数组列表和一个将对象添加到数组列表的方法。目前我使用重载方法来区分不同类型的对象。有没有办法使用未定义的对象作为方法的参数并在方法内部区分它是哪种对象?

采纳答案by Bernhard Barker

By "undefined object as a parameter", I assume you mean that you're looking to write a function that doesn't specify the type of the object in the function declaration, allowing you to only have one function.

通过“未定义的对象作为参数”,我假设您的意思是您希望编写一个不在函数声明中指定对象类型的函数,允许您只有一个函数。

This can be done with generics.

这可以通过泛型来完成。

Instead of:

代替:

static void func(String str)
{
  System.out.println("The string is: "+str);
}
static void func(Integer integer)
{
  System.out.println("The integer is: "+integer);
}

you can have:

你可以有:

static <T> void func(T value)
{
  if (value instanceof Integer)
    System.out.println("The integer is: "+value);
  else if (value instanceof String)
    System.out.println("The string is: "+value);
  else
     System.out.println("Type not supported!! - "+value.getClass());
}

Test:

测试:

func("abc"); // The string is: abc
func(3);     // The integer is: 3
func(3.0);   // Type not supported!! - class java.lang.Double

See Java genericsfor more information.

有关更多信息,请参阅Java 泛型

回答by Ted Hopp

By "undefined object" I assume you mean null. You can cast nullto a specific type of object and the compiler will know which overloaded method to bind to the call:

通过“未定义的对象”,我假设您的意思是null. 您可以null转换为特定类型的对象,编译器将知道要绑定到调用的重载方法:

public void method(String s) { . . . }
public void method(Integer s) { . . . }
public void caller() {
    method((String) null);
    method((Integer) null);
}

If you have an object of undefined type, you can use instanceofoperator to test what type it is, or getClass()to obtain the class object itself. If you have a nullvalue of unknown type, there's not much you can do other than redefining your method signature to accept an additional argument of type Class.

如果您有一个未定义类型的对象,您可以使用instanceof运算符来测试它是什么类型,或者getClass()获取类对象本身。如果您有一个null未知类型的值,除了重新定义您的方法签名以接受 type 的附加参数之外,您无能为力Class

However, if the comment by Dukeling is accurate and by "undefined object" you mean "an object of unknown type", you should look into using Java generics. Generics let you write a single method that works with a range of object types.

但是,如果 Dukeling 的评论是准确的,并且“未定义对象”的意思是“未知类型的对象”,那么您应该考虑使用 Java 泛型。泛型使您可以编写一个适用于一系列对象类型的方法。

public <T> void method(T arg) { . . . }
public void caller() {
    method("String"); // String arg
    method(0);        // Integer arg
}

Start with the Java generics tutorialfor more information.

Java 泛型教程开始了解更多信息。

回答by Govardhan

One way you can do it is pass two parameters. First parameter is the Object you need to pass and second parameter is the indicator of which type of Object you are passing.

一种方法是传递两个参数。第一个参数是您需要传递的对象,第二个参数是您传递的对象类型的指示符。

public void TestFunc(Object obj1, String type){}

Of course, there can be better ways than using a String, we can use Enums and some other mechanism. You can also use InstanceOf to differentiate if you don't want to pass additional parameter.

当然,还有比使用 String 更好的方法,我们可以使用 Enums 和其他一些机制。如果您不想传递额外的参数,您也可以使用 InstanceOf 来区分。