1

我有以下类,其方法标记为未选中,给我一个警告。他们编译/工作正常,但我很感兴趣,如果有一种方法来泛化类,因为 IntelliJ IDEA 正在为我提供(但无法这样做)来删除警告。

public abstract class Attribute {

private final AttributedNode containerNode;
private boolean computationComplete;

public Attribute(AttributedNode node) {
    this.containerNode = node;
    this.computationComplete = false;
}

public AttributedNode getContainerNode() {
    return containerNode;
}

public Attribute getAttributeFromChildNode(int childIndex, AttributeNameEnum attributeName) {
    AttributedNode node = getContainerNode().getChild(childIndex);
    return node.getAttribute(attributeName);
}

public String getChildNodeText(int index) {
    return getContainerNode().getChild(index).getText();
}

@SuppressWarnings("unchecked")
public <T, E extends Attribute> List<T> getListOfChildAttributeValues(AttributeNameEnum name) {
    List<T> result = new LinkedList<>();
    for (AttributedNode node : this.getContainerNode().getChildren()) {
        E attribute = (E) node.getAttribute(name);
        result.add((T) attribute.getValue());
    }
    return result;
}

public <T extends Attribute> Object getAttributeValueFromContainer(AttributeNameEnum name) {
    return this.<T>getAttributeValueFromNode(name, this.getContainerNode());
}

public <T extends Attribute> Object getAttributeValueFromParent(AttributeNameEnum name) {
    return this.<T>getAttributeValueFromNode(name, this.getContainerNode().getParent());
}

@SuppressWarnings("unchecked")
public <T extends Attribute> Object getAttributeValueFromNode(AttributeNameEnum name, AttributedNode node) {
    T attribute = (T) node.getAttribute(name);
    return attribute.getValue();
}
// other methods eluded

PS我提前道歉,也许我缺乏对泛型的理解。

4

0 回答 0