zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }
  • 相关阅读:
    kafka整理笔记笔记
    node(一)安装nodejs最新版到debian,ubuntu,mint系统
    ubuntu16.04安装visual-studio-code
    ubuntu16.04更新内核--使用4.6以上的内核会让用A卡的Dell电脑更快--及卸载多余内核
    linux查看主板型号及内存硬件信息,及硬盘测速
    git使用,在ubuntu中
    Ubuntu中文目录文件夹改为英文
    w3m 在ubuntu中的使用
    关于右键属性与du -sh显示的文件大小不一致的解决
    ubuntu16.04安装chrome
  • 原文地址:https://www.cnblogs.com/BruceKing/p/12068053.html
Copyright © 2011-2022 走看看