字段
1 2 3
|
private string loginID = string.Empty;
|
属性
1 2 3 4 5 6 7
|
public string LoginID { get { return this.loginID; } set { this.loginID = value; } }
|
自动属性
1 2 3
|
public string? Password { get; set; }
|
构造函数
- 初始化类的实例成员
- 构造函数重载
- 不同参数 可调用
public User(): this("zs","dasda") { }
1 2 3 4 5 6 7 8 9 10
|
public User() { }
public User(string loginID, string Password) { this.loginID = loginID; this.Password = Password; }
|
方法
1 2 3 4 5
| public void PrintUser() { Console.WriteLine("账号:{0},密码:{1}", loginID, Password); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class User { private string loginID = string.Empty;
public string LoginID { get { return this.loginID; } set { this.loginID = value; } }
public string? Password { get; set; }
public User() { }
public User(string loginID, string Password) { this.loginID = loginID; this.Password = Password; }
public void PrintUser() { Console.WriteLine("账号:{0},密码:{1}", loginID, Password); } }
|
C# 集合类
1
| List<User> list = new List<User>(2)
|
1 2 3 4 5 6 7 8 9 10 11 12
| List<User> list2 = new List<User>(1); list2.Add(new User("dsa", "sda")); list2.Add(new User("dsa", "sda")); list2.Add(new User("dsa", "sda"));
for (int i = 0; i < list2.Count; i++) { list2[i].PrintUser(); }
|
c# 字典集合
1 2 3 4
| Dictionary<string,User> list3 = new Dictionary<string,User>(); list3.Add("张三",new User("张三","wdwad222")); list3["张三"].PrintUser(); Console.ReadKey();
|
继承
Protected
父类引用指向子类成员
1
| Person person = new Student();
|
Static 静态
静态成员变量
static public staticCount;
静态构造函数
static Student() {}
静态代码块
- 不能访问实例成员
- 静态代码块加载在实例前
- 实例方法可以访问实例成员和静态成员
静态类
static class NewClass{}
结构体
封装小型类型的值类型
值类型在栈中不用开辟新空间
适用于存放位置、颜色信息
struct