永发信息网

c#的泛型集合问题

答案:1  悬赏:70  手机版
解决时间 2021-05-07 12:48
  • 提问者网友:棒棒糖
  • 2021-05-07 05:43

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WindowsApplication1
{
public class Employee:IComputeProfit
{
string name;
Dictionary<string, Customer> dic = new Dictionary<string, Customer>();

public string Name
{
get { return name; }
set { name = value; }
}

public bool AddCustomer(Customer c)
{
dic.Add(c.Name, c);
return true;
}

public void Print()
{
try
{
FileStream fs = new FileStream(string.Format("{0}.txt", this.Name), FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
writer.WriteLine("{0}的客户列表,客户的个数为:{1}", this.Name, dic.Count);
writer.WriteLine("-------------------------------------------------------");
foreach (Customer c in dic.Values)
{
writer.WriteLine("客户姓名:{0}", c.Name);
writer.WriteLine("客户类型:{0}", c.Leval);
}
writer.Close();
fs.Close();
}
catch
{

}
}

public Customer GetCustomerByName(string customerName)
{
if (dic.ContainsKey(customerName))
return dic[customerName];
else
return null;
}


#region IComputeProfit 成员

public decimal ComputeProfit()
{
decimal d = 115;
d = d + 100 * dic.Count;
return d;
}

#endregion
}
}

---------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
public enum CustomerType
{
黄金,白金,钻石
}

public class Customer:IComputeProfit
{
string name;
CustomerType leval;

public CustomerType Leval
{
get { return leval; }
set { leval = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}


#region IComputeProfit 成员

public decimal ComputeProfit()
{
switch (this.Leval)
{
case CustomerType.黄金:
return 125.35;
break;
case CustomerType.白金:
return 175;
break;
case CustomerType.钻石:
return 250;
break;
}
}

#endregion
}
}

----------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
public static class Manager
{
static Dictionary<string, Employee> dic = new

Dictionary<string, Employee>();

public static Dictionary<string, Employee> Dic
{
get { return Manager.dic; }
}

public static bool AddEmployee(Employee e)
{
dic.Add(e.Name, e);
return true;
}

public static bool Service(string employeeName,Customer c)
{
if (dic.ContainsKey(employeeName))
{
dic[employeeName].AddCustomer(c);
return true;
}
else
return false;

}

public static Employee GetEmployeeByName(string employeeName)
{
if (dic.ContainsKey(customerName))
return dic[customerName];
else
return null;
}
}
}

我现在就是不懂到底这个键值对的搜索问题. 尤其是这个employeeName这个键 根本就是没声明过 但是ContainsKey又是如何去查询、? 可能我脑子有点迷糊。 希望有个明白人 帮我详细讲讲泛型集合问题 3Q

最佳答案
  • 五星知识达人网友:一秋
  • 2021-05-07 06:28
employeeName 这个键 是在方法参数里边声明的。
public static bool Service(string employeeName ,Customer c)

在这里声明的。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯