User.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace _20200406afternoon
{
class User
{
//字段
private string loginId;
private string passWord;
//属性
public string LoginID
{ get { return this.loginId; }
set { this.loginId = value; }
}
public string PassWord
{
get { return this.passWord; }
set { this.passWord = value; }
}
//构造函数
public User()
{
}
public User(string LoginId,string PassWord)
{
this.LoginID = LoginId;
this.PassWord = PassWord;
}
//方法
public void PrintUserInfo()
{
Console.WriteLine("用户名{0},密码{1}",loginId,passWord);
}
}
}
UserList.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace _20200406afternoon
{
/*需求
* User u1 = new User();
*数组初始化 必须指定大小
* User[] userArray = new User[?];
* 读取元素 必须通过 索引
* userArray[?]= user01
*/
class UserList//用户集合类,对用户数组 进行扩展
{
//************字段********************************
private User[] data;
private int currentIdex=0;
//************属性********************************
public User[] Users
{
get { return this.data; }
set { this.data = value; }
}
/// <summary>
/// 有效元素个数
/// </summary>
public int Capacity
{
get
{
return currentIdex;
}
}
//*************构造函数*****************************
public UserList():this(8)
{
//:this(8)调用的是带参数的构造函数,会执行
}
public UserList(int capacity)
{
data = new User[capacity];
}
//方法
public void Add(User value)
{
CheckCapacity();
data[currentIdex] = value;
}
private void CheckCapacity()
{
if (currentIdex>=data.Length)
{
User[] newData = new User[data.Length * 2];
//这里有个常用方法,CopyTo
data.CopyTo(newData, 0);
data = newData;
}
}
public User GetUser(int index)
{
return data[index];
}
//插入功能
public void InserUser(User data)
{
}
//删除功能
}
}
Program.cs
using System;
using System.Collections.Generic;
namespace _20200406afternoon
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//每日一练
//数组初始化 必须制定大小
UserList list = new UserList(3);
User u1 = new User();
list.Add(u1);
list.Add(new User());
list.Add(new User());
list.Add(new User());
for (int i = 0; i <list.Capacity ; i++)
{
User user = list.GetUser(i);
user.PrintUserInfo();
}
//C# 泛型 List<数据类型> User[]
// 自己写的Userlist 就是C#自带的泛型的功能,有增删改查 等等功能
List<User> list02 = new List<User>();
}
}
}