110,538
社区成员
发帖
与我相关
我的任务
分享
public class Persion
{
public string Name { get; set; }
}
public class Student : Persion
{
}
public class Test1<T>
{
private IList<T> list;
public virtual IList<T> List
{
get
{
if (this.list == null)
{
this.list = new List<T>();
}
return this.list;
}
set
{
this.list = value;
}
}
}
public class Test2<T> : Test1<T>
{
private IList<T> list;
public override IList<T> List
{
get
{
if (this.list == null)
{
this.list = new List<T>();
}
return this.list;
}
set
{
this.list = value;
}
}
}
class Program
{
static void Main(string[] args)
{
IList<Student> list = new List<Student>();
list.Add(new Student() { Name = "a" });
list.Add(new Student() { Name = "b" });
list.Add(new Student() { Name = "c" });
Test2<Student> a = new Test2<Student>();
a.List = list;
foreach (Persion item in a.List)
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
}
namespace ConsoleApplication1
{
public abstract class PersonBase
{
/// <summary>
/// 编号
/// </summary>
public int ID { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
}
public class Teacher : PersonBase
{
/// <summary>
/// 工龄
/// </summary>
public int WorkYear { get; set; }
}
public class Student : PersonBase
{
/// <summary>
/// 年级
/// </summary>
public int Grade { get; set; }
}
public interface IOperate<T>
{
T this[int index] { get; set; }
IList<T> List { get; set; }
void AcceptNotice(Func<T, string> fun);
}
public class TeacherOp<T> : IOperate<T>
{
private IList<T> list;
public T this[int index]
{
get { return list[index]; }
set { list[index] = value; }
}
public virtual IList<T> List
{
get
{
if (list == null)
{
list = new List<T>();
}
return list;
}
set
{
list = value;
}
}
public void AcceptNotice(Func<T, string> fun)
{
foreach (T t in List)
{
Console.WriteLine(fun(t));
}
}
}
class Program
{
static void Main(string[] args)
{
IList<Teacher> list = new List<Teacher>();
list.Add(new Teacher() { ID = 1, Name = "李老师", WorkYear = 1 });
list.Add(new Teacher() { ID = 2, Name = "王老师", WorkYear = 3 });
list.Add(new Teacher() { ID = 1, Name = "张老师", WorkYear = 5 });
IOperate<Teacher> op = new TeacherOp<Teacher>();
op.List = list;
op.AcceptNotice(Notice);
Console.ReadLine();
}
static string Notice(Teacher teacher)
{
return string.Format("{0},放假{1}天", teacher.Name, teacher.WorkYear);
}
}
}