// 试一试:有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>
运行结果: