标签:库存量 obj value 参数 姓名 识别 generic 创建 this
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
bool jc; 三个都为全局变量一个定义 product[] one = new product[10]; 产品总数的数组, strehouse sto = new strehouse(10); 定义仓库数组的产品上限 product[] one = new product[10];
strehouse sto = new strehouse(10); i 用来控制是存在产品存方在第几个仓库里
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
one[i] = new product(Convert.ToInt32(textBox1.Text), textBox2.Text, textBox3.Text, Convert.ToDouble((textBox4.Text)), Convert.ToInt16(textBox5.Text)); /**产品信息导入添加
sto.add(one[i]); /** 产品数组导入仓库数组
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
priioduct pro1 = sto.getproduct(Convert.ToString(textBox1.Text));
label6.Text = "根据编号查找结果:\n\n编号:" + Convert.ToInt32(pro1.Pid) + "\n\n姓名:" + pro1.Name + "\n\n类型:" +
pro1.Type + "\n\n单价:" + Convert.ToString(pro1.Price) + "\n\n库存量:" + Convert.ToString(pro1.Amount);
};
if (textBox2.Text != "")
{
product pro2 = sto.getproduct(Convert.ToInt16(textBox2.Text));
label6.Text = "根据编号查找结果:\n\n编号:" + Convert.ToInt32(pro2.Pid) + "\n\n姓名:" + pro2.Name + "\n\n类型:" +
pro2.Type + "\n\n单价:" + Convert.ToString(pro2.Price) + "\n\n库存量:" + Convert.ToString(pro2.Amount);
};
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
private void label6_Click(object sender, EventArgs e)
{
}
}
class product
{
private int _pid; 定义product产品信息和产品信息的类成员
public int Pid
{
get { return _pid; } get set 设置可读可写属性 ctrl+r+e为创建方式 当return出值后,属性为pid get set 上的名字如之前的属性为_pid后get set的名字为Pid后续属性边为Pid
set { _pid = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _type;
public string Type
{
get { return _type; }
set { _type = value; }
}
private double _price;
public double Price
{
get { return _price; }
set { _price = value; }
}
private int _amount;
public int Amount
{
get { return _amount; }
set { _amount = value; }
}
public product(int _pid, string _name, string _type, double _price, int _amount)
{
this._pid = _pid; /**构造函数的初始值对象
this._name = _name;
this._price = _price;
this._type = _type;
this._amount = _amount;
}
}
class strehouse
{
private product[] products; /**类成员为其他类型的数组
public int namer;
public strehouse(int n)
{
namer = 0;
products = new product[n]; /**构造函数初始化**/
}
public bool add(product a)
{ /**产品添加进仓库 numer控制进第几个
products[namer] = a;
namer++;
return true;
}
public product getproduct(string name) /**重载函数类,自动识别进入的类型从而判断进入何种方式 string为参数 则为string的参数进入
{
bool pi = false;
for (int i = 0; i < namer; i++)
{
if (products[i].Name == name)
{
pi = true;
return products[i];
}
}
if (pi == true)
{
products[namer] = new product(0, "无", "无", 0, 0);
return products[namer];
}
return products[0];
}
public product getproduct(int id)
{
bool pi = false;
for (int i = 0; i < namer; i++)
{
if (products[i].Pid == id)
{
pi = true;
return products[i];
}
}
if (pi == true)
{
products[namer] = new product(0, "无", "无", 0, 0);
return products[namer];
}
return products[0];
}
}
}
标签:库存量 obj value 参数 姓名 识别 generic 创建 this
原文地址:https://www.cnblogs.com/nianlao/p/11735521.html