zoukankan      html  css  js  c++  java
  • C# 派生和继承(派生类与基类)

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 继承
    {
        class Program
        {
            static void Main(string[] args)
            {
                Mammal mammal = new Mammal();
                Console.WriteLine("我是一只哺乳动物");
                mammal.Scukle();
                mammal.Breate();
                mammal.Sleep();
                mammal.Message();
            }
        }
        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 void Message()
            {
                Console.WriteLine("体重:{0}", Weigth);
                Console.WriteLine("年龄:{0}", Age);
                Console.WriteLine("体温:{0}", Temperature);
                Console.WriteLine("我有{0}和{1}", arms, legs);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 继承
    {
        class Vertebrate
        {
            private 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 void Breate()
            {
                Console.WriteLine("呼吸");
            }
            public void Sleep()
            {
                Console.WriteLine("睡觉");
            }
        }
    }
  • 相关阅读:
    LOL 战斗力查询
    D3js-对柱状图的增,删,排序
    我的项目7 js 实现歌词同步(额,小小的效果)
    为什么电脑启动任务管理器会这样
    OpenCV求取轮廓线
    leetcode-Reverse Words in a String
    Linux lvs DR配置
    p2p网贷3种运营模式
    T4308 数据结构判断
    1080 线段树练习
  • 原文地址:https://www.cnblogs.com/BruceKing/p/12060992.html
Copyright © 2011-2022 走看看