一、
应用程序,该程序包括3个类:Monkey类、People类和主类E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀……”的信息。
(2)People类是Monkey类的子类,在People类中定义方法speak(),在speak方法中输出“不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。
namespace 继承练习题1
class Program
{
static void Main(string[] args)
{
Mokey mo = new Mokey("猴子");
mo.Speak();
People po = new People("人类");
po.Speak();
po.Think();
}
}
class Mokey
{
public Mokey(String s)
{
this.s = s;
}
protected string s;
public void Speak()
{
Console.WriteLine(s+": 咿咿呀呀....");
}
}
class Test3:Mokey
{
public Test3(string s):base(s)
{
}
public new void Speak()
{
Console.WriteLine(s+": 不错嘛!会说话了!");
}
public void Think()
{
Console.WriteLine(s+": 别说话!认真思考!");
}
}
}
二、
设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。
小车类Car是Vehicle的子类,其中包含的属性有载人数loader。
卡车类Truck是Car类的子类,其中包含的属性有载重量payload。
每个类都有构造方法和输出相关数据的方法。
最后,写一个测试类来测试这些类的功能。
namespace 继承练习题2{
class Program
{
static void Main(string[] args)
{
Vehicle veh = new Vehicle(4, 1000);
veh.ShowMessage();
Car car = new Car(4, 800, 6);
car.ShowMessage();
Truck tru = new Truck(8, 1200, 2, 800);
tru.ShowMessage();
}
}
class Vehicle
{
public Vehicle(int wh,float we){
this.wheels = wh;
this.weight = we;
}
protected int wheels; //车轮个数
protected float weight; //车重
public void ShowMessage()
{
Console.WriteLine("汽车");
Console.WriteLine("车轮数:{0} 车重:{1}",wheels,weight);
}
}
class Car : Vehicle
{
public Car(int wh, float we, int lo)
: base(wh, we)
{
this.loader = lo;
}
protected int loader; //载人数
public void ShowMessage()
{
Console.WriteLine("小车");
Console.WriteLine("车轮数:{0} 车重:{1} 载人数:{2}", wheels, weight,loader);
}
}
class Truck : Car
{
public Truck(int wh, float we, int lo, int plo):base(wh,we,lo)
{
this.payload = plo;
}
public int payload; //载重量
public void ShowMessage()
{
Console.WriteLine("卡车");
Console.WriteLine("车轮数:{0} 车重:{1} 载人数:{2} 载重量:{3}", wheels, weight, loader,payload);
}
}
}
三、
1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,长方体,具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
namespace 继承练习题3{
class Program
{
static void Main(string[] args)
{
Cuboid cuboid = new Cuboid(3, 4, 5);
Console.WriteLine("长为{0}、宽为{1}、高为{2}的长方体",cuboid.length,cuboid.wide,cuboid.height);
Console.WriteLine("底面面积为:"+cuboid.GetArea());
Console.WriteLine("体积为:"+cuboid.GetVolume());
}
}
class Rectangle
{
public Rectangle(float l,float w)
{
length = l;
wide = w;
}
public float length;
public float wide;
public float GetArea()
{
return length * wide;
}
}
class Cuboid : Rectangle{
public Cuboid(float l,float w,float h) : base(l, w)
{
height = h;
}
public float height;
public float GetVolume()
{
return length * wide * height;
}
}
四、
namespace 继承练习题4{
class Program
{
static void Main(string[] args)
{
Cuboid cuboid = new Cuboid(3, 4, 5);
Console.WriteLine("长为{0}、宽为{1}、高为{2}的长方体",cuboid.length,cuboid.wide,cuboid.height);
Console.WriteLine("底面面积为:"+cuboid.GetArea());
Console.WriteLine("体积为:"+cuboid.GetVolume());
}
}
class People2
{
protected double height;
protected double weight;
public void SpeakHello()
{
Console.WriteLine("我是人");
}
public void AverageHeight()
{
}
public void AverageWeight()
{
}
}
class ChinaPeople : People2
{
public void ChinaGongfu()
{
Console.WriteLine("坐如钟,站如松,睡如弓");
}
}
class AmericanPeople : People2
{
public void AmericanBoxing()
{
Console.WriteLine("直拳,勾拳");
}
}
}