简体   繁体   English

如何避免使用通配符转换继承的递归类?

[英]How to avoid casting on inherited recursive class with wildcard?

1) Suppose you have the following abstract class definition: 1)假设您有以下抽象类定义:

abstract class AbstractBinaryTree<T> {
    AbstractBinaryTree<T> parent;
    AbstractBinaryTree<T> leftChild;
    AbstractBinaryTree<T> rightChild;
    T value;     
}

and an implementation of this class with a new method not previously declared or implemented: 以及之前未声明或实现的新方法的此类的实现:

public class BinarySearchTree<T extends Comparable<T>> extends AbstractBinaryTree<T> {
    public BinarySearchTree(T pVal) {
        super(pVal);
    }


    public Boolean isBST(){
    if(leftChild != null && rightChild != null){
        return (leftChild.value.compareTo(value) < 0 
                && rightChild.value.compareTo(value) >= 0 )
                && ((BinarySearchTree<T>) leftChild).isBST() 
                && ((BinarySearchTree<T>) rightChild).isBST();
    }
    else if(leftChild != null){
        return leftChild.value.compareTo(value) < 0 
                && ((BinarySearchTree<T>) leftChild).isBST() ;
    }
    else if (rightChild != null){
        return rightChild.value.compareTo(value) >= 0
        && ((BinarySearchTree<T>) rightChild).isBST();
    }
    else{
        return true;
    }
}

How do you avoid having to cast all left and right children? 你如何避免抛弃所有左右儿童?

2) Similarly suppose i had the following abstract definition in AbstractBinaryTree: 2)同样假设我在AbstractBinaryTree中有以下抽象定义:

    public abstract AbstractBinaryTree<T> findMinTree();

and its implementation in BST: 及其在BST中的实施:

/***
 * @return the subtree rooted at the min value
 */
public BinarySearchTree<T> findMinTree(){
    if(leftChild != null)
        return (BinarySearchTree<T>) leftChild.findMinTree();
    return this;
}

How do I avoid the cast in 我该如何避免演员表演

public BinarySearchTree<T> findMinTree(){
    if(leftChild != null)
        return (BinarySearchTree<T>) leftChild.findMinTree();
    return this;
}

or when i call it on a child? 或者当我给孩子打电话的时候?

BinarySearchTree<T> y = ((BinarySearchTree<T>) x.rightChild).findMinTree();

I am not allergic to casting but its very heavy in this case. 我对铸造不过敏,但在这种情况下非常重。 Thanks in advance for your answers! 提前感谢您的回答!

You can use even more generics, namely the CRTP : 您可以使用更多的泛型,即CRTP

abstract class AbstractBinaryTree<T, TTree extends AbstractBinaryTree<T, TTree>> {
    TTree parent;
    TTree leftChild;
    TTree rightChild;
    T value;     
}

Instead of the class having the abstract superclass refer to a reference of itself for the structure of the tree, I would have it use a Node class that has references to its parent Node , and left and right children Nodes . 而不是具有抽象超类的类引用自身对树结构的引用,我会让它使用一个Node类,它引用了它的父Node ,以及左右子Nodes The AbstractBinaryTree class would have a reference to the root Node . AbstractBinaryTree类必须根参考Node

abstract class AbstractBinaryTree<T> {
    Node<T> root;
    static class Node<E>
    {
        Node<E> parent;
        Node<E> leftChild;
        Node<E> rightChild;
        E value;
    }
}

Then subclasses wouldn't need the type of the Node to vary with its own type; 那么子类不需要Node的类型随其自己的类型而变化; BinarySearchTree would also use Node s. BinarySearchTree也将使用Node

class BinarySearchTree<T extends Comparable<T>> extends AbstractBinaryTree<T>
{
    // No need to redefine the structure types here.
}

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

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