http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 1822|回复: 2

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

[复制链接]
发表于 2013-11-23 11:44:24 | 显示全部楼层 |阅读模式
这个就是定义一个泛型类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方法,有什么作用,想用这个来干什么,去掉了又什么影响,为什么。

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


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-11-23 17:28:57 | 显示全部楼层
@站长苏飞
我知道第一个其实是一个索引器,我的里解就是他就相当于一个带一个参数的函数,每次调用它都会返回当前LinkedList[index]这个值,有点类似于数组;
但是我有一个疑问,这个索引器是如何与主函数里Console.WriteLine以及ToString配合,实现了对当前类的对象所包含内容的输出呢?   就是说,最后这一行代码Console.WriteLine("整型List的内容为:" + intList);  它的具体实现过程是怎样的,能分析下吗,谢了
发表于 2013-11-23 17:34:27 | 显示全部楼层
我双休,手机看不方便,等上班了,看看,你先研究下
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-18 04:18

© 2014-2021

快速回复 返回顶部 返回列表