3

把一个数组封装成类_企鹅型冬天的技术博客_51CTO博客

 2 years ago
source link: https://blog.51cto.com/qiexingdongtian/5473684
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

把一个数组封装成类

原创

企鹅型冬天 2022-07-14 21:56:58 博主文章分类:C# ©著作权

文章标签 c# 封装类 数组 封装 文章分类 IT业界 其它 yyds干货盘点 阅读数134

类中的元素,也可以是int等其他数据类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day07
{
    class User
    {
	private string loginId;
        private string password;
        public string LoginId
        {
            set { loginId = value; }
            get { return loginId; }
        }
        public string Password
        {
            set { password = value; }
            get { return password; }
        }
        public User() { }
        public User(string loginId,string password) {
            this.loginId = loginId;
            this.password = password;
        }
        public void PrintUser() {
            Console.WriteLine("账号:{0},密码:{1}",loginId,password);
        }
    }

把一个数组封装成类,以便于我们去使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day07
{
    /// <summary>
    /// 用户集合类
    /// </summary>
    class UserList
    {
        //字段
        private User[] user;
        private int i = 0;
        //属性
        public int Count
        {
            get { return i; }
        }
        //构造器
        public UserList() : this(8)
        {

        }
        public UserList(int capcity)
        {
            user = new User[capcity];
        }
        //方法
        public void Add(User user1)
        {
            Expansion();
            user[i++] = user1;
        }
        private void Expansion()
        {
            if (i >= user.Length)
            {
                User[] userNew = new User[user.Length * 2];
                for (int j = 0; j < user.Length; j++)
                    userNew[j] = user[j];
                user = userNew;
            }
        }
        public User GetElement(int index)
        {
            return user[index];
        }
    }
}

类在主方法中的调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day07
{
    class Program
    {
        static void Main(string[] args)
        {            UserList userList = new UserList(3);
            userList.Add(new User());
            userList.Add(new User());
            userList.Add(new User());
            userList.Add(new User());
            for (int i = 0;i<userList.Count ;i++ ) {
                User user = userList.GetElement(i);
                user.PrintUser();
            }
        }
    }
}

最后调试结果如图:

把一个数组封装成类_封装
补充:平常我们以用泛型集合、字典集合代替数组来储存元素,而不用我们自己写一个类来装元素
            List<User> list = new List<User>();
            Dictionary<string, User> dictionary = new Dictionary<string, User>();
  • 1
  • 收藏
  • 评论
  • 分享
  • 举报

上一篇:字符串反转代码


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK