简体   繁体   English

应该何时接受类类型作为参数?

[英]When should a method accept class type as parameter?

I have seen API's designed where they at times accept class types to be passed as parameter's. 我已经看到了API的设计,它们有时会接受作为参数传递的类类型。 For eg[a]: 对于例如[a]:

class AClass {
//details
}

class Someclass {

 public void someMethod(class klass, int num ) {
 // some code
 }

}

class client {

public static void main(String[] args) {
   Someclass obj = new Someclass();
   obj.someMethod(AClass.class, 10); // some sample use cases of client
}

}

Now in java, passing of class is not at all required if its possible to get the class type from the object, as explained below[b] 现在在java中,如果可以从对象获取类类型,则根本不需要传递类,如下所述[b]

class AClass {
//details
}

class Someclass {

 public void someMethod(AClass obj, int num ) {
 // some code
 }

}

class client {

public static void main(String[] args) {
   Someclass obj = new Someclass();
   AClass aclassObj = new Aclass();
   obj.someMethod(aclassObj, 10); 
}

}

I can also change the parameter of someMethod() and use as given below [c] 我也可以改变someMethod()的参数并使用下面给出的[c]

public void someMethod(Object obj, int num ) {

 if(obj instanceof AClass) {
  //do something 
 }

}

So when should we choose the design as shown in [a] instead of [b] ? 那么我们何时应该选择[a]中所示的设计而不是[b]? Are there any general principles to consider for design shown in [a]. 是否有任何一般原则要考虑[a]中所示的设计。

PS: Some API's designed like that are shown below PS:某些API的设计如下所示

Quartz example 石英的例子

A couple of obvious answers are: 一些明显的答案是:

  1. Any time the method doesn't actually take the object as a parameter (eg when you request an instance of a type from a DI framework or factory, such as in the Quartz example you provided). 任何时候该方法实际上并不将对象作为参数(例如,当您从DI框架或工厂请求类型的实例时,例如在您提供的Quartz示例中)。
  2. Any time the parameter you pass in might be null, so you cannot call getType on it, and instanceof won't indicate that the object is an instanceof any type. 每次传入的参数可能为null,因此您无法在其上调用getType ,而instanceof也不会指示该对象是任何类型的instanceof

So it makes sense to take a class argument when the method in question either needs to produce an instance of the class in question or provide some metadata about that class which shouldn't require an instance to be created. 因此,当有问题的方法需要生成有问题的类的实例或提供有关该类的一些元数据(不应该要求创建实例)时,采用class参数是有意义的。

The only other consideration I'd point out is that it is possible to use generics when accepting a class parameter, as in the following method signature on Guice's Injector interface: 我要指出的唯一另一个考虑因素是,在接受类参数时可以使用泛型,如Guice的Injector接口中的以下方法签名:

<T> T getInstance(Class<T> type);

Notice how this allows the method to declare that it's returning an object of the same type that you pass in as a class parameter, so you don't need to cast the object afterward. 注意这是如何允许方法声明它返回一个你作为类参数传入的相同类型的对象,因此你不需要在之后强制转换对象。 You can also use the generic wildcards ( Class<? extends Foo> ) to restrict the types of classes that can be passed into the method. 您还可以使用通用通配符( Class<? extends Foo> )来限制可以传递给方法的类的类型。

Pass a class description (a Class object) if you need it to do processing. 如果需要进行处理,则传递类描述(Class对象)。 Pass an object (which is an instance of the class) if you need the object to do processing. 如果需要对象进行处理,则传递一个对象(该类的实例)。

Another example would be here - where the type of the returned object (in this case the objects in the list) depends on the class type. 另一个例子是在这里 -在返回的对象的类型(在这种情况下,在列表中的对象)取决于类的类型。

Many instances where it would be required could now potentially be replaced by designing an API that uses generics, but for some problems it still offers a concise and easy to understand syntax. 现在可以通过设计使用泛型的API来替换许多需要它的实例,但是对于某些问题,它仍然提供简洁易懂的语法。

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

相关问题 为什么新类的equals()方法的参数应该是Object类型? - Why should the parameter of an equals() method of a new class be of type Object? 如何限制方法仅接受对象作为参数而不是类对象作为类型文字? - How do I restrict method to accept only object as parameter instead of Class Objects as Type Literals? 功能参数类别 <Type> 不会接受类型的孩子 - Function parameter Class<Type> won't accept child of type 有没有办法通过以下方式接受类型作为 Java 中静态方法的参数? - Is there any way to accept type as an parameter for a static method in Java in the following way? 如何测试Method是否接受参数类型? - How can I test if a Method will accept a parameter type? 编写一个方法来接受接口类型而不是类类型 - Writing a method to accept interface type instead of class type 如何使方法接受Java中的类类型变量? - How can I make a method accept a class type variable in Java? 为什么编译器在调用一个不相关的接口类型时会选择这个带有类类型参数的泛型方法? - Why does the compiler choose this generic method with a class type parameter when invoked with an unrelated interface type? 将类型参数的类作为Java中泛型方法的类型参数传递 - Passing a class with type parameter as type parameter for generic method in Java 在方法中添加绑定到类类型参数 - Add bound to class type parameter in method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM