简体   繁体   English

C#在泛型类中定义自动属性

[英]C# defining Automatic Properties in Generic Class

I'm new in C# and I am completing the book "Microsoft Visual C# 2013 Step by Step" written by John Sharp. 我是C#的新手,我正在完成John Sharp写的《 Microsoft Visual C#2013 Step by Step》一书。 An exercise, regarding "Generics", I found this code: 关于“泛型”的练习,我找到了以下代码:

public class Tree<TItem> where TItem : IComparable<TItem>
{
    public TItem NodeData { get; set; }
    public Tree<TItem> LeftTree { get; set; }
    public Tree<TItem> RightTree { get; set; }

    public Tree(TItem nodeValue)
    {
        this.NodeData = nodeValue;
        this.LeftTree = null;
        this.RightTree = null;
    }

    public void Insert(TItem newItem)
    {
        TItem currentNodeValue = this.NodeData;
        if (currentNodeValue.CompareTo(newItem) > 0)
        {
            // Insert the new item into the left subtree
            // code here....
        }
        else
        {
            // Insert the new item into the right subtree
            // code here....
        }
    }

}

I can't understand why he defined the properties in different mode. 我不明白他为什么以不同的方式定义属性。 One in this way: 一种这样的方式:

public TItem NodeData { get; set; }

And the others in this: 还有其他的:

public Tree<TItem> LeftTree { get; set; }
public Tree<TItem> RightTree { get; set; }

Someone can explain me why? 有人可以解释我为什么? Thank you 谢谢

These properties are being used for different things. 这些属性用于不同的事物。 As their name suggest: 顾名思义:

  • NodeData is used to facilitate the information stored in the tree. NodeData用于促进存储在树中的信息。
  • LeftTree / RightTree are there to facilitate the topology of the tree - each current object (node) is basically a root of a tree rooted at itself. LeftTree / RightTree有助于树的拓扑结构-每个当前对象(节点)基本上都是RightTree自身为根的树的根。 So as it is a binary tree it has two direct descendants - the left and the right nodes. 因此,由于它是二叉树,因此具有两个直接后代-左节点和右节点。

Where the part where the generics come to play is about the what is the kind of data stored in the tree. 泛型发挥作用的地方是关于树中存储的数据的类型。 So the NodeData is trivially of "type" TItem . 因此, NodeData的类型很简单,即TItem The left and right nodes are of type Tree<TItem> so to ensure that at any depth of the tree it is a TItem type of data that is stored. 左节点和右节点的类型为Tree<TItem>因此要确保在树的任何深度处都是存储的TItem类型的数据。

To make it simpler lets suppose that you wanted to create a binary tree of integers. 为了简化起见,假设您想创建一个整数的二叉树。 Then you'd model it by: 然后,您可以通过以下方式对其进行建模:

public class Tree 
{
    public int Data { get; set; }
    public Tree Left {get; set; }
    public Tree Right {get; set; }
}

I think this way you can really see what is the fundamental difference between the Data and Left , Right properties. 我认为通过这种方式,您可以真正看到DataLeftRight属性之间的根本区别。

He defines a tree. 他定义了一棵树。 The NodeData property is the current node value. NodeData属性是当前节点值。 Then if the value is smaller than the the current node, the new value is put on the left, otherwise on the right. 然后,如果该值小于当前节点,则将新值放在左侧,否则在右侧。 If the type of LeftValue and RightValue are Tree, it's to have a parent-child structure. 如果LeftValue和RightValue的类型为Tree,则它将具有父子结构。 This class allows to create a data structure like binary tree. 此类允许创建类似二进制树的数据结构。

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

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