简体   繁体   中英

Java generics on method

Might be a dumb question but I would like to ask how to make this possible in Java. I have an interface that has a method with the following signature

public <ComponentType> void attachFacilitiesTable(ComponentType comp);

On my concrete implementation I would like override it to something like:

public void attachFacilitiesTable(Node comp);

Thanks

Regards, Ezekiel

If I understand correctly, ComponentType is a base class, and Node is something that extends it?

If it is so, then I think this is the syntax you are looking for:

public <T extends ComponentType> void attachFacilitiesTable(T comp);

This way, you say, T is a generic type that must extend from ComponentType.

Your example is equivalent to

public <T> void attachFacilitiesTable(T comp);

Where T is a type, without any restrictions. You are just using a different "variable" name, which, although named ComponentType, doesn't have anything to do with the class named ComponentType.

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