s4-1
设计项目s4-1。创建一个Vehicle类并将它声明为抽象类。在Vehicle类中声明一个NoOfWheels方法 使它返回一个字符串值。创建两个类Car和Motorbike从Vehicle类继承 并在这两个类中实现NoOfWheels方法。在Car类中 应当显示“四轮车”信息 而在Motorbike类中应当显示“双轮车”信息。创建另一个带Main方法的测试类,在该类中创建Car和Motorbike的实例 并在控制台中显示消息。
using System;
namespace s4_1
{
abstract class Vehicle
{
public Vehicle()
{
Console.WriteLine("抽象交通工具类被构造!");
}
public abstract string NoOfWheels();
}
class Car : Vehicle
{
public Car() : base()
{
Console.WriteLine("四轮车类被构造!");
}
public override string NoOfWheels()
{
string str = "这是一个四轮车。";
Console.WriteLine(str);
return str;
}
}
class Motorbike : Vehicle
{
public Motorbike() : base()
{
Console.WriteLine("双轮车类被构造!");
}
public override string NoOfWheels()
{
string str = "这是一个双轮车。";
Console.WriteLine(str);
return str;
}
}
class Program
{
public static void Main(string[] args)
{
Car MyCar = new Car();
Motorbike MyMotorbike = new Motorbike();
MyCar.NoOfWheels();
MyMotorbike.NoOfWheels();
Console.ReadLine();
}
}
}
设计测试用例,首先生成Car类和Motorbike类的对象MyCar和MyMotorbike,之后分别调用其重写的NoOfWheels方法,并输出相应信息。
s4-2
设计项目s4-2。定义一个接口,用于描述动物的生活习性,包含食性、捕食、分布区域、睡觉方式、群居方式等;接口里面包括属性和方法。然后定义猫、老虎、狮子、大象等几个动物类来实现该接口;最后定义一个调用类,来运行程序,查看接口的使用情况。
using System;
namespace s4_2
{
public interface IHabit
{
string food { get; set; }
string place { get; set; }
string sleep { get; set; }
string living { get; set; }
void CatchFood();
void ShowDetails();
}
public class Cat : IHabit
{
public string food
{
get
{
return "肉食性";
}
set { }
}
public string place
{
get
{
return "野外分布较少,多作为宠物分布于人类聚居区";
}
set { }
}
public string sleep
{
get
{
return "睡姿多样,睡眠时间长";
}
set { }
}
public string living
{
get
{
return "喜聚居";
}
set { }
}
public void CatchFood()
{
Console.WriteLine("猫完成捕食");
}
public void ShowDetails()
{
Console.WriteLine("猫的食性:" + food);
Console.WriteLine("猫的分布区域:" + place);
Console.WriteLine("猫的睡觉方式:" + sleep);
Console.WriteLine("猫的群居方式:" + living);
}
}
public class Tiger : IHabit
{
public string food
{
get
{
return "肉食性";
}
set { }
}
public string place
{
get
{
return "均为野外分布,数量较少,温带、亚热带、热带均有分布";
}
set { }
}
public string sleep
{
get
{
return "卧姿,睡眠时间长";
}
set { }
}
public string living
{
get
{
return "不喜聚居,多为独行";
}
set { }
}
public void CatchFood()
{
Console.WriteLine("老虎完成捕食");
}
public void ShowDetails()
{
Console.WriteLine("老虎的食性:" + food);
Console.WriteLine("老虎的分布区域:" + place);
Console.WriteLine("老虎的睡觉方式:" + sleep);
Console.WriteLine("老虎的群居方式:" + living);
}
}
public class Lion : IHabit
{
public string food
{
get
{
return "肉食性";
}
set { }
}
public string place
{
get
{
return "均为野外分布,数量较少,主要分布于热带亚热带";
}
set { }
}
public string sleep
{
get
{
return "卧姿,睡眠时间长";
}
set { }
}
public string living
{
get
{
return "喜聚居,有雄性首领";
}
set { }
}
public void CatchFood()
{
Console.WriteLine("狮子完成捕食");
}
public void ShowDetails()
{
Console.WriteLine("狮子的食性:" + food);
Console.WriteLine("狮子的分布区域:" + place);
Console.WriteLine("狮子的睡觉方式:" + sleep);
Console.WriteLine("狮子的群居方式:" + living);
}
}
public class Elephent : IHabit
{
public string food
{
get
{
return "植食性";
}
set { }
}
public string place
{
get
{
return "均为野外分布,主要分布于热带";
}
set { }
}
public string sleep
{
get
{
return "站姿睡眠";
}
set { }
}
public string living
{
get
{
return "喜聚居,有雄性首领";
}
set { }
}
public void CatchFood()
{
Console.WriteLine("大象完成捕食");
}
public void ShowDetails()
{
Console.WriteLine("大象的食性:" + food);
Console.WriteLine("大象的分布区域:" + place);
Console.WriteLine("大象的睡觉方式:" + sleep);
Console.WriteLine("大象的群居方式:" + living);
}
}
class Program
{
public static void Main(string[] args)
{
Cat MyCat = new Cat();
MyCat.ShowDetails();
MyCat.CatchFood();
Console.WriteLine();
Tiger MyTiger = new Tiger();
MyTiger.ShowDetails();
MyTiger.CatchFood();
Console.WriteLine();
Lion MyLion = new Lion();
MyLion.ShowDetails();
MyLion.CatchFood();
Console.WriteLine();
Elephent MyElephent = new Elephent();
MyElephent.ShowDetails();
MyElephent.CatchFood();
Console.ReadLine();
}
}
}
设计测试用例,首先用四个类实现接口,之后在主方法中分别创建四种动物的对象,输出相应信息及捕食方法,结果如下所示。
s4-3
设计项目s4-3。抽象类、继承、接口综合:设计一个系统 XXX门的实现过程。流程:设计一张抽象的门Door,那么对于这张门来说就应该拥有所有门的共性:开门openDoor()和关门closeDoor() ,然后对门进行另外的功能设计,防盗–theftproof()、防水–waterproof()、防弹–bulletproof()、防火、防锈…… 要求:利用继承、抽象类、接口的知识设计该门。
using System;
namespace s4_3
{
class Program
{
public abstract class Door
{
public Door() {}
public abstract void openDoor();
public abstract void closeDoor();
}
public interface IDoor
{
string name { get; set; }
string materials { get; set; }
void theftproof();
void waterproof();
void bulletproof();
void fireproof();
void rustproof();
void Show();
}
public class WoodyDoor : Door,IDoor
{
public string name
{
get
{
return "木门";
}
set{}
}
public string materials
{
get
{
return "木质";
}
set{}
}
public void theftproof()
{
Console.Write("不防盗");
}
public void waterproof()
{
Console.Write("不防水");
}
public void bulletproof()
{
Console.Write("不防弹");
}
public void fireproof()
{
Console.Write("不防火");
}
public void rustproof()
{
Console.Write("防生锈");
}
public void Show()
{
Console.WriteLine("名称:" + name);
Console.WriteLine("材质:" + materials);
Console.Write("防盗性:");
theftproof();
Console.WriteLine();
Console.Write("防水性:");
waterproof();
Console.WriteLine();
Console.Write("防弹性:");
bulletproof();
Console.WriteLine();
Console.Write("防火性:");
fireproof();
Console.WriteLine();
Console.Write("防锈性:");
rustproof();
Console.WriteLine("\n");
}
public override void openDoor()
{
Console.WriteLine("木门已开门");
}
public override void closeDoor()
{
Console.WriteLine("木门已关门");
}
}
public class IronDoor : Door,IDoor
{
public string name
{
get
{
return "铁门";
}
set{}
}
public string materials
{
get
{
return "不锈钢";
}
set{}
}
public void theftproof()
{
Console.Write("防盗");
}
public void waterproof()
{
Console.Write("不防水");
}
public void bulletproof()
{
Console.Write("防弹");
}
public void fireproof()
{
Console.Write("不防火");
}
public void rustproof()
{
Console.Write("防生锈");
}
public void Show()
{
Console.WriteLine("名称:" + name);
Console.WriteLine("材质:" + materials);
Console.Write("防盗性:");
theftproof();
Console.WriteLine();
Console.Write("防水性:");
waterproof();
Console.WriteLine();
Console.Write("防弹性:");
bulletproof();
Console.WriteLine();
Console.Write("防火性:");
fireproof();
Console.WriteLine();
Console.Write("防锈性:");
rustproof();
Console.WriteLine("\n");
}
public override void openDoor()
{
Console.WriteLine("铁门已开门");
}
public override void closeDoor()
{
Console.WriteLine("铁门已关门");
}
}
public class GlassDoor : Door,IDoor
{
public string name
{
get
{
return "玻璃门";
}
set{}
}
public string materials
{
get
{
return "玻璃";
}
set{}
}
public void theftproof()
{
Console.Write("不防盗");
}
public void waterproof()
{
Console.Write("防水");
}
public void bulletproof()
{
Console.Write("不防弹");
}
public void fireproof()
{
Console.Write("不防火");
}
public void rustproof()
{
Console.Write("防生锈");
}
public void Show()
{
Console.WriteLine("名称:" + name);
Console.WriteLine("材质:" + materials);
Console.Write("防盗性:");
theftproof();
Console.WriteLine();
Console.Write("防水性:");
waterproof();
Console.WriteLine();
Console.Write("防弹性:");
bulletproof();
Console.WriteLine();
Console.Write("防火性:");
fireproof();
Console.WriteLine();
Console.Write("防锈性:");
rustproof();
Console.WriteLine("\n");
}
public override void openDoor()
{
Console.WriteLine("玻璃门已开门");
}
public override void closeDoor()
{
Console.WriteLine("玻璃门已关门");
}
}
public class RubberDoor : Door,IDoor
{
public string name
{
get
{
return "橡胶门";
}
set{}
}
public string materials
{
get
{
return "橡胶";
}
set{}
}
public void theftproof()
{
Console.Write("不防盗");
}
public void waterproof()
{
Console.Write("防水");
}
public void bulletproof()
{
Console.Write("防弹");
}
public void fireproof()
{
Console.Write("防火");
}
public void rustproof()
{
Console.Write("防生锈");
}
public void Show()
{
Console.WriteLine("名称:" + name);
Console.WriteLine("材质:" + materials);
Console.Write("防盗性:");
theftproof();
Console.WriteLine();
Console.Write("防水性:");
waterproof();
Console.WriteLine();
Console.Write("防弹性:");
bulletproof();
Console.WriteLine();
Console.Write("防火性:");
fireproof();
Console.WriteLine();
Console.Write("防锈性:");
rustproof();
Console.WriteLine("\n");
}
public override void openDoor()
{
Console.WriteLine("橡胶门已开门");
}
public override void closeDoor()
{
Console.WriteLine("橡胶门已关门");
}
}
public class FireProofDoor : Door,IDoor
{
public string name
{
get
{
return "防火门";
}
set{}
}
public string materials
{
get
{
return "防火材料";
}
set{}
}
public void theftproof()
{
Console.Write("不防盗");
}
public void waterproof()
{
Console.Write("不防水");
}
public void bulletproof()
{
Console.Write("不防弹");
}
public void fireproof()
{
Console.Write("防火");
}
public void rustproof()
{
Console.Write("防生锈");
}
public void Show()
{
Console.WriteLine("名称:" + name);
Console.WriteLine("材质:" + materials);
Console.Write("防盗性:");
theftproof();
Console.WriteLine();
Console.Write("防水性:");
waterproof();
Console.WriteLine();
Console.Write("防弹性:");
bulletproof();
Console.WriteLine();
Console.Write("防火性:");
fireproof();
Console.WriteLine();
Console.Write("防锈性:");
rustproof();
Console.WriteLine("\n");
}
public override void openDoor()
{
Console.WriteLine("防火门已开门");
}
public override void closeDoor()
{
Console.WriteLine("防火门已关门");
}
}
public class SpecialDoor : Door,IDoor
{
public string name
{
get
{
return "特种门";
}
set{}
}
public string materials
{
get
{
return "合金及复合材料";
}
set{}
}
public void theftproof()
{
Console.Write("防盗");
}
public void waterproof()
{
Console.Write("防水");
}
public void bulletproof()
{
Console.Write("防弹");
}
public void fireproof()
{
Console.Write("防火");
}
public void rustproof()
{
Console.Write("防生锈");
}
public void Show()
{
Console.WriteLine("名称:" + name);
Console.WriteLine("材质:" + materials);
Console.Write("防盗性:");
theftproof();
Console.WriteLine();
Console.Write("防水性:");
waterproof();
Console.WriteLine();
Console.Write("防弹性:");
bulletproof();
Console.WriteLine();
Console.Write("防火性:");
fireproof();
Console.WriteLine();
Console.Write("防锈性:");
rustproof();
Console.WriteLine("\n");
}
public override void openDoor()
{
Console.WriteLine("特种门已开门");
}
public override void closeDoor()
{
Console.WriteLine("特种门已关门");
}
}
public static void Main(string[] args)
{
WoodyDoor MyWoodyDoor = new WoodyDoor();
IronDoor MyIronDoor = new IronDoor();
GlassDoor MyGlassDoor = new GlassDoor();
RubberDoor MyRubberDoor = new RubberDoor();
FireProofDoor MyFireProofDoor = new FireProofDoor();
SpecialDoor MySpecialDoor = new SpecialDoor();
MyWoodyDoor.openDoor();
MyWoodyDoor.closeDoor();
MyWoodyDoor.Show();
MyIronDoor.openDoor();
MyIronDoor.closeDoor();
MyIronDoor.Show();
MyGlassDoor.openDoor();
MyGlassDoor.closeDoor();
MyGlassDoor.Show();
MyRubberDoor.openDoor();
MyRubberDoor.closeDoor();
MyRubberDoor.Show();
MyFireProofDoor.openDoor();
MyFireProofDoor.closeDoor();
MyFireProofDoor.Show();
MySpecialDoor.openDoor();
MySpecialDoor.closeDoor();
MySpecialDoor.Show();
Console.ReadLine();
}
}
}
编写抽象门类,在抽象类中定义开门和关门抽象方法。编写门类接口,包含门的名称、材料、性能等成员。之后分别编写不同种类的门使之继承抽象类和接口。测试用例中创建各种门的对象后分别调用重写后的开门方法和关门方法,并输出其防盗性、防水性、防弹性、防火性、防生锈等性质,从而实现对一系列门的设计实现。
|