简体   繁体   English

Java - 多态性和 object class 设计

[英]Java - Polymorphism and object class design

I'm trying to practice generics and polymorphism.我正在尝试练习 generics 和多态性。 I have a Node class as follows:我有一个节点 class 如下:

public class Node<T> {
    public T name;
    public Node[] children;
}

Then a generic Tree class:然后是通用树 class:

public class Tree<T> {
    public Node<T> root;
}

Now, I'm trying to implement a binary tree.现在,我正在尝试实现二叉树。 A binary tree is a tree, thus satisfying is a rule of polymorphism.二叉树是一棵树,因此满足是多态性的规则。 I'm struggling with how to enforce that nodes in binary trees can have a maximum of two children.我正在努力解决如何强制二叉树中的节点最多可以有两个孩子。

Should I extend node class to Binary node and children array is always initialized to size 2?我应该将节点 class 扩展到二进制节点并且子数组始终初始化为大小 2? If BinaryTree extends Tree, how do I place the restriction on the children member variable?如果 BinaryTree 扩展 Tree,如何对子成员变量进行限制?

It's like I am trying to make:这就像我正在尝试制作:

BinryTree extends Tree
BinaryTreeNode extends Node

Where Node is a member variable in Tree and BinaryTreeNode is a member variable in BinaryTree.其中 Node 是 Tree 中的成员变量, BinaryTreeNode 是 BinaryTree 中的成员变量。 What would be a proper design?什么是合适的设计?

What about:关于什么:

public class Node<T> {
public T name;
public Node[] children;
public Node(int numOfChildren) { children = new Node[numOfChildren]; }
}

If you would like the node to hold 2 nodes call the constructor with 2, That way children will hold up to 2 nodes.如果您希望节点容纳 2 个节点,请使用 2 调用构造函数,这样子节点将容纳多达 2 个节点。 Also it would be better to use encapsulation and make children private and provide accessibility with getters and setters:此外,最好使用封装并使子级私有并通过 getter 和 setter 提供可访问性:

public class Node<T> {
public T name;
private Node[] children;

public Node(int numOfChildren) { children = new Node[numOfChildren]; }

public Node[] getChildren() { return children; }
}

Here is what I ended up with from the above suggestions.这是我从上述建议中得出的结论。 Node class:节点 class:

public abstract class Node<T> {
    protected T value;
    protected Node[] children;
}

BinaryTreeNode二叉树节点

public class BinaryTreeNode<T> extends Node {
    public BinaryTreeNode() {
        children = new BinaryTreeNode[2];
    }

    public BinaryTreeNode(T value) {
        this.value = value;
        children = new BinaryTreeNode[2];
    }

    public void setValue(T value) {
        this.value = value;
    }

    public T getValue() {
        return (T)value;
    }

    public BinaryTreeNode left() {
        return (BinaryTreeNode)children[0];
    }

    public BinaryTreeNode right() {
        return (BinaryTreeNode) children[1];
    }

    public void setLeft(BinaryTreeNode<T> leftNode) {
        children[0] = leftNode;
    }

    public void setRight(BinaryTreeNode<T> rightNode) {
        children[1] = rightNode;
    }
}

Tree Class:树 Class:

public abstract class Tree<T> {
    public Node<T> root;
}

BinaryTree:二叉树:

public class BinaryTree<T> extends Tree<T> {

    public BinaryTree() {
        root = new BinaryTreeNode<>();
    }

    public void visit(Node node) {
        System.out.println(node.value);
    }

    public void inOrderTraversal(BinaryTreeNode node) {
        if(node != null) {
            inOrderTraversal(node.left());
            visit(node);
            inOrderTraversal(node.right());
        }
    }
}

I apologize for lengthy comment.对于冗长的评论,我深表歉意。 Thankful for any feedback on improving design.感谢您对改进设计的任何反馈。

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

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