C#Protected和多態(虛方法)


Protected 在基類中定義后,能被派生類調用,但是不能被其他類調用。

virtual 在基類中定義后,在派生類中能被重寫。

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

namespace 繼承
{
    class Vertebrate
    {
        protected string spine;//受保護的字段
        private double weigth;
        private double temperature;
        public double Weigth
        {
            set
            {
                if (value < 0)
                {
                    weigth = 0;
                }
                else
                {
                    weigth = value;
                }
            }
            get { return weigth; }
        }
        public double Temperature
        {
            set
            {
                if (value < 0)
                {
                    temperature = 0;
                }
                else
                {
                    temperature = value;
                }
            }
            get { return temperature; }
        }
        public Vertebrate()
        {
            spine = "脊柱";
            weigth = 0;
            temperature = 0;
        }
        public virtual  void Breate() //虛方法
        {
            Console.WriteLine("呼吸");
        }
        public void Sleep()
        {
            Console.WriteLine("睡覺");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace 繼承
{
    class Program
    {
        static void Main(string[] args)
        {
            Vertebrate vertebrate = new Vertebrate();
            vertebrate.spine1 = "脊柱"; //public的就能被直接調用
            Mammal mammal = new Mammal();
            Fish fish = new Fish();
            Animal animal = new Animal();
            Console.WriteLine("我是一只哺乳動物");
            mammal.Scukle();
            animal.Breate();
            mammal.Breate();
            fish.Breate();
            mammal.Sleep();
            mammal.Message();
        }
    }
    class Fish : Vertebrate
    {
        public override void Breate()//重寫基類中的虛方法
        {
            Console.WriteLine("用鰓呼吸");
        }
    }
    class Animal : Vertebrate
    {

    }
    class Mammal : Vertebrate//派生類:基類
    {
        private string arms;
        private string legs;
        private int age;
        public int Age
        {
            set { age = value; }
            get { return age; }
        }
        public Mammal()
        {
            arms = "前肢";
            legs = "后肢";
            Age = 0;
            Weigth = 10;
            Temperature = 37;
        }
        public void Scukle()
        {
            Console.WriteLine("哺乳");
        }
        public override void Breate()
        {
            Console.WriteLine("用肺呼吸");
        }
        public void Message()
        {
            Console.WriteLine("體重:{0}", Weigth);
            Console.WriteLine("年齡:{0}", Age);
            Console.WriteLine("體溫:{0}", Temperature);
            Console.WriteLine("我有{0}和{1}", arms, legs);
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM