欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#Protected和多态(虚方法)

程序员文章站 2023-11-10 22:19:46
Protected 在基类中定义后,能被派生类调用,但是不能被其他类调用。 virtual 在基类中定义后,在派生类中能被重写。 using System; using System.Collections.Generic; using System.Text; namespace 继承 { cla ......

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);
        }
    }
}