Java 我什么时候应该将类作为参数传递?

Java 我什么时候应该将类作为参数传递?,java,class,Java,Class,当我们应该将一个类作为参数而不是它的实例传递时,我对undrestand感到困惑。 例如: myMethod(classA.class); 您能举个例子说明我们应该在何时以及如何将类作为参数传递吗?一个经典的例子是通过反射创建类的实例: //avoiding exception handling, leave that up to you static <T> T make(Class<T> clazz) { return clazz.newInstance()

当我们应该将一个类作为参数而不是它的实例传递时,我对undrestand感到困惑。 例如:

myMethod(classA.class);

您能举个例子说明我们应该在何时以及如何将类作为参数传递吗?

一个经典的例子是通过反射创建类的实例:

//avoiding exception handling, leave that up to you
static <T> T make(Class<T> clazz) {
    return clazz.newInstance();
}
//...
A a = make(A.class);

另外,当您希望在编译时确保某些引用属于特定类时,如中所用。

一个经典示例是通过反射创建类的实例时:

//avoiding exception handling, leave that up to you
static <T> T make(Class<T> clazz) {
    return clazz.newInstance();
}
//...
A a = make(A.class);

此外,当您希望在编译时确保某些引用属于特定类时,如

让我们考虑我们有一些创建者

  abstract class Creator<T>
  {
    Creator(Class<T> c)
    {
        this.c = c;
    }

    T addMainElement(Object obj)
    {
        return c.cast(this);
    }

    private Class<T> c;
  }
若基类并没有关于子类的知识,那个么我们将无法做到这一点。 而且我们不会被从主造物主到主造物主的未经检查的施法打扰,如果我们这样施法,我们将不会受到警告

  return (T)this;

<> < /P> < P>让我们考虑我们有一些创建者

  abstract class Creator<T>
  {
    Creator(Class<T> c)
    {
        this.c = c;
    }

    T addMainElement(Object obj)
    {
        return c.cast(this);
    }

    private Class<T> c;
  }
若基类并没有关于子类的知识,那个么我们将无法做到这一点。 而且我们不会被从主造物主到主造物主的未经检查的施法打扰,如果我们这样施法,我们将不会受到警告

  return (T)this;

例如,如果要将多个字段的值封装到一个实体中,如Hibernate框架的方法session.getClass entityClass、String primaryKey。您需要定义entityClass,以便Hibernate知道如何将查询结果封装到实体中。 使用类作为参数的简单示例:

 public T getInstance(Class<T extends Serializable> clazz) throws Exception
   {
          // the ParameterType "T extend Serializable" means that:
          // the argument clazz must be a sub of the Interface Serializable
          if(null != clazz)
           {
               return clazz.newInstacne();
           }

         return null;
    }

例如,如果要将多个字段的值封装到一个实体中,如Hibernate框架的方法session.getClass entityClass、String primaryKey。需要定义entityClass,以便Hibernate知道如何将查询结果封装到实体中。 使用类作为参数的简单示例:

 public T getInstance(Class<T extends Serializable> clazz) throws Exception
   {
          // the ParameterType "T extend Serializable" means that:
          // the argument clazz must be a sub of the Interface Serializable
          if(null != clazz)
           {
               return clazz.newInstacne();
           }

         return null;
    }
愚蠢的例子:

public class PassClassExample {

    public static class ClassValidator {
        private Class theClass;
        public ClassValidator(Class theClass) {
            this.theClass = theClass;
        }

        public boolean instanceOf(Class someClass) {
            return theClass == someClass;
        }
    }

    public static void main (String [] args ) {
        ClassValidator personValidator = new ClassValidator(Person.class);

        Person you = new Person();
        Animal me = new Animal();

        System.out.println(personValidator.instanceOf(you.getClass()));
        System.out.println(personValidator.instanceOf(me.getClass()));

    }

    public static class Person {

    }

    public static class Animal {

    }
}
将打印出来

true
false
表示你是人而我是动物:

愚蠢的例子:

public class PassClassExample {

    public static class ClassValidator {
        private Class theClass;
        public ClassValidator(Class theClass) {
            this.theClass = theClass;
        }

        public boolean instanceOf(Class someClass) {
            return theClass == someClass;
        }
    }

    public static void main (String [] args ) {
        ClassValidator personValidator = new ClassValidator(Person.class);

        Person you = new Person();
        Animal me = new Animal();

        System.out.println(personValidator.instanceOf(you.getClass()));
        System.out.println(personValidator.instanceOf(me.getClass()));

    }

    public static class Person {

    }

    public static class Animal {

    }
}
将打印出来

true
false
表明你是人,我是动物: