using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
interface IRunable
{
float Distance
{
get;
}
float Speed
{
get;
set;
}
void Run( );
}
interface IShoutable
{
void Shout( );
}
class Cow : IRunable, IShoutable
{
private float _Distance = 0.0f;
//实现IRunable.Distance属性
public float Distance
{
get
{
return this._Distance;
}
}
private float _Speed;
//实现IRunable.Speed属性
public float Speed
{
get
{
return this._Speed;
}
set
{
this._Speed = value;
}
}
//实现IRunable.Run()方法
public void Run()
{
System.Console.WriteLine("一头牛正以 {0} 米每秒的速度奔跑", this._Speed);
this._Distance += this._Speed;
}
//实现IShoutable.Shout()方法
public void Shout( )
{
System.Console.WriteLine("一头牛正在嚎叫......");
}
}
class Program
{
static void Main(string[] args)
{
//定义IRunable引用,并初始化为一个Cow对象
IRunable run = new Cow( );
//直接使用IRunable,它实际上是Cow
run.Speed = 10;
run.Run( );
System.Console.WriteLine("1-总路程:{0}", run.Distance);
run.Speed = 20;
run.Run( );
System.Console.WriteLine("2-总路程:{0}", run.Distance);
MultInterface( );
}
static void MultInterface()
{
Cow aCow = new Cow( );
IRunable run = aCow;
IShoutable shout = aCow;
run.Speed = 2.6f;
run.Run( );
shout.Shout( );
aCow.Speed = 9.5f;
aCow.Run( );
aCow.Shout( );
}
}
}
不大明白这个接口的作用,不过还是留着吧,加油!