java - 如何检查参数类的类型?

标签 java android instanceof

我有以下方法:

public static String getServiceUri(Class<?> c) {

 // I'd like to check which type the parameter is...
 if(c.getClass().equals(MyClass.class)){
     do stuff 1
 } else {
     do stuff 2
 }
}

调用方法: getServiceUri(MyClass.class);

getServiceUri 上,我想根据 ServiceClass 的类型调用 WebService。

我知道 equals 会比较对象实例,但在这种情况下,我试图发现对象的类型。

有人知道我如何使用这种方法进行比较吗?

最佳答案

instanceof operator is the best choice..

你可以这样做

if(c instanceof MyClass){
 //do your stuff

}

关于java - 如何检查参数类的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10006166/

相关文章:

java - 更改数据后如何刷新GRAL图表?

android - layout_height ="0dp"是什么意思?

java - 简化多个旨在调用相同操作的 onclicklistener

java - 如何在java中验证这个时间/日期字符串?

java - Android:如何将 ArrayList 的每个值绑定(bind)到一个变量?

java - 如果值为 null,Mvel 表达式求值问题

android - Android studio 中的 git 与 TortoiseGit 不同

Java:实例泛型

java - 以通用方式使用 instanceof

php - 为包装类操作 PHP-instanceof-operator