马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

一个泛型的例题代码,看了有几点疑问,求解答

  [复制链接]
查看4762 | 回复5 | 2013-11-23 11:32:13 | 显示全部楼层 |阅读模式
这个就是定义一个泛型类LinkedList,做一个针对(整型数据和Student类型数据)的链表
Student类型已在开头定义好 ,Node就是针对链表中的节点进行一些操作,LinkedList就是调用Node中的一些方法再加上自己的方法实现链表的功能,一下是代码。
我的问题代码在后面提出。



[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericSamp
{
    public class Student
    {
        private string StudentName;
        private int StudentAge;
        public Student(string name, int age)
        {
            this.StudentName = name;
            this.StudentAge = age;
        }
        public override string ToString()
        {
            return this.StudentName + ":年龄" + this.StudentAge + "岁";
        }
    }
    public class Node<T>
    {
        T data;
        Node<T> next;
        public Node(T data)
    {
        this.data=data;
        this.next=null;
    }
    public T Data{
    get {return this.data;}
    set{data=value;}
}
    public Node<T> Next{
    get{return this.next;}
    set{this.next=value;}

}
    public void Append(Node<T> newNode){
    if(this.next==null){this.next=newNode;}
    else{next.Append(newNode);}
}
    public override string ToString(){
    string output=data.ToString();
    if(next!=null){output+=","+next.ToString();}
    return output;
}
    }
    public class LinkedList<T>
    {
        Node<T> headNode = null;
        public void Add(T data)
        {
            if (headNode == null)
            {
                headNode = new Node<T>(data);
            }
            else
            {
                headNode.Append(new Node<T>(data));
            }
        }
        public T this[int index]
        {
            get
            {
                int temp = 0;
                Node<T> node = headNode;
                while (node != null && temp <= index)
                {
                    if (temp == index)
                    {
                        return node.Data;
                    }
                    else
                    {
                        node = node.Next;
                    }
                    temp++;
                }
                return default(T);

            }

        }
        public override string ToString()
        {
            if (this.headNode != null)
            {
                return this.headNode.ToString;
            }
            else
            {
                return string.Empty;
            }
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            LinkedList<int> intList=new LinkedList<int>();
            for (int i = 0; i < 5; i++)
            {
                intList.Add(i);
            }
            Console.WriteLine("整型List的内容为:" + intList);
            LinkedList<Student> StudentList = new LinkedList<Student>();
            StudentList.Add(new Student("张三",20));
            StudentList.Add(new Student("李四",22));
            StudentList.Add(new Student("王五",21));
            Console.WriteLine("Student类型List的内容为:" + StudentList);
        }
    }
}


第一个问题:在LinkedList泛型那段中的一个方法public T this[int index],这段代码如何实现的对LinkedList数组中的每一个元素赋值,求详细分析过程。
第二个问题:在Node类和LinkedList类定义的时候,最后都重载了一个Tostring方法,有什么作用,想用这个来干什么,去掉了又什么影响,为什么。

问题可能不好回答,还是求大神帮忙了

ibcadmin | 2013-11-23 13:12:46 | 显示全部楼层
第一个问题没懂

第二个问题, 重写ToString() ,  在这个重写中,主要判断了headNode是否为空,如果不为空,就将headNode类型转换成string类型,  如果为空, 就返回NULL
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
桂林一枝花 | 2013-11-23 13:39:35 | 显示全部楼层
你的泛型有点看不懂。T是什么。
wang1234587 | 2013-11-23 17:26:26 | 显示全部楼层
ibcadmin 发表于 2013-11-23 13:12
第一个问题没懂

第二个问题, 重写ToString() ,  在这个重写中,主要判断了headNode是否为空,如果不为 ...

第一个问题我知道它是一个索引器,我的里解就是他就相当于一个带一个参数的函数,每次调用它都会返回当前LinkedList[index]这个值,有点类似于数组;
但是我有一个疑问,这个索引器是如何与主函数里Console.WriteLine以及ToString配合,实现了对当前类的对象所包含内容的输出呢?   就是说,最后这一行代码Console.WriteLine("整型List的内容为:" + intList);  它的具体实现过程是怎样的,能分析下吗,谢了
wang1234587 | 2013-11-23 17:26:59 | 显示全部楼层
修身以求齐家 发表于 2013-11-23 13:39
你的泛型有点看不懂。T是什么。

T当然是泛型的类型参数了。。。
ibcadmin | 2013-11-23 18:18:12 | 显示全部楼层
wang1234587 发表于 2013-11-23 17:26
第一个问题我知道它是一个索引器,我的里解就是他就相当于一个带一个参数的函数,每次调用它都会返回当前 ...
1.LinkedList<T> 被定义了一个泛型。
2.当执行到代码
LinkedList<int> intList = new LinkedList<int>();
            for (int i = 0; i < 5; i++)
            {
                intList.Add(i);
            }

这的时候 ,   intList 被定义为int类型的泛型,并且给赋值 变成如下:

intList[0]=0
intList[1]=1
intList[2]=2
intList[3]=3intList[4]=4

3.当你直接输出intList对象的时候,按理说应该是报错的。 但是你这有写一句代码:

if (next != null) { output += "," + next.ToString(); }

如果类型不等于空的时候, 讲输出对象集合,并且以“,”号拼接, 然后将int转换为string,
到这一步,  你的intList 泛型对象已经不是泛型对象了,而是一个string类型的字符串,
然后就输出了 : 整型List的内容为:0,1,2,3,4

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则