最全C#OOP面向对象程序设计源代码(Car.cs)
作者:C/S框架网  发布日期:2011/07/13 23:11:22
最全C#OOP面向对象程序设计源代码(Car.cs)

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Tech2007.CarLibrary
{
   /// <summary>
   /// 汽车基类
   /// </summary>
   public class Car : IDrivable //支持可驾驶接口.
   {
      protected string _ProductCode = string.Empty;
      protected string _Name = string.Empty;
      protected Color _Color = Color.Blue;
      protected string _Module = string.Empty;
      protected string _Manufactory = string.Empty;
      
      public string CarProductCode { get { return _ProductCode; } set { _ProductCode = value; } }
      public string CarName { get { return _Name; } set { _Name = value; } }
      public Color CarColor { get { return _Color; } set { _Color = value; } }
      public string CarModule { get { return _Module; } set { _Module = value; } }
      public string CarManufactory { get { return _Manufactory; } set { _Manufactory = value; } }
      
      public string PrivateProperty { get { return "abc"; } set { ;} }
      
      public override int GetHashCode()
      {
         return base.GetHashCode();
      }
      
      /// <summary>
      /// 用于比较对象,传入的对象与自己比较
      /// </summary>
      public override bool Equals(object obj)
      {
         if (obj is Car) //首先判断传入的对象是不是汽车类型, 比如传进一个乒乓球,绝对不相同了.
         {
            Car car = obj as Car; //将object转换成Car
            
            //判断汽车的名称与型号, 如需要加强判断还可以比较汽车的Color,生产商等.
            if (car.CarName == this.CarName && car.CarModule == this.CarModule)
            return true;
            else
            return false;
         }
         return false;
      }
      
      //汽车引擎
      protected CarEngine _engine = null;
      public CarEngine Engine { get { return _engine; } }
      
      //为了显示动态信息,汽车对象内组合一个处理日志的类
      protected ITraceLog _TraceLog = null;
      
      /// <summary>
      /// 汽车碰撞试验
      /// </summary>
      public virtual void Crash()
      {
         //汽车装有安全装置,没问题,启动安全装置.
         if (this is IProtectDevice)
         {
            (this as IProtectDevice).Reaction();
         }
         else
         {
            _TraceLog.ShowMessage("该车无安全装置,人员受伤!");
         }
      }
      /// <summary>
      /// 驾驶汽车
      /// </summary>
      public virtual void Drive()
      {
         _engine.Run();
         _TraceLog.ShowMessage(_Name + "正在行驶....");
      }
      
      /// <summary>
      /// 喇叭发声
      /// </summary>
      public virtual void PlaySound()
      {
         _TraceLog.ShowMessage(_Name + "按喇嘛警告行人....");
      }
      
      /// <summary>
      /// 打开车门
      /// </summary>
      public virtual void OpenDoor()
      {
         _TraceLog.ShowMessage(_Name + "正在打开车门(开门方式:往左侧/右侧)....");
      }
      
      /// <summary>
      /// 设置汽车的引擎
      /// </summary>
      public void SetEngine(CarEngine engine) { _engine = engine; }
      
      /// <summary>
      /// 用于显示动态信息, 一个附加类, 这里用到简单的合成模式(Composion Pattern)
      /// </summary>
      public void SetTraceLog(ITraceLog log)
      {
         _TraceLog = log;
         _engine.SetLog(log);
      }
      
      /// <summary>
      /// 显示汽车信息
      /// </summary>
      public virtual void ShowCarInfo()
      {
         _TraceLog.ShowMessage("汽车名称: " + _Name);
         _TraceLog.ShowMessage(" 型号: " + _Module);
         _TraceLog.ShowMessage(" 颜色: " + _Color.Name);
         _TraceLog.ShowMessage(" 制造厂: " + _Manufactory);
         if (_engine != null)
         {
            _TraceLog.ShowMessage(" 发动机型号: " + _engine.Name);
            _TraceLog.ShowMessage(" 排量: " + _engine.Displacement);
         }
         else
         _TraceLog.ShowMessage(" 该汽车没有安装发动机");
      }
   }
   
   /// <summary>
   /// 宝马系列
   /// </summary>
   public class CarBMW200X : Car
   {
      public CarBMW200X()
      {
         _ProductCode = "BMW200X";
         _Color = Color.Black;
         _Module = "Module:200X";
         _Name = "宝马商务型汽车";
         _Manufactory = "德国宝马";
         _engine = new Engine_3500ML();
      }
   }
  
上一篇 下一篇