简体   繁体   中英

Java Interface implementation problem

I have problem with sets. Required java.lang.String found String... What can i do there?

public interface Node {
        public <V> V get();
        public <V> void sets(V value);
    }

public enum MIBNodes implements Node {

    TEST {
         private String e;
        @Override
        public String get() {
            return "aa";
        }

        @Override
        public <String> void sets(String value) {
           e=value;
        }




    };


};

UPDATE
Each enum instance like TEST, TEST1... may have different type.. String, Integer or anyother... So public enum MIBNodes implements Node { cant become public enum MIBNodes implements Node<String> {

This is the Problem:

@Override
public <String> void sets(String value) {
        ^^^^^^
    e=value;
}

Here, String is a type variable (a re-definition of V ), not a java.lang.String . And I don't really think you can fix that without changing your design:

public interface Node<V> {
    public V get();
    public void sets(V value);
}

And in case you want your enum to be generic: that's impossible. Different enum items can't implement the same interface with different generic parameters.

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