zoukankan      html  css  js  c++  java
  • 接口和抽象类


    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 ");
            }



        }
       


  • 相关阅读:
    HTTP协议中常用相应的状态码总结
    mysql 用户管理
    史上最全的mysql聚合函数总结(与分组一起使用)
    jQuery+masonry实现瀑布流
    MySQL Workbench 导入导出乱码解决方法
    在Google Maps 上点击标签显示说明并保持不消失
    在Google Maps 上点击标签后显示说明
    如何在Google Maps 添加多个标记
    如何在 Google 地图中添加标记和说明
    Google Maps API3 之 Hello World
  • 原文地址:https://www.cnblogs.com/duwamish/p/1513391.html
Copyright © 2011-2022 走看看