C#.NET程序设计第6章 集合、索引器与泛型实例6-4

设计一个Windows应用程序,要求如下;

(1)构造一个产品基本类;

(2)分别定义家电、日用百货、衣服等派生类,要求具有不同的特征和行为;

(3)定义一个泛型货架类,约束参数类型为产品类,该泛型的货架类包括一个泛型集合,用于存放各种产品对象,并包含一个方法,用于输出每个产品的相关信息。

(4)再仿照实例6-4,定义泛型的货架类对象,完成对产品的添加和信息的输出。

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 Test6
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        Product<Shangpin> sh = new Product<Shangpin>();
        private void btAdd1_Click(object sender, EventArgs e)
        {
            sh.shangpings.Add(new Jiadian(txtName.Text));
            lblShow.Text += string.Format("\n添加家电类:{0}成功", txtName.Text);
           //copy from iotword.com
        }

        private void btAdd2_Click(object sender, EventArgs e)
        {
            sh.shangpings.Add(new Bauhuo(txtName.Text));
            lblShow.Text += string.Format("\n添加日用百货类:{0}成功", txtName.Text);
        }

        private void btAdd3_Click(object sender, EventArgs e)
        {
            sh.shangpings.Add(new Yifu(txtName.Text));
            lblShow.Text += string.Format("\n添加衣服类:{0}成功", txtName.Text);
        }

        private void btPut_Click(object sender, EventArgs e)
        {
            lblShow.Text = sh.ShangjiaTo();
        }

        public abstract class Shangpin
        {
            public string name;
            //copy from iotword.com
            public Shangpin(string name)
            { this.name = name; }
            public abstract string Shangjia();
        }
        public class Jiadian : Shangpin
        {
            public Jiadian(string name) : base(name) { }
            public override string Shangjia()
            {
                return string.Format("[0]:家电类:请轻拿轻放!", name);
            }
        }
        public class Bauhuo : Shangpin
        {
            public Bauhuo(string name) : base(name) { }
            public override string Shangjia()
            {
                return string.Format("[0]:日用百货类:请仔细清点数目!", name);
            }
        }

        public class Yifu : Shangpin
        {
            public Yifu(string name) : base(name) { }
            public override string Shangjia()
            {
                return string.Format("[0]:衣服类:材质易损,请托管保留!", name);
            }
        }
        public class Product<T> where T : Shangpin
        {
            private List<T> shs = new List<T>();
            //copy from iotword.com
            public List<T> shangpings
            {
                get { return shs; }
            }
            public string ShangjiaTo()
            {
                string msg = string.Empty;
                foreach (T sh in shs)
                {
                    msg += "\n" + sh.Shangjia();
                }
                return msg;
            }
        }

    }
}
物联沃分享整理
物联沃-IOTWORD物联网 » C#.NET程序设计第6章 集合、索引器与泛型实例6-4

发表评论

提供最优质的资源集合

立即查看 了解详情