资讯详情

资讯详情

C#面向对象,封装、继承、多态、委托与事件实例

一面向对象封装性编程创建一个控制台应用程序要求1定义一个服装类Cloth具体要求如下1包含3个字段服装品牌mark,服装颜色color定价price2一个接收3个参数的构造函数3自定义函数Information输出一个服装实例的所有信息。2在主类中实现如下功能1调用构造函数创建1个服装类的实例2通过该实例调用Information方法获取该实例信息。如图1所示。public class Cloth{// 字段public string mark; // 服装品牌public string color; // 服装颜色public decimal price; // 定价// 构造函数public Cloth(string mark, string color, decimal price){this.mark mark;this.color color;this.price price;}// 自定义函数Information输出服装实例的所有信息public void Information(){Console.WriteLine($服装品牌: {mark}颜色: {color}定价: {price}元);}}private static void Main(string[] args){// 调用构造函数创建一个服装类的实例Cloth newCloth new Cloth(笨小孩, 蓝色, 300);// 通过该实例调用Information方法获取该实例信息newCloth.Information();// 程序结束等待用户输入以便查看输出信息Console.ReadLine();}二、面向对象继承性编程创建一个控制台应用程序要求1.自定义一个Fruit类添加name字段和构造函数添加Info方法输出水果信息2.再定义一个Banana类Banana类继承Fruit类在Banana类中添加color字段和构造函数再添加一个like方法3.测试类Main方法中实例化两个类对象输出如图2所示。// 自定义Fruit类public class Fruit{// 字段public string name;// 构造函数public Fruit(string name){this.name name;}// Info方法输出水果信息public void Info(){Console.WriteLine($名称{name});}}// Banana类继承Fruit类public class Banana : Fruit{// 字段public string color;// 构造函数public Banana(string name, string color) : base(name){this.color color;}public void InfoCOLOR(){Console.WriteLine($名称{color});}// like方法public void Like(){Console.WriteLine($猴子喜欢吃{color}的{name});}}private static void Main(string[] args){// 实例化Fruit类对象Fruit fruit new Fruit(香蕉);fruit.Info();// 实例化Banana类对象Banana banana new Banana(香蕉, 黄色);banana.InfoCOLOR(); // 调用基类的InfoCOLOR方法banana.Like();// 等待用户输入以便查看输出信息Console.ReadLine();}三、面向对象多态性编程。创建一个控制台应用程序要求1.定义一个Vehicle类包含name字段和虚方法Wheel2.再定义一个Car类继承Vehicle类重写Wheel方法3.再定义一个Bike类继承Vehicle类重写Wheel方法4.测试类Main方法中定义Vehicle数组长度为3实例化各类对象输出如图3所示。// 定义Vehicle基类public class Vehicle{public string Name { get; set; }// Vehicle类的构造函数public Vehicle(string name){Name name;}// 虚方法Wheelpublic virtual void Wheel(){Console.WriteLine(${Name} 都有轮子);}}// Car类继承Vehicle类并重写Wheel方法public class Car : Vehicle{public Car(string name) : base(name) { }// 重写Wheel方法public override void Wheel(){Console.WriteLine(${Name} 有四个轮子);}}// Bike类继承Vehicle类并重写Wheel方法public class Bike : Vehicle{public Bike(string name) : base(name) { }// 重写Wheel方法public override void Wheel(){Console.WriteLine(${Name} 有两个轮子);}}private static void Main(string[] args){// 创建一个Vehicle类型的数组长度为3Vehicle[] vehicles new Vehicle[3];// 实例化各类对象并添加到数组中vehicles[0] new Car(交通工具);vehicles[1] new Bike(汽车);vehicles[2] new Vehicle(自行车); // 默认使用基类的Wheel方法// 遍历数组并调用Wheel方法foreach (var vehicle in vehicles){vehicle.Wheel();}// 等待用户输入以便查看输出信息Console.ReadLine();}四 委托与事件创建一个控制台应用程序要求1.定义一个Monitor类和一个Student类模拟收作业过程2.定义委托类型Mydele和事件Homework3.班长说开始收作业学生响应消息交作业。输出如图4所示。// 定义委托类型MyDelegatepublic delegate void MyDelegate(string message);// 定义事件Homeworkpublic class HomeworkEventArgs : EventArgs{public string Message { get; }public HomeworkEventArgs(string message){Message message;}}// 定义Monitor类public class Monitor{// 事件声明public event EventHandlerHomeworkEventArgs Homework;// 开始收作业的方法public void StartCollectingHomework(){Console.WriteLine(班长说‘开始收作业’);// 检查是否有订阅者并触发事件Homework?.Invoke(this, new HomeworkEventArgs(请交上作业));}}// 定义Student类public class Student{public string Name { get; }public Student(string name){Name name;}// 订阅事件并响应作业提交public void SubscribeHomeworkEvent(Monitor monitor){monitor.Homework OnHomeworkReceived;}// 事件处理方法private void OnHomeworkReceived(object sender, HomeworkEventArgs e){Console.WriteLine(${Name}说‘好的我这就交作业。’);}}private static void Main(string[] args){// 创建Monitor对象Monitor monitor new Monitor();// 创建Student对象Student student1 new Student(学生A);Student student2 new Student(学生B);// 学生订阅收作业事件student1.SubscribeHomeworkEvent(monitor);student2.SubscribeHomeworkEvent(monitor);// 班长开始收作业触发事件monitor.StartCollectingHomework();Console.ReadLine(); // 等待用户输入以便查看输出信息}
觉得有用,分享给同行:

为您的企业打造数字门面

稳重轻奢商务风格,端正雅致视觉,长效耐看不易过时。

立即咨询 →