using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Interfaces
{
class Program
{
static void Main(string[] args)
{
Tran t1 = new Tran("001", "8/10/2015", 198.00);
t1.ShowTrans();
Console.ReadKey();
}
}
//声明一个接口实例
public interface Transe
{
//接口成员
void ShowTrans();//输出方法
double getAmout();//返回小数类型
}
//定义一个类继承接口
public class Tran : Transe
{
//定义变量
private string Tcode;
private string Date;
private double Amout;
//赋值
public Tran()
{
Tcode = "";
Date = " ";
Amout = 0.0;
}
public Tran(string c,string d,double a)
{
Tcode = c;
Date = d;
Amout = a;
}
public double getAmout()
{
return Amout;
}
//输出
public void ShowTrans()
{
Console.WriteLine("Tran:{0}", Tcode);
Console.WriteLine("Date:{0}", Date);
Console.WriteLine("Amout:{0}", getAmout());
}
//注意事项
//1.接口不能用public abstract 修饰.
//2.接口内不能有字段变量,构造函数;
//3.可以定义属性;
//4.实现与接口定义一致;
//5.实现接口,必须实现接口的所有方法;
}
}
泛型接口案例:
//定义接口 U:派生类
public interface IComparable<T,U> where T : class
{
void a1(T t);
void a2(U u);
}
//实体类
public class C1
{
public int Cen { get; set; }
}
public class D2
{
public int Den { get; set; }
}
//实现泛型接口方法
public class S1 : IComparable<C1,D2>
{
public void a1(C1 t)
{
Console.WriteLine(t.Cen);
}
public void a2(D2 u)
{
Console.WriteLine(u.Den);
}
}
D2 d = new D2();
C1 c = new C1();
d.Den = 1;
c.Cen = 5;
//实例化接口
IComparable<C1, D2> cc = new S1();
//调用
cc.a1(c);
cc.a2(d);
结果: