[英]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的设计如下所示
A couple of obvious answers are: 一些明显的答案是:
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.