码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象基础

时间:2015-10-15 01:06:31      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace _01面向对象
{


  class Program
  {
    static void Main(string[] args)
    {
      //Student stu = new Student();
    }
  }


  public class Student
  {
    //定义好一个类以后,不写构造函数会有一个默认的无参数的构造函数


    //当为类手动编写一个构造函数后,会覆盖默认的那个构造函数。
    //只要手动添加了构造函数就把默认的构造函数覆盖了。
  public Student(string name, int age, string gender, string sid)
  {
    this.Name = name;
    this.Age = age;
    this.Gender = gender;
    this.SId = sid;
  }

  //public Student(string name)
  //{
  // this.Name = name;
  //}

  public string Name
  {
    get;
    set;
  }

  private int _age;

  public int Age
  {
    get
    {
      return _age;
    }
    set
    {
      this._age = value;
    }
  }
  public string Gender { get; set; }
  public string SId { get; set; }

  public void SayHi()
  {
    Console.WriteLine("hi!!");
  }

 }
}

2.写了一个猜拳的游戏,总共三个类:Computer,UserPalyer(玩家),caipan(裁判)主要代码如下:

Computer:

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

namespace _03猜拳游戏
{
  public class ComputerUser
  {
    //用来保存计算机出拳结果的属性
    public string FistName
    {
      get;
      set;
    }

    //计算机的出拳方法
    public int ShowFist()
    {
      Random random = new Random();
      int r = random.Next(1, 4);
      //1 石头
      //2 剪刀
      //3 布
      switch (r)
      {
        case 1:
          this.FistName = "石头";
        break;
        case 2:
          this.FistName = "剪刀";
        break;
        case 3:
          this.FistName = "布";
        break;
      }
      return r;
    }
  }
}

UserPlayer:

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

namespace _03猜拳游戏
{
  public class UserPlayer
  {
    public string FistName
    {
      get;
      set;
    }

    //玩家的出拳方法
    public int ShowFist(string fist)
    {

      //1 石头
      //2 剪刀
      //3 布

      this.FistName = fist;
      int result = -1;
      switch (fist)
      {
        case "石头":
          result = 1;
        break;
        case "剪刀":
          result = 2;
        break;
        case "布":
          result = 3;
        break;
      }
      return result;
    }
  }
}

Caipan:

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

namespace _03猜拳游戏
{
  public class CaiPan
  {
    public string IsUserWin(int user, int computer)
    {
      if (user - computer == 0)
      {
        return "平局";
      }
      else if (user - computer == -1 || user - computer == 2)
      {
        return "用户赢了";
      }
      else
      {
        return "输了";
      }
    }
  }
}

事件代码:

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

namespace _03猜拳游戏
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    //石头
    private void button1_Click(object sender, EventArgs e)
    {
      //把sender显示类型转换为button

      //this

      Button btn = (Button)sender;
      if (btn != null)
      {
        UserPlayer u1 = new UserPlayer();
        int userFist = u1.ShowFist(btn.Text);
        lblUser.Text = u1.FistName;

        ComputerUser pc1 = new ComputerUser();
        int computerFist = pc1.ShowFist();
        lblComputer.Text = pc1.FistName;

        CaiPan cp = new CaiPan();
        lblResult.Text = cp.IsUserWin(userFist, computerFist);
      }

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void lblComputer_DoubleClick(object sender, EventArgs e)
    {

    }

  }

}

面向对象基础

标签:

原文地址:http://www.cnblogs.com/xiaxianyue/p/4881088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!