【实例类型】
1双色球类
方法的参数是对象。
public List<string> Numbers { get; set; } // 这个是对象的属性
cs
/// <summary>
/// 双色球类
///
/// 作用:主要是用来封装数据
/// </summary>
public class DoubleChromosphere
{
//public string Red1Number { get; set; }
//public string Red2Number { get; set; }
//public string Red3Number { get; set; }
//public string Red4Number { get; set; }
//public string Red5Number { get; set; }
//public string Red6Number { get; set; }
//public string Blue7Number { get; set; }
请大家自学完课程后,可以尝试分开保存两种球的数字
//public List<string> RedNumbers { get; set; }
//public List<string> BlueNumbers { get; set; }
//今天我们授课:把红色球和蓝色球放到一块
public List<string> Numbers { get; set; }
}
2选号器Selector
选号器看成是一个对象【秘书】
让对象去工作。
设计时:注意属性和字段,只在内部使用的变量用字段。
cs
/// <summary>
/// 选号器类
/// </summary>
public class Selector
{
//红色球池
public List<string> RedNumberList { get; set; }
//蓝色球池
public List<string> BlueNumberList { get; set; }
//存储选择的号码
public List<DoubleChromosphere> SelectedNums { get; set; } = new List<DoubleChromosphere>();
private Random random = new Random();//随机数生成器(只在本类内部使用)
/// <summary>
/// 构造方法:初始化球池的数字
/// </summary>
public Selector()
{
RedNumberList = new List<string>
{
"01","02","03","04","05","06","07","08","09","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33"
};
BlueNumberList = new List<string>
{
"01","02","03","04","05","06","07","08","09","10",
"11","12","13","14","15","16"
};
}
/// <summary>
/// 生成7个随机不同的号码
/// </summary>
/// <returns></returns>
public List<string> CreateRandomNum()
{
List<string> numList = new List<string>();
//生成6个红色球
while (true)
{
if (numList.Count == 6) break;//如果随机选取了6个数字了,则退出循环
string num = RedNumberList[random.Next(33)];
if (numList.Contains(num)) continue;//如果是重复的数字,则重新随机抽取
else
{
numList.Add(num);
}
}
//生成一个蓝色球
numList.Add(BlueNumberList[random.Next(16)]);
return numList;
}
//打印、远程保存等(我们在深入的VIP系统学习中再讲解...)
//这些方法,都会用到我们所保存的《已选择号码》
}
【ui】
其他类,是对象,只需要用【new】出,让对象去工作。
ui只需要关心【输入输出】,业务逻辑不必关心。
【字段】是类内全局变量。
1窗体的初始化
cs
namespace xiketang.com.LotteryPro
{
public partial class FrmMain : Form
{
//创建选号器对象
private Selector newSelector = new Selector();
//每次得到的随机数
List<string> randomNum = null;
public FrmMain()
{
InitializeComponent();
this.btnSelect.Enabled = false;
}
2对象工作
//每次得到的随机数
List<string> randomNum = null;
cs
//启动选号
private void btnStart_Click(object sender, EventArgs e)
{
this.timerCreateNum.Start();//启动定时器
this.btnSelect.Enabled = true;
this.btnStart.Enabled = false;
}
//定时器事件:每隔50ms调用一次
private void timerCreateNum_Tick(object sender, EventArgs e)
{
//通过调用选号器,得到随机数
randomNum = newSelector.CreateRandomNum();
//在下面的列表中显示
this.lblNum1.Text = randomNum[0];
this.lblNum2.Text = randomNum[1];
this.lblNum3.Text = randomNum[2];
this.lblNum4.Text = randomNum[3];
this.lblNum5.Text = randomNum[4];
this.lblNum6.Text = randomNum[5];
this.lblNum7.Text = randomNum[6];
}
//选择
private void btnSelect_Click(object sender, EventArgs e)
{
this.timerCreateNum.Stop();
this.btnSelect.Enabled = false;
this.btnStart.Enabled = true;
//创建双色球对象
DoubleChromosphere dc = new DoubleChromosphere
{
//Numbers = new List<string>()
//{
// this.lblNum1.Text,
// this.lblNum2.Text,
// this.lblNum3.Text,
// this.lblNum4.Text,
// this.lblNum5.Text,
// this.lblNum6.Text,
// this.lblNum7.Text
//}
//对象初始化器【双色球对象】
Numbers = this.randomNum //我们因为已经保存了当前的一组数字,所以可以直接复制
};
//添加到选号池,以备后面的打印、远程保存等方法使用
this.newSelector.SelectedNums.Add(dc);
//显示号码
string num = string.Empty;
foreach (string item in this.randomNum)
{
num += item + " ";
}
this.lbNumberList.Items.Add(num);
}
//清除
private void btnDel_Click(object sender, EventArgs e)
{
this.newSelector.SelectedNums.Clear(); //从集合中清除
this.lbNumberList.Items.Clear();//从列表中清除
}
//确认购买:需要保存到远程数据库或服务器(也可以通过webapi远程提交)
private void btnConfirm_Click(object sender, EventArgs e)
{
//首先打印
//远程保存
}
3程序入口
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xiketang.com.LotteryPro
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMain());
}
}
}