简体   繁体   中英

how to define a method that takes a generic type as parameter from certain parent class

I know that I can use Object as the parameter type , but then I have to cast to the class I want.

My question is: Can I do something like that

public void doSomething( <T extends Person> person ) { 
              Student student  =  person ;  
}

like what we can achieve with return type in java

public <T extends Person> T findPerson() { ;;;;;}
Student student =  findPerson(1) ;

English is not my native language, so sorry if the question is not clear enough

You can use it like

public <T extends SomeClass> void doSomething(T item){
   //Do something
   item.number = 4;
}

Where:

class SomeClass{
    public int number;
}

Check this article , I hope it will help you.

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