接口,三层中常用的...
2007-03-09 22:30
309 查看
using System;
namespace LearningCSharp
{
//定义接口
interface ICarnivore
{
bool IsHungry
{
get;
}
Animal Hunt();
void Eat(Animal victim);
}
public abstract class Animal
{
public abstract void Sleep();
}
public class Antelope:Animal
{
public override void Sleep()
{
}
}
public class Lion:Animal,Icarnivore
{
public Lion()
{
hungry=true;
}
private bool hungry;
public bool IsHungry
{
get
{
return hungry;
}
}
public Animal Hunt()
{
return new Antelope();
}
public void Eat(Animal prey)
{
Console.WriteLine("Lion is no longer hungry");
}
public override void Sleep()
{
}
public void JoinPride()
{
}
}
class Tester
{
static void Main(string[] args)
{
Lion aLion=new Lion();
Antelope a=new Antelope();
if(aLion.IsHungry)
{
Animal victim=aLion.Hunt();
if(victim!=null)
{
aLion.Eat(victim);
}
}
aLion.JoinPride();
aLion.Sleep();
}
}
}
is 判断是不是实现了接口
as 强制转换并检查是不是有实现..
namespace LearningCSharp
{
//定义接口
interface ICarnivore
{
bool IsHungry
{
get;
}
Animal Hunt();
void Eat(Animal victim);
}
public abstract class Animal
{
public abstract void Sleep();
}
public class Antelope:Animal
{
public override void Sleep()
{
}
}
public class Lion:Animal,Icarnivore
{
public Lion()
{
hungry=true;
}
private bool hungry;
public bool IsHungry
{
get
{
return hungry;
}
}
public Animal Hunt()
{
return new Antelope();
}
public void Eat(Animal prey)
{
Console.WriteLine("Lion is no longer hungry");
}
public override void Sleep()
{
}
public void JoinPride()
{
}
}
class Tester
{
static void Main(string[] args)
{
Lion aLion=new Lion();
Antelope a=new Antelope();
if(aLion.IsHungry)
{
Animal victim=aLion.Hunt();
if(victim!=null)
{
aLion.Eat(victim);
}
}
aLion.JoinPride();
aLion.Sleep();
}
}
}
is 判断是不是实现了接口
as 强制转换并检查是不是有实现..
相关文章推荐
- 微信api接口常用操作
- 转---常用数据库访问接口介绍(一)
- Java 8中一些常用的全新的函数式接口
- java常用集合及相关接口
- UI常用接口使用规范
- 生活常用功能数据接口API
- arcgis for javascriptAPI常用接口
- 常用电子面单接口对接技术文档
- Spring8:一些常用的Spring Bean扩展接口
- JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程.]
- 常用的webservice接口
- openblas第一弹:openblas 使用说明和常用接口介绍
- HttpServletResponse接口的常用方法
- Elasticsearch基础教程之常用命令(接口)
- 几款常用的在线API管理工具(是时候抛弃office编写接口文档了)
- JUnit中常用的接口和类
- linux网络编程系列-TCP及常用接口分析
- 各种常用的JSON接口,开动你的大脑你就可以做出各种应用,值得收藏
- 几种常用的接口协议的积累,欢迎补充
- 常用的webservice接口