枚举(值类型)
using System;
namespace day7
{
[Flags]enum PersonStyle
{
tall = 1,
rich = 2,
handsome = 4,
man = 8,
woman = 16
}
class Program
{
static void Main(string[] args)
{
/*
* 选择多个枚举
* 位运算 | 按位或:两个对应的二进制位中有一个为1,结果为1
* 条件:
* 1.任意多个枚举值做|运算 的 结果不能与其他枚举值相同(以2的n次方递增)
* 2.定义枚举时使用[Flags]修饰
*
* 判断标志枚举 是否包含指定枚举值
* 运算符 & 按位与:两个对应的二进制位中都为1,结果为1
*/
SelectPerson(PersonStyle.handsome | PersonStyle.man);
// 数据类型转换
// int ==> Enum
PersonStyle style1 = (PersonStyle) 2;
// Enum ==> int
int enumNumber = (int)PersonStyle.handsome;
// string ==> Enum
PersonStyle style02 = (PersonStyle) Enum.Parse(typeof(PersonStyle), "handsome");
// Enum ==> string
string strNum = PersonStyle.handsome.ToString();
}
public static void SelectPerson(PersonStyle style)
{
if ((style & PersonStyle.handsome) == PersonStyle.handsome)
Console.WriteLine("Handsome");
if ((style & PersonStyle.man) != 0)
Console.WriteLine("man");
}
}
}
类(引用类型)
访问级别 class 类名
{ 类成员
字段:存储数据
属性:保护字段
构造函数:提供创建对象方式,初始化类的数据成员
方法:向类的外部提供某种功能
}
通常每一个类都在一个独立的C#源文件中
创建一个新的类意味着当前项目中产生了一个新的数据类型
对象中的声明都在堆中
定义一个wife类
namespace day7
{
/// <summary>
/// 定义老婆类
/// </summary>
public class Wife
{
// 数据成员
private string name;
private string sex;
private int age;
// 方法成员
public void SetName(string name)
{
// this 这个对象引用
this.name = name;
}
public string GetName()
{
return name;
}
public void SetAge(int age)
{
this.age = age;
}
public int GetAge()
{
return age;
}
}
}
对象
类名 引用名;
引用名 = new 构造函数(参数列表);
引用wife类
static void Main()
{
// 声明Wife类型的引用
Wife wife01;
// 指向Wife类型的对象(实例化Wife类型对象)
wife01 = new Wife();
wife01.SetName("DIVA");
wife01.SetAge(18);
Wife wife02 = wife01;
wife02.SetName("EVA");
Console.WriteLine(wife01.GetName());
Console.WriteLine(wife01.GetAge());
}
成员变量
定义在类中方法外的变量
1.具有默认值
2.在类被实例化后存在堆中,对象被回收时,成员变量从堆中清除
3.可与局部变量重名 访问同名成员变量使用this.
值类型:声明在堆中,数据存储在堆中
引用类型:声明在堆中,数据存储在堆的另一块空间
属性
对字段起到保护作用,可实现只读、只写功能
本质就是对字段的读取和写入方法
通常一个共有属性和一个私有字段对应
属性只是外壳,实际上操作的是私有字段
// 自动属性 包含一个字段 两个方法
public string Password{get;set;}
public class Wife
{
// 数据成员
// 字段:存储数据
private string name;
private string sex;
private int age;
// 属性:保护字段,本质就是两个方法
public string Name
{
// 读取时保护
get { return name; }
// 写入时保护 value 要设置的数据
set { this.name = value; }
}
public int Age
{
get { return age; }
set
{
if (value <= 30 && value >= 18)
this.age = value;
else
throw new Exception("容我拒绝");
}
}
}
Wife wife03 = new Wife();
wife03.Name = "Diva";
wife03.Age = 18;
构造函数
提供了创建对象的方式,提供了初始化类数据成员的特殊方法
如果一个类没有构造函数,编辑器会自动提供一个无参数构造函数
如果一个类具有构造函数,编辑器不会提供无参数构造函数
特点:没有返回值 与类同名
如果不希望在类的外部创建对象,则将构造函数私有化 // private Wife(){}
// 构造函数
public Wife()
{
Console.WriteLine("调用构造函数");
}
public Wife(string name):this()
{
// Wife(); 调用无参构造函数
this.name = name; // 构造函数如果为字段赋值,属性中的代码块不会生效
}
public Wife(string name, int age):this(name)
{
// Wife(); 调用上面一个构造函数
// his.name = name; // 构造函数如果为字段赋值,属性中的代码块不会生效
this.Age = age;
}
this关键字表示当前对象的引用
找最小代码例子
static void Main()
{
// // 声明Wife类型的引用
// Wife wife01;
// // 指向Wife类型的对象(实例化Wife类型对象)
// wife01 = new Wife();
// wife01.SetName("DIVA");
// wife01.SetAge(18);
// Wife wife02 = wife01;
// wife02.SetName("EVA");
//
// Console.WriteLine(wife01.GetName());
// Console.WriteLine(wife01.GetAge());
//
// Wife wife03 = new Wife();
// wife03.Name = "Diva";
// wife03.Age = 18;
Wife w01;
w01 = new Wife();
w01.SetName("01");
w01.SetAge(18);
Wife w02 = new Wife("02", 30);
Wife[] wifeArray = new Wife[5];
wifeArray[0] = w01;
wifeArray[1] = w02;
wifeArray[2] = new Wife("03",40);
wifeArray[3] = new Wife("04",20);
wifeArray[4] = new Wife("05",25);
Wife yongestWife = GetWifeByMinimumAge(wifeArray);
}
/// <summary>
/// 找年龄最小wife
/// </summary>
/// <param name="wifes">wife列表</param>
/// <returns>最小wife</returns>
private static Wife GetWifeByMinimumAge(Wife[] wifes)
{
Wife yongestWife = wifes[0];
for (int i = 0; i < wifes.Length; i++)
{
if (yongestWife.Age > wifes[i].Age)
yongestWife = wifes[i];
}
return yongestWife;
}
用户集合类例子
using System;
namespace day7
{
/// <summary>
/// 用户类
/// </summary>
public class User
{
//**********字段***********
private string loginId;
//**********属性***********
// 包含2个方法
public string Loginid
{
get { return this.loginId; }
set { this.loginId = value; }
}
// 自动属性 包含1个字段 2个方法
public string Password { get; set; }
//*********构造函数*********
public User()
{
}
public User(string loginId, string pwd)
{
this.loginId = loginId;
this.Password = pwd;
}
//**********方法***********
public void PrintUser()
{
Console.WriteLine("账号:{0},密码:{1}",Loginid,Password);
}
}
}
namespace day7
{
// UserList 集合:对数组的封装
/// <summary>
/// 用户集合类
/// </summary>
public class UserList
{
//**********字段***********
private User[] data;
private int currentIndex;
//**********属性***********
/// <summary>
/// 有效元素个数
/// </summary>
public int Count
{
get { return currentIndex; }
}
//*********构造函数*********
public UserList():this(8)
{
}
public UserList(int capacity)
{
data = new User[capacity];
}
//**********方法***********
// 添加
public void Add(User value)
{
CheckCapacity();
data[currentIndex++] = value;
}
// 读取
public User GetElement(int index)
{
return data[index];
}
// 扩容
private void CheckCapacity()
{
if (currentIndex >= data.Length)
{
User[] newData = new User[data.Length * 2];
data.CopyTo(newData, 0);
data = newData;
}
}
}
}
static void Main()
{
User u1 = new User("DIO", "wryyyyyyy");
User u2 = new User();
u2.Loginid = "JOJO";
u2.Password = "olaolaola";
UserList list = new UserList(3);
list.Add(u1);
list.Add(u2);
list.Add(new User("Kakyoin","ploplo"));
for (int i = 0; i < list.Count; i++)
{
User user = list.GetElement(i);
user.PrintUser();
}
}
集合
// C# 泛型 集合 list<数据类型>
// User[] new User[];
List<User> list02 = new List<User>(2);
list02.Add(u1);
list.Add(new User("Kakyoin","ploplo"));
list02.Add(u2);
list02.Add(new User());
list02.Insert(0, new User("111", "111"));
list02.RemoveAt(3);
list02.Remove(u1);
for (int i = 0; i < list02.Count; i++)
{
User user = list02[i];
}
字典
// 字典集合 根据? 查找?
Dictionary<string, User> dic = new Dictionary<string, User>();
dic.Add("123",new User("123","123"));
User user04 = dic["123"];