C#接口-对接口理解实例代码
using System;
public interface Iren
{
void eat();
void sleep();
}
public class Xuesheng: Iren
{
public void eat()
{
Console.WriteLine("我是一个学生,我去食堂吃饭");
}
public void sleep()
{
Console.WriteLine("我是一个学生,我回寝室睡觉");
}
}
public class Laoshi: Iren
{
public void eat()
{
Console.WriteLine("我是一个老师,我回家吃饭");
}
public void sleep()
{
Console.WriteLine("我是一个老师,我回家睡觉");
}
}
public class Jiazhang: Iren
{
public void eat()
{
Console.WriteLine("我是一个家长,我下饭馆吃饭");
}
public void sleep()
{
Console.WriteLine("我是一个家长,我去旅馆睡觉");
}
}
public class Schcool
{
public void kaifan(Iren ren)
{
ren.eat();
}
public void fangxue(Iren ren)
{
ren.sleep();
}
}
public class Program
{
static void Main()
{
Schcool sc = new Schcool();
Iren ix = new Xuesheng();
sc.kaifan(ix);
sc.fangxue(ix);
Iren il = new Laoshi();
sc.kaifan(il);
sc.fangxue(il);
Iren ij = new Jiazhang();
sc.kaifan(ij);
sc.fangxue(ij);
}
}