黑马程序员技术交流社区
标题:
c#泛型类使用实例
[打印本页]
作者:
冯华亮
时间:
2012-8-6 12:35
标题:
c#泛型类使用实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 集合类测试
{
class Program
{
static void Main(string[] args)
{
List<Student> stu = new List<Student>();
Student[] st = { new Student("黑马—冯华亮", 111),new Student("黑马-孤独求败", 22), new Student("黑马-马克西姆", 34) };
//Student[] st5 = { new Student(){stuname="zhangsan",stunum=27},new Student(){stuname="lisi",stunum=29}};
for (int i = 0; i < st.Length; i++)
{
stu.Add(st[i]);
}
stu.Sort(Student.numobj);
foreach(Student s in stu)
{
Console.WriteLine(s.ToString());
Console.ReadLine();
}
}
}
}class Student:IComparable<Student>
{
private string stuname;
private int stunum;
private class numsorter : IComparer<Student>
{
public int Compare(Student stu1, Student stu2)
{
return stu1.stuname.CompareTo(stu2.stuname);
}
}
public static IComparer<Student> numobj
{
get { return new numsorter(); }
}
public Student(string name, int num)
{
stuname = name;
stunum = num;
}
//public string stuname { get; set; }
//public int stunum { get; set; }
public override string ToString()
{
return "该生的信息:\n" + stuname + "---" + stunum;
}
public int CompareTo(Student stu1)
{
return this.stunum.CompareTo(stu1.stunum);
}
}
复制代码
作者:
许庭洲
时间:
2012-8-6 13:54
值得学习!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2