C# - 如何从另一个'class ClassName <t>中调用一个方法,其中T:IComparable'</t>

时间:2014-08-06 16:56:33

标签: c# methods call binary-tree icomparable

在我的'Program.cs'中,我试图从另一个类'BinTree.cs'调用方法'inOrder()'。

类BinTree以class BinTree<T> where T : IComparable

开头

我试过了:

inOrder();

BinTree<T>.inOrder();

BinTree<int>.inOrder();

但这些都不起作用。

感谢您的光临。

编辑:

class Program
{
    static void Main(string[] args)
    {
        Node<int> tree = new Node<int>(6);
        tree.Left = new Node<int>(2);
        tree.Left.Right = new Node<int>(5);

        tree.Left.Right.Data = 3;

        tree.Right = new Node<int>(8);



        (new BinTree<int>()).inOrder();

    }

EDIT2:

    private Node<T> root;
    public BinTree()  //creates an empty tree
    {
        root = null;
    }
    public BinTree(Node<T> node)  //creates a tree with node as the root
    {
        root = node;
    }

1 个答案:

答案 0 :(得分:0)

据我所知,您需要创建一个BinTree<int>实例并在构造函数中传入Node<int>实例。像这样:

class Program
{
    static void Main(string[] args)
    {
        // Create Node instances for tree
        Node<int> node = new Node<int>(6);
        node.Left = new Node<int>(2);
        node.Left.Right = new Node<int>(5);
        node.Left.Right.Data = 3;
        node.Right = new Node<int>(8);

        // Create tree, set root node
        BinTree<int> tree = new BinTree<int>(node);
        tree.inOrder(); // Call inOrder method of tree instance
    }
}