1. 字符串格式化
static void Main(string[] args)
{
//方法一:
string name = "Fxx";
string email_type = "qq";
string my_info = "邮箱用户名:{0};邮箱类型{1}";
my_info = string.Format(my_info, name, email_type);
//方法二:
string a = string.Format("邮箱用户名:{0};邮箱类型{1}", name, email_type);
//方法三:
string b = $"邮箱用户名:{name};邮箱类型{email_type}";
}
2. 数组
- 同时储存相同类型的数据的
static void Main(string[] args)
{
//方法一
int[] aa = new int[] { 1, 2, 3, 4, 5 };
//方法二:
int[] bb = { 1, 2, 3, 4, 5 };
}
- 循环数组foreach(元素变量 临时变量名 in 集合或者集合名)
static void Main(string[] args)
{
int[] aa = new int[] { 1, 2, 3, 4, 5 };
//foreach(元素类型 变量名 in 集合或者数组名)
foreach (int i in aa)
{
Console.WriteLine(i);
}
}
3. 面向对象该思考的问题
image.png4. 类的定义规范
image.png- Student.cs
namespace ConsoleApp1
{
/// <summary>
/// 学员类
/// </summary>
class Student
{
//字段:ID(私有)
private int stuId;
//字段:姓名
private string stuName;
//属性:ID(公有)
public int StudId
{
get { return stuId; }
set { stuId = value; }
}
//属性:姓名
public string StuName
{
get { return stuName; }
set { stuName = value; }
}
//方法:获取学院信息
public void GetStudent()
{
string stuInfo = string.Format("姓名:{0},学号{1}.", StudId, StuName);
Console.WriteLine(stuInfo);
}
}
}
- Main.cs(无构造函数,需要类.属性来设置)
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//创建对象
Student stu1 = new Student();
//给属性赋值
stu1.StudId = 1;
stu1.StuName = "fxx";
//调用对象方法
stu1.GetStudent();
}
}
}
- 访问修饰符:
①private,成员变量,一般在类的内部做数据交互使用,一般使用private,使用小驼峰,通俗理解,私有方法和属性,就类似于人体内的器官和各种系统,只在我们人类的身体内交互;
②public,外部使用,接口,公共方法, - 字段和属性的作用
字段:主要用于内部交互,一般为private,如果需要提供给外部使用的时候,要封装成为属性,而不是公共字段
属性:一般用于外部提供数据,用于描述静态特征,一般是public,属性可以根据设计为 只读,只写,并且添加业务逻辑,提高数据安全
5. 类的方法
访问修饰符(private,public) 返回类型(void,string,int,对象,任何类型都可以当返回值) 方法名(数据类型 参数名1,数据类型 参数名2){
//方法
}
6. 构造方法
//加上这个就可用对象.属性来添加
public Student()
{
}
public Student(int id)
{
this.StudId = id;
}
public Student(int id, string name, int age):this(id)
{
this.StuName = name;
this.Age = age;
}
7. 对象初始化器
与构造方法的差别:
相同点:都可以完成对象的初始化
不同点:
①构造方法具有强制化,必须一一对应(希望用户初始化),初始化器没有强制性
②初始化器只能对属性进行初始化,而构造方法可以对任何需要初始化的工作进行,如对象创建是读取文件等
③初始化器在创建对象时候使用,而构造方法卸载类里面
image.png
- 这种方式必须知道参数的个数和参数的位置
Stedent stu = new Student(){
Birthday = Convert.ToDateTime("19910101"),
StudentId = 10005,
StudentName = "Lil Red"
}
8. 泛型集合List<T>
使用泛型集合只能添加一种类型的数据,数据取出后无需强制转换,拆箱
T是Type的简写, 表示可以是任何不确定的具体类型。
- Student.cs
namespace ConsoleApp1
{
class Student
{
public Student() { }
public Student(int id,string stuNam)
{
this.Id = id;
this.StuName = stuNam;
}
public int Id { get; set; }
public string StuName { get; set; }
}
}
- run.cs
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//创建多个学员对象
Student stu1 = new Student(1001,"fxx1");
Student stu2= new Student(1002,"fxx2");
Student stu3 = new Student(1003,"fxx3");
Student stu4 = new Student(1004,"fxx4");
Student stu5 = new Student(){};
//创建集合添加元素
List<Student> stuList = new List<Student>();
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
stuList.Add(stu4);
stuList.Add(new Student() {
StuName ="fxx5",
Id =1005
});
//获取元素个数
Console.WriteLine("集合内的元素个数是:{0}",stuList.Count);
//删除元素
stuList.RemoveAt(0);
stuList.Remove(stu5);
//遍历泛型集合
foreach (Student stu in stuList)
{
Console.WriteLine($"学员姓名:{stu.StuName},学员ID{stu.Id}");
}
}
}
}
8. 泛型字典Dictionary<K,V>
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//创建多个学员对象
Student stu1 = new Student(1001,"fxx1");
Student stu2= new Student(1002,"fxx2");
Student stu3 = new Student(1003,"fxx3");
Student stu4 = new Student(1004,"fxx4");
//创建字典
Dictionary<string, Student> stuDic = new Dictionary<string, Student>();
//添加成员
stuDic.Add("Fxx1", stu1);
stuDic.Add("Fxx2", stu2);
stuDic.Add("Fxx3", stu3);
stuDic.Add("Fxx4", stu4);
//遍历字典
foreach (KeyValuePair<string, Student> item in stuDic)
{
Console.WriteLine($"字典的Key是{item.Key},字段的值是{item.Value},值里的学生信息是{item.Value.StuName}");
}
}
}
}
9.集合排序
默认排序
普通类型的集合直接使用List.sort()
//普通集合
List<string> nameList = new List<string>() { "fxx2", "fxx1", "fxx3", "fxx5" };
//升序
nameList.Sort();
对象类型需要让排序的对象继承排序接口
- Student.cs
class Student : IComparable<Student>
{
public Student() { }
public Student(int id, string stuNam)
{
this.Id = id;
this.StuName = stuNam;
}
public int Id { get; set; }
public string StuName { get; set; }
//接口的实现接口
public int CompareTo(Student other)
{
//降序(other在前降序)
//return other.Id.CompareTo(this.Id);
//other在后,升序
return this.Id.CompareTo(other.Id);
}
}
- run.cs
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//创建多个学员对象
Student stu1 = new Student(1001,"fxx1");
Student stu2= new Student(1002,"fxx2");
Student stu3 = new Student(1003,"fxx3");
Student stu4 = new Student(1004,"fxx4");
//对象集合
List<Student> stuList = new List<Student>() { stu2, stu4, stu3, stu1 };
stuList.Sort();
foreach (Student item in stuList)
{
Console.WriteLine(item.Id+item.StuName);
}
}
}
}
对象动态排序
- 添加四个排序类
namespace ConsoleApp1
{
class Student
{
public Student() { }
public Student(int id,string stuNam)
{
this.Id = id;
this.StuName = stuNam;
}
public int Id { get; set; }
public string StuName { get; set; }
}
#region 四个排序类
//添加四个排序类
class StuNameAsc : IComparer<Student>
{
public int Compare(Student x, Student y)
{
//升序
return x.StuName.CompareTo(y.StuName);
}
}
class StuNameDesc : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return y.StuName.CompareTo(x.StuName);
}
}
class IdAsc : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.Id.CompareTo(y.Id);
}
}
class IdDesc
{
public int Compare(Student x, Student y)
{
return y.Id.CompareTo(x.Id);
}
}
#endregion
}
- 使用
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//创建多个学员对象
Student stu1 = new Student(1001,"fxx1");
Student stu2= new Student(1002,"fxx2");
Student stu3 = new Student(1003,"fxx3");
Student stu4 = new Student(1004,"fxx4");
//对象集合
List<Student> stuList = new List<Student>() { stu2, stu4, stu3, stu1 };
//姓名升序
stuList.Sort(new StuNameAsc());
foreach (Student item in stuList)
{
Console.WriteLine(item.Id+item.StuName);
}
//姓名降序
stuList.Sort(new StuNameDesc());
foreach (Student item in stuList)
{
Console.WriteLine(item.Id + item.StuName);
}
}
}
}
网友评论