.NET 泛型的特殊使用
来源:互联网 发布:qq群淘宝客优惠劵软件 编辑:程序博客网 时间:2024/04/30 01:54
定义泛型的后面加where子句,可以要求,该泛型定义的参数必须满足一定条件:如实现了一定接口,继承自某一基类,具有一个无参构造函数等.
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericWhereDemo
...{
class Program
...{
static void Main(string[] args)
...{
ShapeList<Rectangle> rect = new ShapeList<Rectangle>();
rect.ShowArea(new Rectangle(3, 6));
ShapeList<Triangle> tri = new ShapeList<Triangle>();
tri.ShowArea(new Triangle(3, 6));
ShapeList<SolidLine> line = new ShapeList<SolidLine>();
line.ShowArea(new SolidLine(5));
Console.ReadLine();
}
}
class ShapeList<T> where T : IShape
...{
public void ShowArea(T item)
...{
Console.WriteLine("Area is {0}", item.Area());
}
}
interface IShape
...{
double Area();
}
class Rectangle : IShape
...{
public double length;
public double width;
public Rectangle(double length, double width)
...{
this.length = length;
this.width = width;
}
public double Area()
...{
return this.length * this.width;
}
}
class Triangle : IShape
...{
public double length;
public double height;
public Triangle(double length, double height)
...{
this.length = length;
this.height = height;
}
public double Area()
...{
return this.length * this.height / 2;
}
}
class SolidLine
...{
public double length;
public SolidLine(double length)
...{
this.length = length;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace GenericWhereDemo
...{
class Program
...{
static void Main(string[] args)
...{
ShapeList<Rectangle> rect = new ShapeList<Rectangle>();
rect.ShowArea(new Rectangle(3, 6));
ShapeList<Triangle> tri = new ShapeList<Triangle>();
tri.ShowArea(new Triangle(3, 6));
ShapeList<SolidLine> line = new ShapeList<SolidLine>();
line.ShowArea(new SolidLine(5));
Console.ReadLine();
}
}
class ShapeList<T> where T : IShape
...{
public void ShowArea(T item)
...{
Console.WriteLine("Area is {0}", item.Area());
}
}
interface IShape
...{
double Area();
}
class Rectangle : IShape
...{
public double length;
public double width;
public Rectangle(double length, double width)
...{
this.length = length;
this.width = width;
}
public double Area()
...{
return this.length * this.width;
}
}
class Triangle : IShape
...{
public double length;
public double height;
public Triangle(double length, double height)
...{
this.length = length;
this.height = height;
}
public double Area()
...{
return this.length * this.height / 2;
}
}
class SolidLine
...{
public double length;
public SolidLine(double length)
...{
this.length = length;
}
}
}
- .NET 泛型的特殊使用
- Xib的特殊使用
- if的特殊使用
- CycloneII特殊管脚的使用
- 特殊引用$this的使用
- 使用spring的特殊bean
- 使用Spring的特殊bean
- img src的特殊使用
- UILabel特殊属性的使用
- UIlabel特殊属性的使用
- npm的一些特殊使用
- 使用spring的特殊bean
- .net(c#)中ref的特殊用法
- linux下练习 c++ 特殊容器、特殊函数的使用
- 特殊的引用”$this“的使用
- 特殊的引用“$this”的使用
- ASP.NET:泛型的使用
- Shell变量名使用的特殊字符
- 初学Ajax
- 看《夜宴》
- 用Python来烹调大餐
- .NET中的数据结构
- jfreechart生成Multiple Pie Chart
- .NET 泛型的特殊使用
- nullable generic structure
- CAPXOUS AutoComplete 1.2.5 自动完成 AJAX 破解版 注册版
- 关于参加考试的一点想法以及我的系分经历
- 婚礼上,新郎的一段精彩表白
- .net 服务器端生成alert/confim 消息
- gc的用法
- 从javascript语言本身谈项目实战
- 最后的祈祷