码迷,mamicode.com
首页 > 其他好文 > 详细

封装,多态和继承的例子

时间:2016-09-02 23:26:53      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

一,父类Animal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace jichengchonggou
{
    public class Animal
    {
        //获得声音的方法
        public virtual string GetShoutSound()
        {
            return "";
        }
    }
}

二,子类Cat和Dog

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace jichengchonggou
{
    public class Dog : Animal
    {
        public override string GetShoutSound()
        {
            return "旺旺旺旺!!!!!";
        }
    }
}

子类二

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace jichengchonggou
{
    public class Cat : Animal
    {
        public override string GetShoutSound()
        {
            return "喵喵喵喵!!!!!";
        }
    }

}

三,输出:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace jichengchonggou
{
    internal class Program
    {
        private static void Main(string[] args) //主函数 
        {
            Animal animal1 = new Dog();
            Animal animal2 = new Cat();
            string a1 = animal1.GetShoutSound();
            string a2 = animal2.GetShoutSound();
            Console.WriteLine(a1 + a2);
        }
    }
}

 

封装,多态和继承的例子

标签:

原文地址:http://www.cnblogs.com/May-day/p/5835783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!