简体   繁体   English

方法 insert(String, Class<t> ) 类型中的数据不适用于 arguments</t>

[英]The method insert(String, Class<T>) in the type Data is not applicable for the arguments

I want to have a function that takes in a generic type argument, but I am getting error message for using this.我想要一个接受泛型类型参数的 function,但我收到使用它的错误消息。 How do I use generic the right way in Java?如何在 Java 中正确使用泛型?

Plane plane = new Plane();
Car car = new Car();
data.insert("fast", car);
data.insert("super fast", plane);
...
<T> void insert(String a, Class<T> object) {
    System.out.println("Inserting " + object.getName() + a);
}

Your insert function is expecting a Class object like Plane.class , that's why object.getName() is known. Your insert function is expecting a Class object like Plane.class , that's why object.getName() is known.

I think your intention would be something like:我认为你的意图是这样的:

<T extends HasName> void insert(String a, T object) {
    System.out.println("Inserting " + object.getName() + a);
}

where HasName is an interface(could be a super class, too), with method getName , that is implemented(extended) by Car and Plane.其中 HasName 是一个接口(也可以是一个超级 class),使用方法getName ,由 Car 和 Plane 实现(扩展)。 That's how to ensure that object.getName() can be called.这就是确保可以调用object.getName()的方法。

暂无
暂无

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

相关问题 方法 valueOf(Class<t> , String) 类型的 Enum 不适用于 arguments (Class <enum<t> &gt;,字符串) </enum<t></t> - The method valueOf(Class<T>, String) in the type Enum is not applicable for the arguments (Class<Enum<T>>, String) fromMessage(String,Class <T extends BaseObject> )中的MessageConverter类型不适用于参数(字符串,类 <T> ) - The method fromMessage(String, Class<T extends BaseObject>) in the type MessageConverter is not applicable for the arguments (String, Class<T>) 类Class中的方法foo(T [])不适用于自变量(向量 <Integer> ) - The method foo(T[]) in the type Class is not applicable for the arguments (Vector<Integer>) 方法播放(类 <T> )不适用于参数(类) - The method play(Class<T>) is not applicable for the arguments (Class) 类型中的方法不适用于参数(类 <Main> )? - The method in the type is not applicable for the arguments (Class<Main>)? 部分类型中的方法setText(String)不适用于参数 - method setText(String) in the type Part is not applicable for the arguments 类型为nammi的方法epli(String [])不适用于arguments() - The method epli(String[]) in the type nammi is not applicable for the arguments () 类型“ x”的方法search()不适用于参数(字符串) - The method search() in the type “x” is not applicable for the arguments (String) 类型中的方法不适用于参数 - Method in the type is not applicable to the arguments 类型中的方法不适用于参数 - The method in the type is not applicable for the arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM