13

JAVAでメソッドの戻り値の型を見つけるのを手伝ってくれる人はいますか? これを試しました。しかし、残念ながらうまくいきません。私を案内してください。

 Method testMethod = master.getClass().getMethod("getCnt");

  if(!"int".equals(testMethod.getReturnType()))
   {
      System.out.println("not int ::" + testMethod.getReturnType());
   }

出力:

int ではない ::int

4

7 に答える 7

15

メソッドgetReturnType()は戻りますClass

あなたが試すことができます:

if (testMethod.getReturnType().equals(Integer.TYPE)){ 
      .....;  
}
于 2013-02-06T13:32:08.013 に答える
5
if(!int.class == testMethod.getReturnType())
{
  System.out.println("not int ::"+testMethod.getReturnType());
}
于 2013-02-06T13:34:27.877 に答える
2

戻り値のタイプはClass<?>...文字列を取得するには次のことを試してください。

  if(!"int".equals(testMethod.getReturnType().getName()))
   {
      System.out.println("not int ::"+testMethod.getReturnType());
   }
于 2013-02-06T13:29:41.400 に答える
2

getReturnType()Class<?>ではなくを返すStringため、比較は正しくありません。

また

Integer.TYPE.equals(testMethod.getReturnType())

または

int.class.equals(testMethod.getReturnType())

于 2013-02-06T13:37:39.930 に答える
1

getReturnType()Classオブジェクトを返し、文字列と比較しています。あなたが試すことができます

if(!"int".equals(testMethod.getReturnType().getName() ))
于 2013-02-06T13:30:17.527 に答える
1

このgetReturnTypeメソッドは、比較対象のClass<?>オブジェクトではないオブジェクトを返します。StringオブジェクトがClass<?>オブジェクトと等しくなることはありませんString

それらを比較するには、使用する必要があります

!"int".equals(testMethod.getReturnType().toString())

于 2013-02-06T13:30:54.563 に答える
1

getretunType() は を返しますClass<T>。整数型と等しいかどうかをテストできます

if (testMethod.getReturnType().equals(Integer.TYPE)) {
    out.println("got int");
}
于 2013-02-06T13:31:38.747 に答える