小姐好白在线观看,迷你忍者没有声音,财经郎眼1117
从简单的例子理解泛型
话说有家影视公司选拔偶像派男主角,导演说了,男演员,身高是王道。于是有下面代码:
public boy(string name, int height) {
this.mname = name;
this.mheight = height;
}
}
//演员选拔类
public class compare
{
//导演导超女出生,喜欢一对一pk
public boy whoisbetter(boy boy1, boy boy2)
{
if (boy1.height > boy2.height)
{
return boy1;
}
else
{
return boy2;
}
}
}
//测试
static void main(string[] args)
{
boy boy1 = new boy("潘长江", 165);
boy boy2 = new boy("刘德华", 175);
console.writeline(new compare().whoisbetter(boy1, boy2).name);
console.readline();
}
public girl(string name, int weight){
this.mname = name;
this.mweight = weight;
}
}
//演员选拔类中添加一个女演员方法
public class compare
{
//男演员身高是王道
public boy whoisbetter(boy boy1, boy boy2)
{
if (boy1.height > boy2.height)
{
return boy1;
}
else
{
return boy2;
}
}
//女演员苗条是王道
public girl whoisbetter(girl girl1, girl girl2)
{
if (girl1.weight < girl2.weight)
{
return girl1;
}
else
{
return girl2;
}
}
}
//测试
static void main(string[] args)
{
boy boy1 = new boy("潘长江", 165);
boy boy2 = new boy("刘德华", 175);
girl girl1 = new girl("巩俐", 120);
girl girl2 = new girl("周迅", 80);
console.writeline(new compare().whoisbetter(boy1, boy2).name);
console.writeline(new compare().whoisbetter(girl1, girl2).name);
console.readline();
}
public boy(string name, int height) {
this.mname = name;
this.mheight = height;
}
public int compareto(object obj)
{
//比较身高
return this.mheight - ((boy)obj).height;
}
}
/// <summary>
/// 女演员:实现icomparable接口
/// </summary>
public class girl : icomparable
{
//姓名
private string mname;
//体重 www.jb51.net
private int mweight;
public string name
{
get { return this.mname; }
}
public int weight
{
get { return this.mweight; }
}
public girl(string name, int weight){
this.mname = name;
this.mweight = weight;
}
public int compareto(object obj)
{
//比较体重
return ((girl)obj).weight - this.mweight;
}
}
girl girl1 = new girl("巩俐", 120);
girl girl2 = new girl("周迅", 80);
console.writeline(((boy)new compare().whoisbetter(boy1, boy2)).name);
console.writeline(((girl)new compare().whoisbetter(girl1, girl2)).name);
console.writeline(new compare().whoisbetter(boy1.height, boy2.height));
console.writeline(new compare().whoisbetter(girl1.weight, girl2.weight));
console.readline();
}
girl girl1 = new girl("巩俐", 120);
girl girl2 = new girl("周迅", 80);
console.writeline(((boy)new compare().whoisbetter(boy1, girl1)).name);
console.readline();
}
girl girl1 = new girl("巩俐", 120);
girl girl2 = new girl("周迅", 80);
console.writeline((new compare<boy>().whoisbetter(boy1, boy2)).name);
console.writeline((new compare<girl>().whoisbetter(girl1, girl2)).name);
console.writeline(new compare<int>().whoisbetter(boy1.height, boy2.height));
console.writeline(new compare<string>().whoisbetter(boy1.name, girl1.name));
console.readline();
}
约束 说明
t:结构 类型参数必须是值类型。可以指定除nullable 以外的任何值类型。
t:类 类型参数必须是引用类型;这一点也适用于任何类、接口、委托或数组类型。
t:new() 类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。
t:<基类名> 类型参数必须是指定的基类或派生自指定的基类。
t:<接口名称> 类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。
t:u 为t 提供的类型参数必须是为u 提供的参数或派生自为u 提供的参数。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论