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();
}
}
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();
}
}
相关文章推荐
- 全面剖析C#接口编程之实现接口
- 支付宝Payto接口的c#.net实现
- [转] 支付宝Payto接口的c#.net实现
- C#使用接口实现回调
- 用C#接口实现通用的文本数据序列化(ZT)
- VB.net和c#利用IOleCommandTarget接口实现Html编辑器的Glyphs(原创)
- C#中显/隐式实现接口及其访问方法
- 支付宝Payto接口的c#.net实现
- 支付宝Payto接口的c#.net实现
- 全面剖析C#接口编程之实现接口 (2)
- C#中CollectionBase类中IList接口的实现
- matlab与c#接口的实现
- C#中的显式接口成员实现(explicit interface member implementation)
- 支付宝Payto接口的c#.net实现
- URL重写实现IHttpHandler接口(Asp.Net C#)
- 支付宝Payto接口的c#.net实现
- C#中实现接口的几种方法
- C#中CollectionBase类中IList接口的实现
- 支付宝Payto接口的c#.net实现
- C#2.0 接口的实现与使用