C#对自定义类进行运算符重载以及使用copyto对数组进行合并

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;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Web;
namespace test重载加运算符
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string jsonString1 = "{\"status\":{\"code\":\"0\",\"msg\":\"正确\"},\"data\":{\"picture_num\":6,\"picture_list\":[{\"picture_name\":\"img1\",\"picture_scope\":1,\"picture_sign\":\"da16641a9dd3d0ed099ac04c6823826c7bde5d61\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://g.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=2b0b1aab2a34349b6b066882f9d164f3/b7fd5266d0160924a53e59aad60735fae6cd3463.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://g.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=5d653573b0119313c743fbb4500037a6/b7fd5266d0160924a53e59aad60735fae6cd3463.jpg\"},{\"picture_name\":\"img2\",\"picture_scope\":1,\"picture_sign\":\"7361946e8bcf21193817d71425f2bf78fb25b360\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://e.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=431161427b310a55db24d8f3877e3294/37d12f2eb9389b50fbe51be38735e5dde7116e6d.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://e.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=8d357d9ab251f819f125074eef8c719c/37d12f2eb9389b50fbe51be38735e5dde7116e6d.jpg\"},{\"picture_name\":\"img3\",\"picture_scope\":1,\"picture_sign\":\"c43df3a2be33ec2b9d0c358f9cee061f50d68460\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://f.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=f4f8676d881363270aedc434a1b4d1d1/a9d3fd1f4134970ad569eb9e97cad1c8a7865d6d.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://f.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=aa6dee25442309f7e76fa91647363782/a9d3fd1f4134970ad569eb9e97cad1c8a7865d6d.jpg\"},{\"picture_name\":\"img4\",\"picture_scope\":1,\"picture_sign\":\"d6cae9083ba9d7f35dc1ce5721f30cf184600c40\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://f.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=1413c7a2df54564efa65e23e83e5edbf/8b13632762d0f703ccc86e6d0afa513d2697c50d.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://f.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=fb596652ff1f4134e037017a1027aebb/8b13632762d0f703ccc86e6d0afa513d2697c50d.jpg\"},{\"picture_name\":\"img5\",\"picture_scope\":1,\"picture_sign\":\"697d423ae23f0d523d532ef5fc040c812240c741\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://a.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=790563cad31373f0ea3f699894343ac6/4a36acaf2edda3ccd49f439103e93901213f920c.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://a.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=438279c441a98226b8c12f23bfba8270/4a36acaf2edda3ccd49f439103e93901213f920c.jpg\"},{\"picture_name\":\"img6\",\"picture_scope\":1,\"picture_sign\":\"6a97ad9e4e18f7176150617cb71120d33ce4df60\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://a.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=423cc07519d5ad6eb5f962edb1f048e6/3b87e950352ac65c801f9dc2f9f2b21193138a6d.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://a.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=ebae50d00db30f24359ae807fdadea32/3b87e950352ac65c801f9dc2f9f2b21193138a6d.jpg\"}]}}";
            //使用json对对象进行赋值
            ImageList _list1 = JsonConvert.DeserializeObject<ImageList>(jsonString1);
            string jsonString2 = "{\"status\":{\"code\":\"0\",\"msg\":\"正确\"},\"data\":{\"picture_num\":5,\"picture_list\":[{\"picture_name\":\"img7\",\"picture_scope\":1,\"picture_sign\":\"da16641a9dd3d0ed099ac04c6823826c7bde5d61\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://g.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=2b0b1aab2a34349b6b066882f9d164f3/b7fd5266d0160924a53e59aad60735fae6cd3463.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://g.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=5d653573b0119313c743fbb4500037a6/b7fd5266d0160924a53e59aad60735fae6cd3463.jpg\"},{\"picture_name\":\"img8\",\"picture_scope\":1,\"picture_sign\":\"7361946e8bcf21193817d71425f2bf78fb25b360\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://e.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=431161427b310a55db24d8f3877e3294/37d12f2eb9389b50fbe51be38735e5dde7116e6d.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://e.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=8d357d9ab251f819f125074eef8c719c/37d12f2eb9389b50fbe51be38735e5dde7116e6d.jpg\"},{\"picture_name\":\"img9\",\"picture_scope\":1,\"picture_sign\":\"c43df3a2be33ec2b9d0c358f9cee061f50d68460\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://f.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=f4f8676d881363270aedc434a1b4d1d1/a9d3fd1f4134970ad569eb9e97cad1c8a7865d6d.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://f.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=aa6dee25442309f7e76fa91647363782/a9d3fd1f4134970ad569eb9e97cad1c8a7865d6d.jpg\"},{\"picture_name\":\"img10\",\"picture_scope\":1,\"picture_sign\":\"d6cae9083ba9d7f35dc1ce5721f30cf184600c40\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://f.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=1413c7a2df54564efa65e23e83e5edbf/8b13632762d0f703ccc86e6d0afa513d2697c50d.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://f.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=fb596652ff1f4134e037017a1027aebb/8b13632762d0f703ccc86e6d0afa513d2697c50d.jpg\"},{\"picture_name\":\"img11\",\"picture_scope\":1,\"picture_sign\":\"697d423ae23f0d523d532ef5fc040c812240c741\",\"is_cover\":0,\"width\":1920,\"height\":1200,\"img_src\":\"http://a.hiphotos.bdimg.com/album/h%3D370%3Bq%3D90/sign=790563cad31373f0ea3f699894343ac6/4a36acaf2edda3ccd49f439103e93901213f920c.jpg\",\"m_width\":592,\"m_height\":370,\"pic_big_src\":\"http://a.hiphotos.bdimg.com/album/w%3D1458%3Bq%3D90/sign=438279c441a98226b8c12f23bfba8270/4a36acaf2edda3ccd49f439103e93901213f920c.jpg\"}]}}";
            //使用json对对象进行赋值
            ImageList _list2 = JsonConvert.DeserializeObject<ImageList>(jsonString2);
            //示例
            ImageList _list3 = _list1 + _list2;
        }
    }

    //相片列表类
    public class ImageList
    {
        //重载+运算符
        public static ImageList operator +(ImageList ImageList1, ImageList ImageList2)
        {
            //计算新ImageList的长度
            int newListLength = ImageList1.data.picture_list.Count() + ImageList2.data.picture_list.Count();
            //实例化临时的ImageList
            ImageList temp = new ImageList(newListLength);
            //如果构造函数已经有默认值则不需赋值了
            //temp.status = ImageList1.status;
            //将第一个参数复制进新的ImageList对象
            ImageList1.data.picture_list.CopyTo(temp.data.picture_list, 0);
            //将第二个参数复制进新的ImageList对象,数组的起始位置为第一数组参数的长度
            ImageList2.data.picture_list.CopyTo(temp.data.picture_list, ImageList1.data.picture_list.Count());
            //赋值
            temp.data.picture_num = newListLength.ToString();
            return temp;
        }
        public ImageList(int lengthOfpicture_list)
        {
            this.status = new Status();
            this.data = new Data(lengthOfpicture_list);
        }

        public Status status { get; set; }
        public Data data { get; set; }

        public class Status
        {
            public Status() 
            {
                this.code = "0";
                this.msg = "正确";
            }
            public string code { get; set; }
            public string msg { get; set; }
        }
        public class Data
        {
            public Data(int lengthOfpicture_list) 
            {
                this.picture_num = "";
                this.picture_list = new Picture_list[lengthOfpicture_list];
            }
            public string picture_num { get; set; }
            public Picture_list[] picture_list { get; set; }
        }

        public class Picture_list
        {
            public Picture_list() { }
            public string picture_name { get; set; }
            public string picture_scope { get; set; }
            public string picture_sign { get; set; }
            public string is_cover { get; set; }
            public string width { get; set; }
            public string height { get; set; }
            public string img_src { get; set; }
            public string m_width { get; set; }
            public string m_height { get; set; }
            public string pic_big_src { get; set; }
        }
    }
}

 

posted @ 2014-03-07 20:43  ashamp  阅读(455)  评论(0编辑  收藏  举报