具体代码如下:
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();
}
//脊椎动物类
class Vertebrata
{
//私有成员
private double bodyWeight;
private double bodyTemperature;
//公有成员
//构造函数
public Vertebrata()
{
bodyWeight = 0;
bodyTemperature = 0;
}
//函数:呼吸
public void Breathe()
{
Console.WriteLine("呼吸");
}
//函数:进食
public void Eat()
{
Console.WriteLine("进食");
}
函数:睡机
public void Sleep()
{
Console.WriteLine("睡觉");
}
}
//在Vertebrata类中,每个脊椎动物都有bodyWeight和bodyTemperature两个私有数据,
//有Breathe(),Eat()和Sleep()三个行为。当然我们现在还不可能对现实世界进行全真模拟,
//所有只能进行简化处理。
//哺乳动物类
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("奔跑");
}
}
//这里Mammal类由Vertebrata类派生而出,Mammal不光具有自己的成员,
//而且继承了Vertebrata类的所有成员
运行一下结果看看~