Java检查对象是否为类型的实例

时间:2020-01-14 18:38:02

标签: java

美好的一天Devs。我正在尝试将传入的对象与期望的类型进行比较。我的语法感觉不错,我相信解决方案就在眼前。我正在做的事情合乎逻辑吗?引发的错误是“ pExpectedType无法解析为类型”

     /**
     * Tests whether the specified object is an instance of the expected type and throws an exception if the expected type is not in the inheritance hierarchy of the object.
     * @param pValue    The object the test expects to be of the specified type.
     * @param pExpectedType The expected type of value.
     * @param pMessage  The message to include in the exception when value is not an instance of expectedType. The message is shown in test results.
     * @param pParameters   An array of parameters to use when formatting message.
     * @throws AssertFailedException    Thrown if value is null or expectedType is not in the inheritance hierarchy of value.
     */
    public static void IsInstanceOfType (Object pValue, Class<?> pExpectedType, String pMessage, Object[] pParameters) throws AssertFailedException
    {
        if(pValue == null || pValue instanceof pExpectedType == false)
            throw new AssertFailedException(String.format(pMessage, pParameters));
    }

3 个答案:

答案 0 :(得分:2)

使用

if (!pExpectedType.isInstance(pValue))

答案 1 :(得分:0)

该方法很好,应该通过检查pExpectedType是否为pValue的实例来工作。

if(!(pValue instanceof pExpectedType))

同样,另一种方法可能是使用反射。例如:

public static void isInstanceOfTypeWithReflection(Object pValue, Class<?> pExpectedType, String pMessage, Object[] pParameters) throws AssertFailedException {
    Class cls = pValue.getClass();

    if(!pExpectedType.isAssignableFrom(cls)){
        throw new AssertFailedException(String.format(pMessage, pParameters));
    }
}

答案 2 :(得分:0)

有以下三种方法来测试对象是否与期望的类匹配:

  1. 使用isInstance()是“ Java语言instanceof运算符的动态等效项”。
  2. 比较getClass()的输出将仅匹配确切的类。
  3. 使用isAssignableFrom()将检查该类是否“与指定的Class参数所表示的类或接口相同,或者是该类或接口的超类或超接口”。
<table>
  <tbody>
    <tr>
      <th>Table Header</th>
    </tr>

    <tr>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

    </tr>

    <tr>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

      <td valign="bottom">
        <span class="item">
          <img src="https://picsum.photos/100" class="thumb"><br>
          Description</span><br>
      </td>

    </tr>

  </tbody>
</table>