java 获取可能的对象实例/类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1159476/
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
get possible instance/type of object
提问by machine
I have the problem that a given object could be one of several different objects (like files in a directory, that could be sound, image, text, ...) Is there the possibility to find out the type of the object beside from the 'instanceof' operator? Couse that would result in an endless if - then row ...
我有一个问题,给定的对象可能是几个不同的对象之一(例如目录中的文件,可能是声音、图像、文本……) 'instanceof' 运算符?Cuse 会导致无休止的 if - then row ...
回答by Joachim Sauer
foo.getClass()gets the java.lang.Classobject describing the class of the object.
foo.getClass()获取java.lang.Class描述对象类的对象。

