1、定义一个类Employee ,就有姓名(name)、工资(wage)属性,并定义相应的属性操作方法和构造方法
2、在main方法中,定义List<T>类型的变量list,用于保存Employee类型的对象,注意要求使用泛型知识来定义list,依次放入4个Employee对象,并通过循环遍历list中的所有对象,计算工资和,并输出到屏幕上。
3、自定义一个泛型类Test,具有2个泛型参数T、K,并定义2个属性分别为T、K类型的变量t、k,编写出对应的操作属性的方法和构造方法,并在main函数中声称一个Test的实例,将两个属性分别赋值为字符串“Lisi@mjrg.net”和Employee的对象e1,e1的name是“李四”,e1的wage是2000,最后向屏幕输出Test实例的2个属性值。
C#泛型的题目,高手来啊
答案:2 悬赏:30 手机版
解决时间 2021-03-19 22:01
- 提问者网友:世勋超人
- 2021-03-18 22:22
最佳答案
- 五星知识达人网友:末日狂欢
- 2021-03-18 22:45
1
public class Employee
{
public string name { get; set; }
public float wage { get; set; }
}2,
var list = new List<Employee>();
list.Add(new Employee() { name = "A1", wage = 1000 });
list.Add(new Employee() { name = "A2", wage = 1000 });
list.Add(new Employee() { name = "A3", wage = 1000 });
list.Add(new Employee() { name = "A4", wage = 1000 });
float g = 0;
list.ForEach(x =>
{
g += x.wage;
});
Console.Write("工资总和:" + g);3
class Test<T, K>
{
public T t { get; set; }
public K k { get; set; }
public Test(T t1, K k1)
{
this.t = t1;
this.k = k1;
}
}
Test<string, Employee> bb = new Test<string, Employee>("
Lisi@mjrg.net
", new Employee() { wage = 1000, name = "李四" });
Console.Write(bb.t);
Console.Write(bb.k.name);
Console.Write(bb.k.wage);
public class Employee
{
public string name { get; set; }
public float wage { get; set; }
}2,
var list = new List<Employee>();
list.Add(new Employee() { name = "A1", wage = 1000 });
list.Add(new Employee() { name = "A2", wage = 1000 });
list.Add(new Employee() { name = "A3", wage = 1000 });
list.Add(new Employee() { name = "A4", wage = 1000 });
float g = 0;
list.ForEach(x =>
{
g += x.wage;
});
Console.Write("工资总和:" + g);3
class Test<T, K>
{
public T t { get; set; }
public K k { get; set; }
public Test(T t1, K k1)
{
this.t = t1;
this.k = k1;
}
}
Test<string, Employee> bb = new Test<string, Employee>("
Lisi@mjrg.net
", new Employee() { wage = 1000, name = "李四" });
Console.Write(bb.t);
Console.Write(bb.k.name);
Console.Write(bb.k.wage);
全部回答
- 1楼网友:夜风逐马
- 2021-03-18 23:11
t是泛类型,node是泛型类,在实际调用过程中t将被指定为一个确切的类型,如:string、int、datetime甚至是一个确定的class等等。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯