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("睡觉");
        }
    }
}

c#教程

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值