点击访问官网 class
修饰符 | 子类 | 外部 |
---|---|---|
public | √ | √ |
protected | √ | × |
private | × | × |
null
,值类型略default(类型)
查看任意类型的默认值[访问修饰符] class 类名
{// 特征 --- 成员变量// 行为 --- 成员方法// 保护特征 --- 成员属性// 构造函数 + 析构函数// 索引器// 运算符重载// 静态成员
}
class Person
{public string name;protected int age;private bool sex;
}
class Person
{public void Speak(string word){Console.WriteLine(word);}private int OneYearPass(){return this.age++;}
}
只能获取不能修改
or只能修改不能获取
帕斯卡命名法
;就是大写首字母访问修饰符 类型 属性名
{get{}set{}
}
class Person
{// 成员变量private int money;// 成员属性public int Money{get{// 写一些逻辑保护成员变量...// money -= 5;return money;}set{// 写一些逻辑保护成员变量...// value += 5;// value 用于表示 入参money = value;}}
}
class Person
{// 成员变量private int money;// 成员属性public int Money // 只能写不能读{private get // 不可读{return money;}set{money = value;}}
}
class Person
{// 成员变量private readonly int money;// 成员属性public int Money // 只读{get{return money;}}
}
class Person
{// 自动属性(类里并没有 money 属性)public int Money{get{return money;}}
}
public int Money
{get => money;set => money = value
}
public int Money{ set; get; }
//或者直接省略
public int Money;// Others
public int Money{ private set; get; }
public int Money{ set; private get; }
public int Money{ get { return money - 5; } }
public int Money{ set { money = value + 5; } }
public int Money{ get; }
class Person
{public Person(string name, int age, bool sex){this.name = name;this.age = age;this.sex = sex;}
}
:this(参数列表)
重用构造函数class Person
{public Person() { }public Person(string name):this() // 调了无参构造{this.name = name;}public Person(int age, bool sex){this.age = age;this.sex = sex;}public Person(string name, int age, bool sex): this(age, sex) // 调了上面的构造{this.name = name;}
}
C#
有自动垃圾回收机制GC(学过java的小伙伴应该知道)~类名(){}
*垃圾回收机制:
- 垃圾回收,英文简写 GC ( Garbage Co1 lector)
- 垃圾回收的过程是在遍历堆 (Heap)上动态分配的所有对象,通过识别它们是否被引用来确定哪些对象是垃圾,哪些对象仍要被使用
- 所谓的垃圾就是没有被任何变量、对象引用的内容;垃圾就需要被回收释放
- 垃圾回收有很多种算法,比如:
- 引用计数 ( Reference Counting)
- 标记清除 ( Mark Sweep)
- 标记整理 ( Mark Compact)
- 复制集合 ( Copy Co1 lection)
- 注意:
- GC只负责堆 (Heap)内存的垃圾回收
- 引用类型都是存在堆 (Heap)中的,所以它的分配和释放都通过垃圾回收机制来管理
- 栈 ( Stack)上的内存是由系统自动管理的
- 值类型在栈( Stack)中分配内存的,他们有自己的生命周期,不用对他们进行管理,会自动分配和释放
- C#中内存回收机制的大概原理
- 0代内存 1代内存 2代内存
- 代的概念
- 代是垃圾回收机制使用的一种算法 (分代算法)
- 新分配的对象都会被配置在第 θ代内存 中
- 每次分配都可能会进行垃圾回收以释放内存(0代内存满时)
- 在一次内存回收过程开始时,垃圾回收器会认为堆中全是垃圾,会进行以下两步
- 标记对象从根 ( 静态字段、方法参数 ) 开始检查引用对象,标记后为可达对象,未标记为不可达对象,不可达对象就认为是垃圾
- 搬迁对象压缩堆 ( 挂起执行托管代码线程 ) ,释放未标记的对象,搬迁可达对象,修改引用地址
- 大对象总被认为是第二代内存,目的是减少性能损耗,提高性能
- 不会对大对象进行搬迁压缩,85080字节(83kb)以上的对象为大对象
访问修饰符 返回类型 this[参数类型 参数名, 参数类型 参数名 ...]
{get{}set{}
}
class Person
{// 变量部分private Person[] friends;// 索引器public Person this[int index]{get { return friends[index]; }set { friends[index] = value; }}
}// 某方法内↓↓↓
Person p = new Person();
p[0] = new Person();
Console.WriteLine(p[0]);
class Person
{// 变量部分private Person[] friends;// 索引器public Person this[int index]{get { return friends[index >= 10 ? 9 : index]; }set { if (friends == null) {friends = new Person[10];}friends[index >= 10 ? 9 : index] = value; }}
}// 某方法内↓↓↓
Person p = new Person();
p[0] = new Person();
Console.WriteLine(p[0]);
class Person
{// 变量部分private Person[] friends;// 索引器public Person this[int index, string key] // 随便定义,反正逻辑如何也是可以随便写{get { return friends[index]; }set {if (friends == null) {friends = new Person[10];}friends[index] = value; }}
}// 某方法内↓↓↓
Person p = new Person();
p[0, ""] = new Person();
Console.WriteLine(p[0, ""]);
operator
public static 返回类型 operator 运算符(参数列表)
{// ...
}
class Point
{public int x;public int y;public Point(int x, int y){this.x = x;this.y = y;}// + 重载 1public static Point operator +(Point p1, Point p2) {return new Point(p1.x + p2.x, p1.y + p2.y);}// + 重载 2public static Point operator +(Point p, int add) {return new Point(p.x + add, p.y + add);}// + 重载 3public static Point operator +(int add, Point p) {return p + add;}
}// 某方法内↓↓↓
Console.WriteLine(new Point(1, 2) + new Point(2, 3));
<
就必须重载 >
static
修饰的 成员变量、成员属性、方法等 就称为静态成员*const 和 static 区别
- 常量可以理解为特殊的静态量
- 相同
- 都可以类名直接点出来
- 不同
- const 必须初始化,不能修改,static 随意
- const 只能修饰变量,static 随意
- const 必须写在访问修饰符后面,static 随意
class Yuan
{// 静态成员变量public static float PI = 3.14159226f;// 普通成员变量public float r;// 静态成员方法public static float Area(float r) {return PI * r * r;}// 普通成员方法public float DemoFun(){return PI * r * r;}
}// 某方法内↓↓↓
Console.WriteLine(Yuan.PI); // 3.141592
Console.WriteLine(Yuan.Area(10f)); // 314.1592
访问修饰符 static 返回类型 函数名(this 拓展类型 参数名, 参数类型 参数名, 参数类型 参数名 ...)
{// ...
}
// 拓展方法
static class Tools
{/// /// 为 int 拓展一个成员方法(实例化后才能使用)/// /// 使用该方法的实例对象/// 倍数public static void BaiDefineTime(this int value, int time) {Console.WriteLine("小白为 int 拓展的 SpeakValue 方法,当前值{0}的{1}倍为{2}", value,time, value * time);}
}// 某方法内↓↓↓
int i = 10;
i.BaiDefineTime();// "小白为 int 拓展的 SpeakValue 方法,当前值10的6倍为60"
class Person
{public string name;public int age;public Body body;// 内部类public class Body {Arm leftArm;Arm rightArm;// 内部类class Arm{ // ...}}
}// 某方法内↓↓↓
Person p = new Person();
// 使用可访问的内部类
Person.Body body = new Person.Body();
partial
partial class Person
{public string Name;
}partial class Person
{public int Age;public void Speak(){Console.WriteLine("我叫{0},我今年{1}岁了", Name, Age);}
}// 某方法内↓↓↓
Person p = new Person();
p.Name = "666";
p.Age = 18;
p.Speak();// "我叫666,我今年18岁了"