using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceTask
{
public interface IFlyable
{
void TakeOff();//起飞
void TouchDown();//降落
void Flight();//飞行
}
public interface IJumper
{
void JumpStart();
void TouchDown();
}
public class SuperMan:IFlyable,IJumper
{
public void TakeOff()
{
Console.WriteLine("超人起飞");
}
void IFlyable.TouchDown()
{
Console.WriteLine("超人飞行降落");
}
public void Flight()
{
Console.WriteLine("超人飞行");
}
public void JumpStart()
{
Console.WriteLine("超人起跳");
}
void IJumper.TouchDown()
{
Console.WriteLine("超人跳远降落");
}
}
public class Plane:IFlyable
{
public void TakeOff()
{
Console.WriteLine("飞机起飞");
}
public void TouchDown()
{
Console.WriteLine("飞机降落");
}
public void Flight()
{
Console.WriteLine("飞机飞行");
}
}
public abstract class Bird : IFlyable
{
public abstract void TakeOff();
public abstract void TouchDown();
public abstract void Flight();
}
public class Sparrow : Bird//麻雀
{
public override void TakeOff()
{
Console.WriteLine("麻雀起飞");
}
public override void TouchDown()
{
Console.WriteLine("麻雀降落");
}
public override void Flight()
{
Console.WriteLine("麻雀飞行");
}
}
public class Swan : Bird//天鹅
{
public override void TakeOff()
{
Console.WriteLine("天鹅起飞");
}
public override void TouchDown()
{
Console.WriteLine("天鹅降落");
}
public override void Flight()
{
Console.WriteLine("天鹅飞行");
}
}
public static class Test
{
public static void TestFly(object obj)
{
IFlyable fly = null;
if (obj is IFlyable)
{
fly = obj as IFlyable;
fly.Flight();
}
else
{
Console.WriteLine("类型不匹配");
}
}
public static void TestRun(object obj)
{
IJumper jump = null;
if (obj is IJumper)
{
jump = obj as IJumper;
jump.TouchDown();
}
else
{
Console.WriteLine("类型不匹配");
}
}
}
class Program
{
static void Main(string[] args)
{
SuperMan sup = new SuperMan();
Plane plane = new Plane();
Sparrow sparrow = new Sparrow();
Swan swan = new Swan();
Test.TestFly(sup);
Test.TestFly(plane);
Test.TestFly(sparrow);
Test.TestFly(swan);
Test.TestRun(sup);
Test.TestRun(plane);
Test.TestRun(sparrow);
Test.TestRun(swan);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceTask
{
public interface IFlyable
{
void TakeOff();//起飞
void TouchDown();//降落
void Flight();//飞行
}
public interface IJumper
{
void JumpStart();
void TouchDown();
}
public class SuperMan:IFlyable,IJumper
{
public void TakeOff()
{
Console.WriteLine("超人起飞");
}
void IFlyable.TouchDown()
{
Console.WriteLine("超人飞行降落");
}
public void Flight()
{
Console.WriteLine("超人飞行");
}
public void JumpStart()
{
Console.WriteLine("超人起跳");
}
void IJumper.TouchDown()
{
Console.WriteLine("超人跳远降落");
}
}
public class Plane:IFlyable
{
public void TakeOff()
{
Console.WriteLine("飞机起飞");
}
public void TouchDown()
{
Console.WriteLine("飞机降落");
}
public void Flight()
{
Console.WriteLine("飞机飞行");
}
}
public abstract class Bird : IFlyable
{
public abstract void TakeOff();
public abstract void TouchDown();
public abstract void Flight();
}
public class Sparrow : Bird//麻雀
{
public override void TakeOff()
{
Console.WriteLine("麻雀起飞");
}
public override void TouchDown()
{
Console.WriteLine("麻雀降落");
}
public override void Flight()
{
Console.WriteLine("麻雀飞行");
}
}
public class Swan : Bird//天鹅
{
public override void TakeOff()
{
Console.WriteLine("天鹅起飞");
}
public override void TouchDown()
{
Console.WriteLine("天鹅降落");
}
public override void Flight()
{
Console.WriteLine("天鹅飞行");
}
}
public static class Test
{
public static void TestFly(object obj)
{
IFlyable fly = null;
if (obj is IFlyable)
{
fly = obj as IFlyable;
fly.Flight();
}
else
{
Console.WriteLine("类型不匹配");
}
}
public static void TestRun(object obj)
{
IJumper jump = null;
if (obj is IJumper)
{
jump = obj as IJumper;
jump.TouchDown();
}
else
{
Console.WriteLine("类型不匹配");
}
}
}
class Program
{
static void Main(string[] args)
{
SuperMan sup = new SuperMan();
Plane plane = new Plane();
Sparrow sparrow = new Sparrow();
Swan swan = new Swan();
Test.TestFly(sup);
Test.TestFly(plane);
Test.TestFly(sparrow);
Test.TestFly(swan);
Test.TestRun(sup);
Test.TestRun(plane);
Test.TestRun(sparrow);
Test.TestRun(swan);
}
}
}
浙公网安备 33010602011771号