简体   繁体   English

将类型 T 列表转换为对象

[英]Cast type T list to object

I have a method which takes generic T type parameters.我有一个采用泛型 T 类型参数的方法。

public<T> void run(List<T> inputs){
 System.out.println((Student) inputs.get(0).getFirstName());

}

I am trying to access Student class methods inside run method.我正在尝试访问 run 方法中的 Student 类方法。

But I am getting cannot resolve method 'getFirstName()' compilation error.但我得到了无法解决方法 'getFirstName()'编译错误。

Please help!请帮忙!

Your calling getFirstName() method with the wrong reference.please find below code.您使用错误的引用调用 getFirstName() 方法。请找到以下代码。

Keep this type casting '(Student) inputs.get(0)' in ().在 () 中保持这种类型的铸造 '(Student)inputs.get(0)'。

public<T> void run(List<T> inputs){
 System.out.println( ((Student) inputs.get(0)).getFirstName());

}

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

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