抽象方法实现多态

抽象方法在抽象类中。抽象类中可以有非抽象成员,子类可以来使用。

抽象类中,申明的构造函数,也可以让子类通过base来调用。

抽象类只能是父类,但是没有规定抽象类必须要有子类

抽象方法既然是抽象的,子类必须实现。子类不想实现父类的方法,在子类中,就将方法设置为abstract,要重写父类方法,必须加override

子类不一定要去实现父类的抽象成员,但是必须必须重写方法,同时将方法的定义为abstract

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

namespace 多态的练习
{
    abstract class Animal
    {
        /// <summary>
        /// 抽象方法,不提供默认实现
        /// </summary>
        public abstract void GetFood();
        
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态的练习
{
    abstract class Dog:Animal
    {
        /// <summary>
        /// 子类可以不用实现父类的抽象方法,但是要带上Abstract,类名前也要加上这个修饰符
        /// </summary>
        public abstract override void GetFood();
      
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态的练习
{
    class LaoYing:Animal
    {
        public override void GetFood()
        {
            Console.WriteLine("老鹰靠俯冲捕食。");
            //base.GetFood();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态的练习
{
    class Snack:Animal
    {
        public override void GetFood()
        {
            Console.WriteLine("蛇靠偷袭捕食");
            //base.GetFood();  //虚方法提供了默认实现,就是调用父类的方法
        }
    }
}

测试:

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

namespace 多态的练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //实现多态的步骤
            //1.先写好父类,和可以被重写的方法
            //2.写好子类,重写父类的方法
            //3.声明父类变量,实例化子类对象

            Animal[] ans = new Animal[2];
            ans[0] = new Snack();
            ans[1] = new LaoYing();

            foreach (var item in ans)
            {
                item.GetFood();
            }
            Console.ReadKey();
        }
    }
}

属性也可以实现抽象的定义:

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

namespace 多态的练习
{
    abstract class Animal
    {
        /// <summary>
        /// 抽象方法,不提供默认实现
        /// </summary>
        public abstract void GetFood();

        /// <summary>
        /// 属性也可以实现抽象的定义
        /// </summary>
        public abstract int Age { get; set; }
        
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态的练习
{
    class LaoYing:Animal
    {
        public override void GetFood()
        {
            Console.WriteLine("老鹰靠俯冲捕食。");
            //base.GetFood();
        }

        public override int Age
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/caofangsheng/p/4518625.html