Java 泛型类模板

标签 java generics

试图创建一个具有多个节点的 N 叉树,节点对象类型不同 [国家 |状态等],我尝试修改下面的泛型类 -

https://github.com/vivin/GenericTree/blob/master/src/main/java/net/vivin/GenericTreeNode.java

我尝试了以下 -

package com.mycompany.ds;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GenericTreeNode<T>{

    private T data;
    private List<GenericTreeNode<? super T>> children;
    private GenericTreeNode<? super T> parent;

    public GenericTreeNode() {
        super();
        children = new ArrayList<GenericTreeNode<? super T>>();
    }

    public GenericTreeNode(T data) {
        this();
        setData(data);
    }

    public GenericTreeNode<? super T> getParent() {
        return this.parent;
    }

    public List<GenericTreeNode<? super T>> getChildren() {
        return this.children;
    }

    public int getNumberOfChildren() {
        return getChildren().size();
    }

    public boolean hasChildren() {
        return (getNumberOfChildren() > 0);
    }

    public void setChildren(List<GenericTreeNode<? super T>> children) {
        for(GenericTreeNode<? super T> child : children) {
           child.parent = this;
        }

        this.children = children;
    }

    public void addChild(GenericTreeNode<? super T> child) {
        child.parent = this;
        children.add(child);
    }

    public void addChildAt(int index, GenericTreeNode<T> child) throws IndexOutOfBoundsException {
        child.parent = this;
        children.add(index, child);
    }

    public void removeChildren() {
        this.children = new ArrayList<GenericTreeNode<? super T>>();
    }

    public void removeChildAt(int index) throws IndexOutOfBoundsException {
        children.remove(index);
    }

    public GenericTreeNode<? super T> getChildAt(int index) throws IndexOutOfBoundsException {
        return children.get(index);
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return getData().toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
           return true;
        }
        if (obj == null) {
           return false;
        }
        if (getClass() != obj.getClass()) {
           return false;
        }
        GenericTreeNode<?> other = (GenericTreeNode<?>) obj;
        if (data == null) {
           if (other.data != null) {
              return false;
           }
        } else if (!data.equals(other.data)) {
           return false;
        }
        return true;
    }

    /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
    @Override
    public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((data == null) ? 0 : data.hashCode());
       return result;
    }

    public String toStringVerbose() {
        String stringRepresentation = getData().toString() + ":[";

        for (GenericTreeNode<? super T> node : getChildren()) {
            stringRepresentation += node.getData().toString() + ", ";
        }

        //Pattern.DOTALL causes ^ and $ to match. Otherwise it won't. It's retarded.
        Pattern pattern = Pattern.compile(", $", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(stringRepresentation);

        stringRepresentation = matcher.replaceFirst("");
        stringRepresentation += "]";

        return stringRepresentation;
    }
}

但是下面方法的错误——

 public void setChildren(List<GenericTreeNode<? super T>> children) {
        for(GenericTreeNode<? super T> child : children) {
           child.parent = this;
        }

        this.children = children;
    }

    public void addChild(GenericTreeNode<? super T> child) {
        child.parent = this;
        children.add(child);
    }

错误-

1 - Type mismatch: cannot convert from GenericTreeNode<T> to GenericTreeNode<? super capture#2-of ? super 
 T>

2 - Type mismatch: cannot convert from GenericTreeNode<T> to GenericTreeNode<? super capture#4-of ? super 
 T>

我该如何解决这些问题?

最佳答案

您可以创建一个表示 GISEntity 的类/接口(interface),并创建其通用类型 T 扩展 GISEntity 的通用树节点。这将允许您拥有不同种类的 GISEntity 子类的节点——国家/州等。

关于Java 泛型类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14106285/

相关文章:

java - 奇怪的 HashMap 结果 - Java,Hadoop

java - android ImageView 内存不足错误

java - Blackberry 上的 XML 解析问题

java - 将请求参数转换为泛型类

java - 如何使用java Socket编程在同一个文件中实现对等代码,即服务器和客户端代码

Java - 使用音频剪辑时出现问题

c# - 如何在 T 为 double 或 Int 时在通用方法中使用 <T>.TryParse

c# - 如何编写通用扩展方法?

c# - 如何在 C# 中以更通用的方式编写这些方法?

ios - 将 IBOutlet/对象传递到可重复动画的通用函数中