如何使用封装编写正常的List.Add方法?

时间:2017-06-14 13:55:38

标签: c# list listview encapsulation

我是C#的新手,我在代码中添加了一个List。它工作得很好,但我也希望用封装来做到这一点。我做了一个开始,但我坚持了一点。谢谢您的回答。 这是我正常列表的类。

namespace Data
{
    public static class ListItem2Operations
    {
        public static List<ListItem2> GetMenuItems(string type)
        {
            List<ListItem2> items = new List<ListItem2>();

            items.Add(new ListItem2("image/hotel_porto_bay_rio_internacional_de_luxe_800x600.jpg", "0px,0px,0px", "lux"));
            items.Add(new ListItem2("image/hotel_porto_bay_rio_internacional_rooftop_pool_800x600.jpg", "0px,0px,0px", "lux"));
            items.Add(new ListItem2("image/hotel_porto_bay_serra_golf_library_800x600.jpg", "0px,0px,0px", "nice"));
            items.Add(new ListItem2("image/hotel_porto_bay_serra_golf_living_room_800x600.jpg", "0px,25px,0px", "lux"));
            items.Add(new ListItem2("image/hotel_porto_bay_serra_golf_suite_800x600.jpg", "0px,25px,0px", "nice"));
            items.Add(new ListItem2("image/lhotel_porto_bay_sao_paulo_suite_lhotel_living_room_800x600.jpg", "0px,25px,0px", "nice"));
            return items;
        }
    }
}

这是我初始化变量的地方。

namespace Data
{
    public class ListItem2
    {
        public string Src { get; set; }

        public string Pos { get; set; }

        public string Category { get; set; }
        public ListItem2(string Src, string Pos, string Category)
        {

            this.Src = Src;
            this.Pos = Pos;
            this.Category = Category;
        }
    }
}

这是我对封装的初始化;

class Operations
{
    private string src;
    public string _src
    {
        get { return src; }
        set { src = value; }
    }
    private string pos;
    public string _pos
    {
        get { return pos; }
        set { pos = value; }
    }
    private string category;
    public string _category
    {
        get { return category; }
        set { category = value; }
    }

    public Operations(string Src, string Pos, string Category)
    {

        this._src = src;
        this._pos = pos;
        this._category = category;
    }

    List<string> items = new List<string>();
}

感谢您的回答并度过了愉快的一天。

0 个答案:

没有答案