简体   繁体   中英

Cast type T list to object

I have a method which takes generic T type parameters.

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.

But I am getting cannot resolve method 'getFirstName()' compilation error.

Please help!

Your calling getFirstName() method with the wrong reference.please find below code.

Keep this type casting '(Student) inputs.get(0)' in ().

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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