ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C#中类和结构的一个区别...

C#中类和结构的一个区别...

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-02-12 16:48:46 0 删除 编辑

最近在努力学习C#语法...今晚在左一个二叉树的迭代遍历时发生了点错误...

代码如下:

 

 

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumeratorTestForPair
{
    class BinaryTree : IEnumerable where T : IComparable
    {

        public BinaryTree(T value)
        {
            Value = value;
        }

        private T _Value;

        public T Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        private Pair> _SubItems;

        public Pair> SubItems
        {
            get
            {
                return _SubItems;
            }
            set
            {
                IComparable first;
                first = value.First._Value;

                if (first.CompareTo(value.Second._Value) < 0)
                {

                }
                else
                {

                }

                _SubItems = value;
            }
        }
       
        //[System.Runtime.CompilerServices.IndexerName("Entry")]
        public T this[PairItem[] branches]
        {
            get
            {
                BinaryTree currentNode = this;
                int totalLevel = (branches == null) ? 0 : branches.Length;
                int currentLevel = 0;

                while (currentLevel < totalLevel)
                {
                    currentNode = currentNode.SubItems[branches[currentLevel]];
                    if (currentNode == null)
                    {
                        throw new IndexOutOfRangeException();
                    }
                    ++currentLevel;
                }
                return currentNode.Value;

            }
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            yield return Value;
            foreach (BinaryTree tree in SubItems)
            {
                if (tree != null)
                {
                    foreach (T item in tree)
                    {
                        yield return item;
                    }
                }
            }
           
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

    }
}

 

 

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumeratorTestForPair
{
    class Pair : IPair,
        IEnumerable
    {
        public Pair(T first)
        {
            this._First = first;
            this._Second = default(T);
        }

        public Pair(T first, T second)
        {
            this._First = first;
            this._Second = second;
        }

        #region IPair Members

        public T First
        {
            get
            {
                return _First;
            }

            private set
            {
                _First = value;
            }
        }
        private T _First;

        public T Second
        {
            get
            {
                return _Second;
            }

            private set
            {
                this._Second = value;
            }
        }
        private T _Second;

        public T this[PairItem index]
        {
            get
            {
                switch (index)
                {
                    case PairItem.First:
                        return First;
                    case PairItem.Second:
                        return Second;
                    default:
                        throw new NotImplementedException(
                            string.Format("The enum {0} has not been implemented."));
                           
                }
            }

            set
            {
                switch (index)
                {
                    case PairItem.First:
                        First = value;
                        break;
                    case PairItem.Second:
                        Second = value;
                        break;
                    default:
                        throw new NotImplementedException(
                            string.Format("The enum {0} has not been implemented."));
                }
            }
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            yield return First;
            yield return Second;
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion


        public System.Collections.Generic.IEnumerable
            GetNotNullEnumerator()
        {
            if (First == null || Second == null)
                yield break;
            yield return Second;
            yield return First;
        }

        public System.Collections.Generic.IEnumerable
            GetReverseEnumerator()
        {
            yield return Second;
            yield return First;
        }


    }
}

 

 

而调试代码是:...

 

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumeratorTestForPair
{
    class Program
    {
        static void Main(string[] args)
        {

            BinaryTree jfkFamilyTree = new BinaryTree("John Fizgerald Kennedy");

            jfkFamilyTree.SubItems = new Pair>(
                new BinaryTree("Joseph Patrick Kennedy"),
                new BinaryTree("Rose Elizabeth Fizgrald"));

            jfkFamilyTree.SubItems.First.SubItems = new Pair>(
                new BinaryTree("Patrick Joseph Kennedy"),
                new BinaryTree("Mary Augusta Hickey"));

            jfkFamilyTree.SubItems.Second.SubItems = new Pair>(
                new BinaryTree("John Francis Fizgerald"),
                new BinaryTree("Mary Hoseph Hannom"));

            foreach (string name in jfkFamilyTree)
            {
                Console.WriteLine(name);
            }


        }

    }


}

 

结果发生问题说 迭代到树尖时SubItems为null....后来在迭代中加了个判断是否为null就貌似解决了....但不对啊???为什么书没有啊?难道输错了...

 

最后 终于找到问题是录入错误把Pair弄成了class...而BinaryTree中持有一个Pair类型的SubItems的“引用”...不是值类型..这样会直接初始化为null所以错了

 

那怎么让他初始化为一个默认的Pair 加个构造函数可以 但书上是把class改成Struct后就是持有一个值类型了...这样初始化会调用一个结构的默认值(感觉像默认构造函数...),就是一个Pair...每个成员都是null...终于搞懂了...

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-548884/,如需转载,请注明出处,否则将追究法律责任。

下一篇: Web定时任务
请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5322283