开发者

Implementing polymorphic Java interfaces with specific types

开发者 https://www.devze.com 2023-01-29 19:58 出处:网络
I want to implement the Map<K,V> interface开发者_运维技巧, but I want to constrain K to type String.

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.


try : class Test< V > implements Map< String, V > hope it helps


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> { /* ... */ }
0

精彩评论

暂无评论...
验证码 换一张
取 消