简体   繁体   English

Java - 使用 Generics 类型

[英]Java - Using Generics Types

What is the correct way to use the generic type in this situation?在这种情况下使用泛型类型的正确方法是什么? Consider the following code:考虑以下代码:

public class GTree<T> {
    private int level;
    private T value;
    private String key;
    private LinkedList<GTree<T>> subTrees;
    private GTree<T> parent;

    public class NodeEntry<T extends Comparable<? super T>> implements Comparable<T>{
        private T value;
        private String key;
        
        public NodeEntry(T v, String k) {
            this.value = v;
            this.key = k;
        }
        
        public T getValue() {
            return value;
        }
        
        public String getKey() {
            return key;
        }
        
        public int compareTo(T o) {
             return getValue().compareTo(o);
         }

        public int compareTo(GTree<T>.NodeEntry<T> o) {
            // TODO Auto-generated method stub
            return value.compareTo(o.getValue());
        }
    }

    private void getAllBranches(LinkedList<LinkedList<NodeEntry<T>>> path)
    {
        if(leaf())
        {
            LinkedList<NodeEntry<T>> lastPath = path.getLast();
            NodeEntry<T> p = new NodeEntry<T>(value, key);
            lastPath.addLast(p);
            return;
        }
        else
        {
            LinkedList<NodeEntry<T>> lastPath = path.getLast();
            NodeEntry<T> p = new NodeEntry<T>(value, key);
            lastPath.addLast(p);

            ....

            for(int i = 0; i < subTrees.size(); i++)
            {
                T x = subTrees.get(i).getValue();
                if(subTrees.get(i).leaf())
                {
                    lastPath.addLast(new NodeEntry<T>(subTrees.get(i).getValue(), subTrees.get(i).getKey()));
                    ....
                }
                else
                    subTrees.get(i).getAllBranches(path);
            }
        }
    }
}

The compiler is complaining about the generic type T whenever I use it, except when I define the variable x: Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends Comparable<?每当我使用泛型类型 T 时,编译器都会抱怨它,除非我定义了变量 x:绑定不匹配:类型 T 不是有界参数 <T extends Comparable<? super T>> of the type GTree. GTree 类型的 super T>>。 NodeEntry节点入口

The issue is that NodeEntry specifically is looking for a class that is of type T that extends Comparable but the GTree class does not have this bound.问题是 NodeEntry 专门寻找一个类型为T的 class extends Comparable但 GTree class 没有此限制。 To resolve this, you must either remove the bound requirement from NodeEntry to define the type parameter T from GTree with the upper bound T extends Comparable .要解决此问题,您必须从 NodeEntry 中删除绑定要求,以从 GTree 定义类型参数 T ,其上限为T extends Comparable

If NodeEntry expects the same value as GTree then in this specific case GTree's type parameter T should be bound to Comparable.如果 NodeEntry 需要与 GTree 相同的值,那么在这种特定情况下,GTree 的类型参数 T 应该绑定到 Comparable。

    public static class GTree<T extends Comparable<T>> {
        
        private int level;
        
        private T value;
        
        private String key;
        
        private LinkedList<GTree<T>> subTrees;
        
        private GTree<T> parent;

        public static class NodeEntry<T extends Comparable<T>> implements Comparable<T> {
            
            private T value;
            
            private String key;

            public NodeEntry(T v, String k) {
                this.value = v;
                this.key = k;
            }

            public T getValue() {
                return value;
            }

            public String getKey() {
                return key;
            }

            @Override
            public int compareTo(T o) {
                return value.compareTo(o);
            }
        }

        private void getAllBranches(LinkedList<LinkedList<NodeEntry<T>>> path) {
            // more
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM