0:
应用程序,该程序包括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个类的功能。
Monkey:
using System;
namespace cchoop
{
class Monkey
{
public Monkey() { }
public Monkey(string s)
{
}
public virtual void Speak()
{
Console.WriteLine("咿咿呀呀......");
}
}
}
People :
using System;
namespace cchoop
{
class People : Monkey
{
public override void Speak()
{
Console.WriteLine("不错嘛!会说话了!");
}
public void Think()
{
Console.WriteLine("别说话!认真思考!");
}
}
}
测试:
using System;
namespace cchoop
{
class Program
{
static void Main(string[] args)
{
Monkey monkey = new Monkey();
monkey.Speak();
Monkey people = new People();
people.Speak();
People people2 = new People();
people2.Think();
}
}
}
1;
设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。
小车类Car是Vehicle的子类,其中包含的属性有载人数loader。
卡车类Truck是Car类的子类,其中包含的属性有载重量payload。
每个类都有构造方法和输出相关数据的方法。
最后,写一个测试类来测试这些类的功能。
using System;
namespace cchoop
{
class Program
{
static void Main(string[] args)
{
Vehicle car = new Car(4, 100, 4);
car.PrintInfo();
Vehicle truck = new Truck(2, 200, 2, 2000);
truck.PrintInfo();
}
}
class Vehicle
{
protected int wheels; //车轮数
protected float weight; //车重
public Vehicle(int wheels, float weight)
{
this.wheels = wheels;
this.weight = weight;
}
public virtual void PrintInfo()
{
Console.WriteLine("wheels:{0},weight:{1}", wheels, weight);
}
}
class Car : Vehicle
{
protected int loader; //载人数
public Car(int wheels, float weight, int loader)
: base(wheels, weight)
{
this.loader = loader;
}
public override void PrintInfo()
{
Console.WriteLine("wheels:{0},weight:{1},loader:{2}", wheels, weight, loader);
}
}
class Truck : Car
{
private float payload; //载重
public Truck(int wheels, float weight, int loader, float payload)
: base(wheels, weight, loader)
{
this.payload = payload;
}
public override void PrintInfo()
{
Console.WriteLine("wheels:{0},weight:{1},loader:{2},payload:{3}", wheels, weight, loader, payload);
}
}
}
2;
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,长方体,具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
using System;
namespace cchoop
{
class Program
{
static void Main(string[] args)
{
Rectangle rectangle = new Rectangle(2, 2, 1.5f);
Console.WriteLine("长方体底面积:{0}", rectangle.GetArea());
Console.WriteLine("长方体面积:{0}", rectangle.GetVolume());
}
}
/// <summary>
/// 矩形类
/// </summary>
class Squareness
{
protected float length;
protected float width;
public Squareness(float length, float width)
{
this.length = length;
this.width = width;
}
public float GetArea()
{
return length * width;
}
}
/// <summary>
/// 长方体
/// </summary>
class Rectangle : Squareness
{
private float height;
public Rectangle(float length, float width, float height)
: base(length, width)
{
this.height = height;
}
public float GetVolume()
{
return length * width * height;
}
}
}