一、类、对象与实例
每个对象都是某个类的一个实例
创建一个对象的过程就是实例化,实例化的产物为实例、或叫做对象
static void Main(srting[] args)
{
Point a = new Point();
a.x = 15;
a.y = 20;
DrawPoint(a);
return;
}
public class Point
{
public int x;
public int y;
}
public static void DrawPoint(Point point) =>
Console.WriteLine($"左边点为x:{point.x}, y:{point.y}");
二、对象聚合
高内聚,低耦合
static void Main(srting[] args)
{
Point a = new Point();
a.x = 15;
a.y = 20;
a.DrawPoint();
Point.b = new Point();
b.x = 12;
b.y = 30;
a.GetDistance(b);
Console.Read();
return;
}
public class Point
{
public int x;
public int y;
public void DrawPoint() =>
Console.WriteLine($"左边点为x:{x}, y:{y}");
public double GrtDistance(Point p) =>
Math.Pow(x - p.x, 2) + Maath.Pow(y - p.y, 2);
}
三、构造方法与方法重载
static void Main(srting[] args)
{
Point a = new Point(15, 10);
a.DrawPoint();
Point.b = new Point(22, 10);
a.GetDistance(b);
Console.Read();
return;
}
public class Point
{
public int x;
public int y;
public Point()//构造方法
{
this.x = 15;
this.y = 10;
}
public Point(int x, int y)//方法重载1
{
this.x = x;
this.y = y;
}
public Point(int x)//方法重载2
{
this.x = x;
this.y = 20;
}
public void DrawPoint() =>
Console.WriteLine($"左边点为x:{x}, y:{y}");
public double GrtDistance(Point p) =>
Math.Pow(x - p.x, 2) + Maath.Pow(y - p.y, 2);
}
四、字段、属性与封装
当对x,y进行private保护时,如果需要对x的值进行检查(例如不能为负数)
static void Main(srting[] args)
{
Point a = new Point(15, 10);
a.DrawPoint();
Point.b = new Point(22, 10);
a.GetDistance(b);
Point point = new Point(25, 50);
point.SetX(30);
Console.Read();
return;
}
public class Point
{
private int x;
private int y;
//封装x,y
public void SetX(int value)
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,x = value;
}
public int GetX()=> this,x;
public void SetY(int value)
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,y = value;
}
public int GetY()=> this,y;
public Point()//构造方法
{
this.x = 15;
this.y = 10;
}
public Point(int x, int y)//方法重载1
{
this.x = x;
this.y = y;
}
public Point(int x)//方法重载2
{
this.x = x;
this.y = 20;
}
public void DrawPoint() =>
Console.WriteLine($"左边点为x:{x}, y:{y}");
public double GrtDistance(Point p) =>
Math.Pow(x - p.x, 2) + Maath.Pow(y - p.y, 2);
}
使用属性对代码进行简化,使用“getter,setter懒人包”
static void Main(srting[] args)
{
Point a = new Point(15, 10);
a.DrawPoint();
Point.b = new Point(22, 10);
a.GetDistance(b);
Point point = new Point(25, 50);
point.X = 30;
Console.Read();
return;
}
public class Point
{
//定义属性x
private int x;
public int X{get{ruturn this.x;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,x = value;
}
};
//定义属性y
private int y;
public int Y{get{ruturn this.y;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,y = value;
}
};
//定义属性z
public int Z{get; set;}
public Point()//构造方法
{
this.x = 15;
this.y = 10;
}
public Point(int x, int y)//方法重载1
{
this.x = x;
this.y = y;
}
public Point(int x)//方法重载2
{
this.x = x;
this.y = 20;
}
public void DrawPoint() =>
Console.WriteLine($"左边点为x:{x}, y:{y}");
public double GrtDistance(Point p) =>
Math.Pow(x - p.x, 2) + Maath.Pow(y - p.y, 2);
}
五、常量const、只读read-only与只写write-only
static void Main(srting[] args)
{
Point a = new Point(15, 10);
a.DrawPoint();
Point.b = new Point(22, 10);
a.GetDistance(b);
Point point = new Point(25, 50);
point.X = 30;
point.T;
Console.Read();
return;
}
public class Point
{
//定义属性x
private int x;
public int X{get{ruturn this.x;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,x = value;
}
};
//定义属性y
private int y;
public int Y{get{ruturn this.y;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,y = value;
}
};
//定义属性z
public int Z{get; set;}
//时间维度,只读
public int T{get;} = 5;
//第五维,sigularity,只写
private int s;
public int S{ set{ s = value; } }
//第六维,常量
const int a = 99;
//第七维,readonly
public readonly int b;
public Point()//构造方法
{
this.x = 15;
this.y = 10;
b = 1000;
}
public Point(int x, int y)//方法重载1
{
this.x = x;
this.y = y;
b = 999;
}
public Point(int x)//方法重载2
{
this.x = x;
this.y = 20;
}
public void DrawPoint() =>
Console.WriteLine($"左边点为x:{x}, y:{y}");
public double GrtDistance(Point p) =>
Math.Pow(x - p.x, 2) + Maath.Pow(y - p.y, 2);
}
六、索引(Index)和范围(Range)
类索引(Index)的语法结构
访问修饰符 声明修饰符 类型 this[参数]{
get;
set;
}
static void Main(srting[] args)
{
Point a = new Point(15, 10);
a.DrawPoint();
Point.b = new Point(22, 10);
a.GetDistance(b);
Point point = new Point(25, 50);
point.X = 30;
point.T;
//index索引
string[] words = new string[]
{ //index from start index from end
"The", //0 ^9
"quick", //1 ^8
"brown", //2 ^7
"fox", //3 ^6
"jumped", //4 ^5
"over", //5 ^4
"the", //6 ^3
"lazy", //7 ^2
"dog" //8 ^1
};
Index k = ^1;
Concole.Write(words[1]);
//Concole.Write(words[words.Length - 1]);
Concole.Write(words[^1]);
//range[开始位置..结束位置]
Range p = 0..3;
var list = words[0..3];
//point 索引
Console,WriteLine(point[0]);//gamma[0],"The"
point[0] = "hello"; //可以进行修正
Console,WriteLine(point["dog"]);//查找dog位置
Console.Read();
return;
}
public class Point
{
//定义属性x
private int x;
public int X{get{ruturn this.x;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,x = value;
}
};
//定义属性y
private int y;
public int Y{get{ruturn this.y;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,y = value;
}
};
//定义属性z
public int Z{get; set;}
//时间维度,只读
public int T{get;} = 5;
//第五维,sigularity,只写
private int s;
public int S{ set{ s = value; } }
//第六维,常量
const int a = 99;
//第七维,readonly
public readonly int b;
//第八维,索引
string[] gamma= new string[]
{ //index from start index from end
"The", //0 ^9
"quick", //1 ^8
"brown", //2 ^7
"fox", //3 ^6
"jumped", //4 ^5
"over", //5 ^4
"the", //6 ^3
"lazy", //7 ^2
"dog" //8 ^1
};
public string this[int index]
{
get{ return gamma[index]; }
set{ gamma[index] = value; }
}
public int this[string str]//索引重载
{
get{ return Array.IndexOf(gamma, str); }
}
public Point()//构造方法
{
this.x = 15;
this.y = 10;
b = 1000;
}
public Point(int x, int y)//方法重载1
{
this.x = x;
this.y = y;
b = 999;
}
public Point(int x)//方法重载2
{
this.x = x;
this.y = 20;
}
public void DrawPoint() =>
Console.WriteLine($"左边点为x:{x}, y:{y}");
public double GrtDistance(Point p) =>
Math.Pow(x - p.x, 2) + Maath.Pow(y - p.y, 2);
}
要点
- [创建索引需要使用this关键词 ]
- [ 索引可以用于快速访问一组数据的某一项]
- [索引的使用需要使用方括号 ]
- [索引不能使用static、ref和out来修饰 ]
- [ 索引可以被重载]
七、Partial Class 局部类
static void Main(srting[] args)
{
Point a = new Point(15, 10);
a.DrawPoint();
Point.b = new Point(22, 10);
a.GetDistance(b);
Point point = new Point(25, 50);
point.X = 30;
point.T;
//index索引
string[] words = new string[]
{ //index from start index from end
"The", //0 ^9
"quick", //1 ^8
"brown", //2 ^7
"fox", //3 ^6
"jumped", //4 ^5
"over", //5 ^4
"the", //6 ^3
"lazy", //7 ^2
"dog" //8 ^1
};
Index k = ^1;
Concole.Write(words[1]);
//Concole.Write(words[words.Length - 1]);
Concole.Write(words[^1]);
//range[开始位置..结束位置]
Range p = 0..3;
var list = words[0..3];
//point 索引
Console,WriteLine(point[0]);//gamma[0],"The"
point[0] = "hello"; //可以进行修正
Console,WriteLine(point["dog"]);//查找dog位置
point.Delta = 15;
point.PrintDelta();
Console.Read();
return;
}
Point.cs
public partial class Point
{
//定义属性x
private int x;
public int X{get{ruturn this.x;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,x = value;
}
};
//定义属性y
private int y;
public int Y{get{ruturn this.y;}
set
{
if(value < 0)
{
throw new Exception("value不能小于0");
}
this,y = value;
}
};
//定义属性z
public int Z{get; set;}
//时间维度,只读
public int T{get;} = 5;
//第五维,sigularity,只写
private int s;
public int S{ set{ s = value; } }
//第六维,常量
const int a = 99;
//第七维,readonly
public readonly int b;
//第八维,索引
string[] gamma= new string[]
{ //index from start index from end
"The", //0 ^9
"quick", //1 ^8
"brown", //2 ^7
"fox", //3 ^6
"jumped", //4 ^5
"over", //5 ^4
"the", //6 ^3
"lazy", //7 ^2
"dog" //8 ^1
};
public string this[int index]
{
get{ return gamma[index]; }
set{ gamma[index] = value; }
}
public int this[string str]//索引重载
{
get{ return Array.IndexOf(gamma, str); }
}
public Point()//构造方法
{
this.x = 15;
this.y = 10;
b = 1000;
}
public Point(int x, int y)//方法重载1
{
this.x = x;
this.y = y;
b = 999;
}
public Point(int x)//方法重载2
{
this.x = x;
this.y = 20;
}
public void DrawPoint() =>
Console.WriteLine($"左边点为x:{x}, y:{y}");
public double GrtDistance(Point p) =>
Math.Pow(x - p.x, 2) + Maath.Pow(y - p.y, 2);
}
Point.Delta.cs
//partial 局部point 类
public partial class Point
{
//第九维度
public int Delta{ get; set; }
public void PrintDelta()
{
Console.WriteLine("此处为第九维");
}
}
局部类的适用范围
- [类型特别大,不适合放在一个文件中实现 ]
- [自动生成的代码,不宜与我们自己编写的代码混合在一起 ]
- [ 一个类同时需要多个人同时编写]
注意事项
- [只适用于类、接口、结构,不支持委托和枚举 ]
- [必须有修饰符partial ]
- [必须位于相同的命名空间中 ]
- [ 必须同时编译]
- [各部分的访问修饰符 必须一致 ]
- [ 累加效应]