编写控制台程序,模拟小汽车Car奔跑:
编写Car类(车名、颜色、产地),实现Run()方法
添加有参构造方法,在构造函数中给成员赋值,通过构造函数创建对象,并调用Run()方法
重载Run()方法,给Run()传递一个int类型参数speed(最高车速),输出信息,车速通过参数传递
//创建一个Car类
namespace demo14
{
///
/// 小汽车类
///
public class Car
{
public Car() { }
public Car(string name, string color, string sourcepalce)
{
this.Name = name;
this.Color = color;
this.SourcePlace = sourcepalce;
}
///
/// 车名(品牌)
///
public string Name { get; set; }
///
/// 颜色
///
public string Color { get; set; }
///
/// 产地
///
public string SourcePlace { get; set; }
///
/// 行驶
///
public void Run()
{
Console.WriteLine($"我是一辆{this.Name},颜色是{this.Color},产地在{this.SourcePlace}");
}
public void Run(int speed)
{
Console.WriteLine($"我是一辆{this.Name},颜色是{this.Color},产地在{this.SourcePlace},时速为{speed}");
}
}
}
//在main()方法中调用
namespace demo14
{
class Program
{
static void Main(string[] args)
{
//创建小汽车
Car car1 = new Car("奔驰", "红色", "德国");
Car car2 = new Car("宝马", "白色", "日本");
Car car3 = new Car("法拉利", "银色", "法国");
car1.Run();
car2.Run();
car3.Run(120);
Console.Read();
}
}
}
模拟遥控器控制电视机开、关、换台
//创建一个电视机类
namespace demo14_2
{
///
/// 电视机类
///
class Television
{
///
/// 是否打开
///
private bool isOn;//默认为false
///
/// 更换频道(换台)
///
///
public void Change(int channelNo)
{
Console.WriteLine($"您切换到了第{channelNo}台");
}
public void Open()
{
//调用硬件接口
if(isOn == false)
{
//很多代码
isOn = true;
Console.WriteLine("打开电视机");
}
}
public void Close()
{
if(isOn)
{
//很多代码
isOn = false;
Console.WriteLine("关闭电视机");
}
}
}
}
//创建一个辅助遥控器类
namespace demo14_2
{
///
/// 电视遥控类(遥控并不包含开机、关机功能)
/// 辅助类
///
class RemoteControl
{
///
/// 换台
///
///
///
public void Change(Television tv)//形参
{
Console.WriteLine("请输入频道号:");
int channelNo = int.Parse(Console.ReadLine());
tv.Change(channelNo);
}
///
/// 打开电视机
///
///
public void Open(Television tv)
{
tv.Open();
}
///
/// 关闭电视机
///
///
public void Close(Television tv)
{
tv.Close();
}
}
}
class Program
{
static void Main(string[] args)
{
//创建一个电视机
Television tv1 = new Television();
tv1.Open();
tv1.Change(23);
tv1.Close();
//创建一个电视机遥控
RemoteControl rc1 = new RemoteControl();
rc1.Open(tv1);
//rc1.Open(tv1);//多次打开(关闭)电视机
//rc1.Open(tv1);
rc1.Change(tv1);
rc1.Close(tv1);
Console.Read();
}
}
使用OOP方法模拟顾客点餐
【创建一个顾客类】
namespace demo14_3
{
///
/// 顾客类
///
public class Costomer
{
public Costomer() { }
public Costomer(string name,char gender,string phoneno)
{
this.Name = name;
this.Gender = gender;
this.PhoneNo = phoneno;
}
public string Name { get; set; }//顾客姓名
public char Gender { get; set; }//顾客性别
public string PhoneNo { get; set; }//顾客联系方式
///
/// 点餐功能
///
///
///
public void DianCai(Waiter waiter,Order order)
{
Console.WriteLine($"顾客{this.Name}开始点餐:{order.MealList}");
waiter.GetOrder(order);//鱼香肉丝,水煮鱼,小白菜
}
public void Eat()
{
Console.WriteLine($"顾客{this.Name}开始用餐");
}
}
}
【创建一个服务员类】
namespace demo14_3
{
///
/// 服务员类
///
public class Waiter
{
private Order order;//扩大作用域
///
/// 获取菜单(接受顾客的)
///
///
public void GetOrder(Order dingDan)
{
this.order = dingDan;
}
///
/// 把菜单交给厨师
///
///
public void SetOrder(Chief chief)
{
Console.WriteLine($"服务员把菜单:{order.MealList}传给了厨师");
chief.GetOrder(order);
}
///
/// 上菜
///
public void Serving()
{
Console.WriteLine($"服务员将{order.MealList}上给了{order.SeatNo}桌的顾客");
//通过菜单关联找到点餐的顾客
order.Costomer.Eat();//上菜给客户
}
}
}
【创建一个厨师类】
namespace demo14_3
{
///
/// 厨师类
///
public class Chief
{
private Order order;//扩大作用域
///
/// 接受菜单(从服务员那里获取)
///
///
public void GetOrder(Order dingDan)
{
this.order = dingDan;
}
///
/// 烹饪功能
///
public void Cook()
{
Console.WriteLine($"厨师开始烹饪{this.order.MealList},制作完毕!");
}
///
/// 通知服务员上菜
///
public void Alert(Waiter waiter)
{
Console.WriteLine("厨师通知服务员来取!");
waiter.GetOrder(order);
}
}
}
【创建一个订单类】
namespace demo14_3
{
///
/// 订单类
///
public class Order
{
public Order() { }
public Order(Costomer costomer,int seatno,string meallist)
{
this.Costomer = costomer;
this.SeatNo = seatno;
this.MealList = meallist;
}
public int SeatNo { get; set; }
public string MealList { get; set; }//鱼香肉丝,水煮鱼,小白菜
public Costomer Costomer { get; set; }
}
}
【在main()方法中调用】
namespace demo14_3
{
class Program
{
static void Main(string[] args)
{
//顾客点餐系统开始
//第一步:创建一个客户
Costomer costomer1 = new Costomer("lily",'女',"11586942273");
//第二步:创建一个服务员
Waiter waiter1 = new Waiter();
Waiter waiter2 = new Waiter();
//第三步:创建一个厨师
Chief chief1 = new Chief();
Order order1 = new Order(costomer1,17, "鱼香肉丝,水煮鱼,小白菜");//创建一个菜单
//第四步:客户开始点餐
costomer1.DianCai(waiter1,order1);
//第五步:服务员将顾客的订单信息填写完整之后传递给厨师
waiter1.SetOrder(chief1);
//第六步:厨师烹饪
chief1.Cook();
//第七步:通知服务员上菜
chief1.Alert(waiter2);
//第八步:服务员上菜(通知了谁就让谁上菜)、顾客开始用餐
waiter2.Serving();
Console.Read();
}
}
}
【执行结果】