抽象方法实现多态

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

抽象类中,申明的构造函数,也可以让子类通过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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值