C#抽象类、ASP.NET抽象方法、虚拟方法和接口的综合应用

作者(出处):百宝箱工作室发布时间:2014年12月7日 12:47:29查阅量:883

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

//综合应用抽象类、抽象方法、虚拟方法和接口
namespace T8
{
    /// <summary>
    /// IDriving接口
    /// </summary>
    public interface IDriving
    {
        /// <summary>
        /// IDriving接口中的Driving()方法
        /// </summary>
        void Driving();
    }
    /// <summary>
    /// Animal抽象类
    /// </summary>
    abstract class Animal
    {
        /// <summary>
        /// Animal抽象类中的Breath()抽象方法
        /// </summary>
        public abstract void Breath();
        public void Sleep()
        {
            Console.WriteLine("Animal Sleep.");
        }
    }
    /// <summary>
    /// Person类继承Animal抽象类
    /// </summary>
    class Person : Animal
    {
        /// <summary>
        /// Person类重写基类的Breath()方法
        /// </summary>
        public override void Breath()
        {
            Console.WriteLine("Person Breath.");
        }
        /// <summary>
        /// Person类中的Walk()虚拟方法
        /// </summary>
        public virtual void Walk()
        {
            Console.WriteLine("Person Walk.");
        }
        /// <summary>
        /// Person类中自定义的Eating()方法
        /// </summary>
        public void Eating()
        {
            Console.WriteLine("Person Eating.");
        }
    }
    /// <summary>
    /// BlackMan类继承Person类,并实现IDriving接口
    /// </summary>
    class BlackMan : Person, IDriving
    {
        /// <summary>
        /// BlackMan类重写基类的Breath()方法
        /// </summary>
        public override void Breath()
        {
            //base.Breath();
            Console.WriteLine("BlackMan Breath.");
        }
        /// <summary>
        /// BlackMan类实现IDriving接口中的Driving()方法
        /// </summary>
        public void Driving()
        {
            Console.WriteLine("BlackMan Driving.");
        }
        /// <summary>
        /// BlackMan类中自定义的Walk()方法
        /// </summary>
        public void Walk()
        {
            Console.WriteLine("BlackMan Walk.");
        }
        /// <summary>
        /// 使用new关键字隐藏基类的Eating()方法
        /// </summary>
        new public void Eating()
        {
            Console.WriteLine("BlackMan Eating.");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.Breath();
            p.Eating();
            p.Sleep();
            p.Walk();

            //使用Animal来实例化BlackMan,实例化对象只能调用Animal的成员
            Animal a = new BlackMan();
            a.Breath();
            a.Sleep();

            BlackMan b = new BlackMan();
            b.Breath();
            b.Driving();
            b.Eating();            
            b.Sleep();
            b.Walk();

            Console.Read();
        }
    }
}
百宝箱网络工作室  版权所有  
站长QQ:1617818397  站长E-mail:1617818397@qq.com
Powered by www.baibaox.com  Copyright © 2011-2018
点击这里给我发消息