博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

7作业布置.avi---接口作业

Posted on 2010-10-16 00:33  EVON168  阅读(169)  评论(0)    收藏  举报
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);
        }
    }
}