接口和抽象类


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using Arrayfds;
using System.Reflection;

namespace TestArray
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Animal duck 
= new Duck("Duck");
            duck.MakeVoice();
            duck.Show();


            Animal dog 
= new Dog("Dog");
            dog.MakeVoice();
            dog.Show();


            IAction dogAction 
= new Dog("A big dog");
            dogAction.Move();


            Console.ReadLine();
        }

    }




    
abstract public class Animal
    {
        
protected string _name;

        
public abstract string Name
        {
            
get;
        }

        
//声明抽象方法
        public abstract void Show();


        
public void MakeVoice()
        {
            Console.WriteLine(
"all animals can make voice");
        }

    }

    
public interface IAction
    {
        
//定义公共方法标签
        void Move();
    }


    
public class Duck : Animal, IAction
    {
        
public Duck(string name)
        {
            _name 
= name;
        }

        
//重载抽象方法
        public override void Show()
        {
            Console.WriteLine(_name 
+ " is showing for you.");
        }

        
//重载抽象属性
        public override string Name
        {
            
get { return _name; }
        }

        
//实现接口方法
        public void Move()
        {
            Console.WriteLine(
"Duck also can swim.");
        }

    }



    
public class Dog : Animal, IAction
    {
        
public Dog(string name)
        {
            
this._name = name;
        }

        
public override void Show()
        {

            Console.WriteLine(
this._name + " is showing for you.");
        }

        
public override string Name
        {
            
get { return _name; }
        }


        
public void Move()
        {
            Console.WriteLine(_name 
+ "also can run ");
        }



    }
   


posted @ 2009-06-29 17:09  paymob  阅读(138)  评论(0编辑  收藏  举报