您的位置:首页 > 编程语言 > C#

C#接口的实现

2015-01-18 17:54 183 查看
using System;

interface Runner

{

void run();

}

interface Swimmer

{

void swim();

}

abstract class Animal

{

abstract public void eat();

}

class Person : Animal , Runner, Swimmer

{

public void run()

{

Console.WriteLine("run");

}

public void swim()

{

Console.WriteLine("swim");

}

public override void eat()

{

Console.WriteLine("eat");

}

public void speak()

{

Console.WriteLine("speak");

}

}

class TestInterface

{

static void m1(Runner r)

{

r.run();

}

static void m2(Swimmer s)

{

s.swim();

}

static void m3(Animal a)

{

a.eat();

}

static void m4(Person p)

{

p.speak();

}

public static void Main(string [] args)

{

Person p = new Person();

m1(p);

m2(p);

m3(p);

m4(p);

Runner a = new Person();

a.run();

}

}

using System;

class InterfaceExplicitImpl

{

static void Main()

{

FileViewer f = new FileViewer();

f.Test();

( (IWindow) f ).Close();

IWindow w = new FileViewer();

w.Close();

}

}

interface IWindow

{

void Close();

}

interface IFileHandler

{

void Close();

}

class FileViewer : IWindow, IFileHandler

{

void IWindow.Close ()

{

Console.WriteLine( "Window Closed" );

}

void IFileHandler.Close()

{

Console.WriteLine( "File Closed" );

}

public void Test()

{

( (IWindow) this ).Close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: