步步为营-05-面向对象简单实例

Posted 逍遥小天狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了步步为营-05-面向对象简单实例相关的知识,希望对你有一定的参考价值。

说明:涉及知识点:winform窗体, 

1 首先大致的UI页面做好

 

2 面向对象分析

需要三个对象(电脑,玩家,裁判),

对象对应方法(出拳,出拳,判断)

2.1 首先定义电脑;

  电脑随机出拳,使用到随机方法.其次要把随机数转化成具体出拳内容,通过属性值显示个UI

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FigerPlay
{
   public class Computer
    {
        //获取出的内容,用于显示给UI
        private string fist;
        public string Fist
        {
            get { return fist; }
            set { fist = value; }
        }
        public int ShowFist() 
       {
           //获取随机数--用于判断输赢
           int computerResult = new Random().Next(1,4);
          
           switch (computerResult)
           {
               case 1:
                   this.Fist = "剪刀";
                   break;
               case 2:
                   this.Fist = "石头";
                   break;
               case 3:
                   this.Fist = "";
                   break;
               default :
                   this.Fist = "未知异常";
                   break;
                                      
           }
           return computerResult;
       }
    }
}
Computer

2.2 定义玩家

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FigerPlay
{
   public class Player
    {
        private string fist;

        public string Fist
        {
            get { return fist; }
            set { fist = value; }
        }
        public int ShowFist(string fistContent) 
       {
           this.Fist = fistContent;
           int computerResult = 0;
           switch (fistContent)
           {
               case "剪刀":
                   computerResult = 1;
                   break;
               case "石头":
                   computerResult = 2;
                   break;
               case "":
                   computerResult = 3;
                   break;            
           }
           return computerResult;
       }
    }
}
Player

2.3 定义裁判

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FigerPlay
{
    public class Referee
    {
        public static string ShowResult(int a, int b)
        {
            string resultStr = string.Empty;
            int result = a - b;
            if (result == 1 || result == -2)
            {
                resultStr = "玩家胜";
            }
            if (result == 0)
            {
                resultStr = "";

            }
            if (result == -1 || result == 2)
            {
                resultStr = "电脑胜";
            }
            return resultStr ;
        }

    }
}
Referee

2.4 mian方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FigerPlay
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //当点击"按钮"是触发事件
        private void btnScissors_Click(object sender, EventArgs e)
        {
            //1-调用用户的ShowFist方法:传递一个参数,返回一个int
            string result = btnScissors.Text;
            PlayGame(result);
        }

        private void btnStone_Click(object sender, EventArgs e)
        {
            //1-调用用户的ShowFist方法:传递一个参数,返回一个int
            string result = btnStone.Text;
            PlayGame(result);
        }

        private void btnCloth_Click(object sender, EventArgs e)
        {
            string result = btnCloth.Text;
            PlayGame(result);
        }

        private void PlayGame(string resu)
        {
           
            //1-调用用户的ShowFist方法:传递一个参数,返回一个int
            Player p = new Player();
            string playerContent = resu;
            int playerResult = p.ShowFist(playerContent);
            //2-调用电脑的ShowFist方法
            Computer c = new Computer();
            int computerResult = c.ShowFist();
            //3-调用裁判的比较方法
            string result = Referee.ShowResult(playerResult, computerResult);
            //4-输出结果
            lblComputerResult.Text = c.Fist;
            lblPlayResult.Text = p.Fist;
            lblShowResult.Text = result;
        }

        
    }
}
View Code

以上是关于步步为营-05-面向对象简单实例的主要内容,如果未能解决你的问题,请参考以下文章

面向对象

[js高手之路]面向对象+设计模式+继承一步步改造简单的四则运算

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

面向对象,面向过程,类

一步步了解构造函数