简体   繁体   English

Java:如何“实例化参数”

[英]Java: how to “instanceof argument”

I have tried: 我努力了:

package ro.ex;    

import java.io.IOException;
import java.lang.reflect.Type;

class Ex {
    public boolean isIns(Object o, Class t) {
        o instanceof t;
    }
    public static void main(String[] args) throws IOException {}
}

above code will raise unknown class "t" 上面的代码将引发未知类“ t”

My question is: How to pass above code. 我的问题是:如何传递以上代码。

update: 更新:

following code can't pass intellij idea syntax checker 以下代码无法通过intellij idea语法检查器

public boolean isIns(Object o, Class<?> t) {
    return o instanceof t;
}

so the right code in idea is: 所以正确的想法是:

public boolean isIns(Object o, Class<?> t) {
    return t.isAssignableFrom(o.getClass());
}

the more simple way is: 更简单的方法是:

package ro.ex;

import java.io.IOException;
import java.lang.reflect.Type;

class Ex {
    public boolean isIns(Object o, Class t) {
        return t.isInstance(o);
    }

    public static void main(String[] args) throws IOException {
        Object r = new Ex().isIns("", String.class);
        System.out.println(r + "\t\t" + new Exception().getStackTrace()[0].getFileName() + ":" + new Exception().getStackTrace()[0].getLineNumber());
    }
}

If you write x instanceof t , then t must be a class. 如果编写x instanceof t ,则t必须是一个类。 In your isIns method, t is not a class, it is a variable of type Class . isIns方法中, t不是类,它是Class类型的变量。

The class Class , however, does offer methods with which you can decide whether some other class is a subclass of it: Class.isAssignableFrom(Class) . 但是,类Class确实提供了一些方法,您可以通过这些方法确定某个其他类是否是它的子类: Class.isAssignableFrom(Class) So you can change you method to: 因此,您可以将方法更改为:

public boolean isIns(Object o, Class t)
{
    return t.isAssignableFrom(o.getClass());
}

(I also changed your code so that the result of is returned to the caller.) (我还更改了您的代码,以便将的结果返回给调用方。)

I have no idea what you are trying to do with your method, but the instanceof syntax is wrong. 我不知道您要使用该方法做什么,但是instanceof语法错误。

 public boolean isIns(Object o, Class t) {
       return  o instanceof t;
    }

instanceof keyword checks with a Valid class name not with variable name. instanceof关键字使用有效的类名而不是变量名进行检查。 That's the compiler error. 那就是编译器错误。

For ex : o instanceof String , if you write like below, it won't compile 例如: o instanceof String ,如果您像下面这样编写,它将无法编译

public boolean isIns(Object o, String str) {
        return o instanceof str;  //err, o instance of String is correct way to check.
    }

And I slightly changed your method signature to match the statement. 我对您的方法签名进行了些微更改,以匹配该语句。

In your code below, t should be the name of a Java class (eg String ). 在下面的代码中, t应该是Java类的名称(例如String )。 In your code, you've passed a variable name which is not appropriate: 在您的代码中,您传递了不合适的变量名:

public void isIns(Object o, Class t) {
    o instanceof t;
}

Since an instanceof check is a one-liner, I'm not sure why you are wrapping it in a method. 由于instanceof检查是单行的,因此我不确定为什么要将它包装在方法中。 But if you insist on doing so, perhaps this is what you want: 但是,如果您坚持要这样做,也许这就是您想要的:

public static boolean isIns(Object o, Class<?> t) {
  return c.isInstance(o);
}

But two notes: 但是有两个注意事项:

  1. Lots of instanceof checks generally indicate bad design. 许多instanceof检查通常表明设计不正确。

  2. A Java programmer will be much more comfortable seeing instanceof rather than isIns(...) . isIns(...)相比,Java程序员更愿意看到instanceof

You should use the dinamic equivalent of instanceof from Class class. 您应该使用Class类中instanceof的动态等效项。 http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInstance(java.lang.Object) http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInstance(java.lang.Object)

  public void isIns(Object o, Class t) {
        t.isInstance(o);
  }

Check for nulls if needed. 如果需要,请检查是否为空。

maybe you want something like this? 也许您想要这样的东西?

public class test2 {

    class c {
    }

    public test2() {
        c obj = new c();
        System.out.println(isIns(obj));
    }

    public boolean isIns(Object o) {
        return (o instanceof c);
    }

    public static void main(String argv[]) {
        new test2();
    }
}

EDIT: Removed senceless Class parameters. 编辑:删除了无意义的类参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM