文章出處

抽象方法在抽象類中。抽象類中可以有非抽象成員,子類可以來使用。

抽象類中,申明的構造函數,也可以讓子類通過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();
            }
        }
    }
}

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()