摘要:(200-07-07) C#.NET 集合(Collections) - 泛型(Generic)
泛型(Generic) 不著重規劃(類別)
泛型(彈性)===>類名型別,Detegate(委派)
Employee 類別
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace csmod04
{
public class Employee
{
//Data Field
private String _id;
public String Id
{
get { return _id; }
set { _id = value; }
}
private String _name;
public String Name
{
get { return _name; }
set { _name = value; }
}
private DateTime _birthDate;
public DateTime BirthDate
{
get { return _birthDate; }
set { _birthDate = value; }
}
private Decimal _salary;
//讀取的屬性
public Decimal Salary
{
get { return _salary; }
}
//建構子
public Employee() { }
//建構子overLoading
public Employee(String _id,String _name,DateTime _birthDate)
{
this._id = _id;
this._name = _name;
this._birthDate = _birthDate;
}
}
}Salse 類別
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace csmod04
{
//繼承性
public class Sales:Employee
{
//Data Field
private Decimal _qa;
public Decimal Qa
{
get { return _qa; }
set { _qa = value; }
}
//建構子
public Sales()
{
}
public Sales(String _id, String _name, DateTime _birthDate):base(_id,_name,_birthDate)
{
}
}
}主程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//限制員工類型(其他型不收)
namespace csmod04
{
class TestEmployeeCol
{
public static void Main()
{
//泛型
List<Employee> col = new List<Employee>();
col.Add(new Employee("0001", "eric", new DateTime(2000, 1, 1)));
col.Add(new Sales("0002", "bill", new DateTime(2000, 1, 1)));
col.Add(new Employee("0003", "sam", new DateTime(2000, 1, 1)));
//逐一參考
foreach (Employee e in col)
{
if (e is Sales)
{
((Sales)e).Qa = 20000;
}
}
}
}
}