C#学习之路,学习笔记 第九章 继承 9.2由基类创建派生类

 // 试一试:有Vertebral类派生Mammal类

<span style="font-size:14px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _9._2由基类创建派生类
{
    class Program
    {
        static void Main(string[] args)
        {
            Mammal beast = new Mammal();
            Console.WriteLine("我是一只野兽");

            Console.WriteLine("\n我有:");
            Console.WriteLine(beast.Skins);

            Console.WriteLine("\n我会:");
            //下面从基类继承来的函数
            beast.Breathe();
            beast.Eat();
            beast.Sleep();
            //下面是派生类自己定义的函数
            beast.Scukle();
            beast.Run();

            Console.ReadLine();
        }
    }



    class Vertebrata
    {
        //私有成员
        private double bodyWeight;
        private double bodyTemperature;

        //公有成员
        //构造函数
        public Vertebrata()
        {
            bodyTemperature = 0;
            bodyWeight = 0;
        }

        //函数:呼吸
        public void Breathe()
        {
            Console.WriteLine("呼吸");
        }

        //函数:进食
        public void Eat()
        {
            Console.WriteLine("进食");
        }

        //函数:睡觉
        public void Sleep()
        {
            Console.WriteLine("睡觉");
        }
    }

    //下面,我们由Vertebrata类派生Mammal类
    //哺乳动物类
    class Mammal : Vertebrata
    {
        //私有成员
        private string skins;    //毛皮

        //公有成员
        //构造函数
        public Mammal()
        {
            skins = "毛皮";
        }

        //属性:Skins
        public string Skins
        {
            get
            {
                return skins;
            }
            set
            {
                skins = value;
            }
        }

        //函数:哺乳
        public void Scukle()
        {
            Console.WriteLine("哺乳");
        }

        //函数:奔跑
        public void Run()
        {
            Console.WriteLine("奔跑");
        }
    }

}
</span>

运行结果:






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值