撰寫一個部門類Deptment :
欄位:包含一個用于存放員工的集合List<Employee>
屬性:部門名稱(DeptName),部門編號(DeptID)
索引器1:提供一個傳入員工姓名和員工編號引數的只寫索引器,實作將員工添加的部門中的功能
索引器2:提供一個傳入員工姓名的只讀屬性,實作通過員工姓名完成模糊查詢出所有匹配的員工物件。
class Deptment
{
public int DeptNo;
public string DeptName;
private List<Employee> emp;
public Deptment(int count)
{
emp = new List<Employee>(count);
}
///索引器1:提供一個傳入員工姓名和員工編號引數的只寫索引器,實作將員工添加的部門中的功能
public Employee this[string name, int ID]
{
set
{
foreach (Employee e in emp)
{
if (name != "" && name != e.name && ID != e.ID)
{
e.DeptName =value.DeptName;
}
}
}
}
///索引器2:提供一個傳入員工姓名的只讀屬性,實作通過員工姓名完成模糊查詢出所有匹配的員工物件。
public Employee this[string name]
{
get
{
while (true)
{
foreach(Employee emp1 in emp)
{
if (name == emp1.ToString())
{
return emp1;
}
else
for (int i = 0; i < emp1.ToString().Length; i++)
{
if (name == emp1.ToString().Substring(i, 1))
{
return emp1;
}
}
}
Employee emp = new Employee();
emp.name = "查無此人";
return null;
}
}
}
}
}
作業不會啊!
uj5u.com熱心網友回復:
為什么是索引,而不是方法?uj5u.com熱心網友回復:
2個 所引起即可 this[string memberName]; this[int memberNo] 型別不可以相同出現,和方法多載一樣,需要多元化形參。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/133772.html
標籤:C#
上一篇:EF6 怎么實作外鍵表關聯查詢
