简体   繁体   中英

Implementing polymorphic Java interfaces with specific types

I want to implement the Map<K,V> interface, but I want to constrain K to type String .

Is this possible? Or does the interface definition impose that K be polymorphic?

If it is possible, could someone help with the class signature, tks.

尝试: class Test< V > implements Map< String, V >希望对您有所帮助

No problem:

public class YourMap implements Map<String, Object> { /* ... */ }

If the class should be generic you can do:

public class YourMap<K extends String,V> implements Map<K, V> { /* ... */ }

But since String is final (as @Peter Lawrey commented), you might as well just do:

public class YourMap<V> implements Map<String, V> { /* ... */ }

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