1. 程式人生 > >C#面向物件 | 面向物件基礎之綜合練習

C#面向物件 | 面向物件基礎之綜合練習

面向物件程式設計練習

  • 使用類來描述遊戲中的角色
    • 欄位:角色名字、簡介、暱稱、攻擊力、防禦力和速度。
    • 方法:每個角色有三個不同的攻擊技能。
          //角色一:埃洛克的基本資訊. AiLuoKe.cs.
          class AiLuoKe
          {
              private string heroName;
              private string heroInfo;
              private int attack;
              private int defense;
              private int speed;
              private string nickName;
      
              public AiLuoKe()
              {
      
              }
      
              public AiLuoKe(string nickName)
              {
                  this.heroName = "埃洛克";
                  this.heroInfo = "埃洛克是一名來自末日邊境的勇士";
                  this.attack = 80;
                  this.defense = 65;
                  this.speed = 40;
                  this.nickName = nickName;
              }
      
              public void Hello()
              {
                  Console.WriteLine("我的名字是{0},簡介是{1},攻擊力{2},防禦力{3},速度{4},暱稱{5}", this.heroName, this.heroInfo, this.attack, this.defense, this.speed, this.nickName);
              }
      
              
              public string HeroName
              {
                  get { return heroName; }
                  set { heroName = value; }
              }
      
              public string HeroInfo
              {
                  get { return heroInfo; }
                  set { heroInfo = value; }
              }
      
              public int Attack
              {
                  get { return attack; }
                  set { attack = value; }
              }
      
              public int Defense
              {
                  get { return defense; }
                  set { defense = value; }
              }
      
              public int Speed
              {
                  get { return speed; }
                  set { speed = value; }
              }
      
              public string NickName
              {
                  get { return nickName; }
                  set { nickName = value; }
              }
      
              public void SuiShiDaJi()
              {
                  Console.WriteLine("碎石打擊");
              }
      
              public void LieYanMaoGou()
              {
                  Console.WriteLine("烈焰錨鉤");
              }
      
              public void ZhanDouPaoXiao()
              {
                  Console.WriteLine("戰鬥咆哮");
              }
          }
      
                  //呼叫AiLuoKe進行測試. Program.cs.
                  static void Main(string[] args)
              {
                  AiLuoKe Shane = new AiLuoKe("Shane");
                  Shane.Hello();
                  Shane.LieYanMaoGou();
                  Shane.SuiShiDaJi();
                  Shane.ZhanDouPaoXiao();
      
                  Console.ReadKey();
              }