PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,不保证每个都对。
C#笔记未按照难度排列
Part 1 接口
接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。接口使得实现接口的类或结构在形式上保持一致。
若在团队中,能够规范整个团队方法函数的命名规范。
也就是:接口定义了函数规则,派生类必须实现接口函数规则
命名: 权限 + interface + 接口名 { 约束方法 }
//接口:
//约束,对方法进行约束
//方法名相同,派生类必须实现该方法
//命名: 权限 + interface + 接口名 { 约束方法 }
//方法可以遵守多个 接口规则
//接口可以遵守多个接口,方法也要遵守接口
//索引器可以有多个,但参数得不同
//多个接口中有相同的方法名字时,子类实现一个即可
//接口父类 和 子类有同名的时候, 前面 + new
例如:
public interface IButton //定义了 IButton接口,主要有类 继承,则该类必须实现 该函数
{
void Click();
}
public interface IDrag : IButton //定义了IDrag接口,该接口又是IButton的派生,所以 继承IDrag时相当于同时继承了IButton
{
void Drag();
int this[int index] //可以有多个索引器,但参数不能一样
{
set;
get;
}
int this[string name]
{
set;
get;
}
}
测试类:
public class Girl : IDrag //遵守后必须实现接口方法,传递关系
{
int Age = 10;
int Number = 2;
public void Drag() //实现IDrag的Drag方法
{
Console.WriteLine("Girls Drag!");
}
public void Click() //实现IButton的Click方法
{
Console.WriteLine("Girls Click!");
}
public int this[int index] //实现IDrag的this[int index] 索引器方法
{
get
{
if (index == 0)
return Age;
else
return Number;
}
set
{
Age = value;
Number = value;
}
}
public int this[string name] //实现IDrag的this[string name] 索引器方法
{
get
{
if (name == "Age")
return Age;
else
return Number;
}
set
{
Age = value;
Number = value;
}
}
}
测试代码:
Girl tmpGirl = new Girl(); //实例化
tmpGirl.Click(); //调用Click函数
tmpGirl.Drag(); //调用Drag函数
Console.WriteLine("Age == {0} Number == {1}", tmpGirl[0], tmpGirl[1]);
tmpGirl[0] = 27;
tmpGirl[1] = 234;
Console.WriteLine("Age == {0} Number == {1}", tmpGirl[0], tmpGirl[1]);
Console.ReadLine();
输出结果为:
Part 2 委托:
C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。通过委托,在一定程度上能够降低程序的耦合度,不用通过应用实例化其他类,就可以使用其他类的函数
//委托: 方法指针
//delegate可以写在类里,写可以写在类外
public delegate void tmpDelegateOne();
public delegate void tmpDelegateTwo();
//权限 + delegate + 返回类型 + 名字
//引用类型
//降低耦合性
委托的命名:
public delegate void tmpDelegateOne();
public delegate void tmpDelegateTwo();
建立委托关系:
public tmpDelegateOne tmpDelegatA; //建立
tmpDelegatA = new tmpDelegateOne(Debug); //建立委托
tmpDelegatA(); //访问委托 Debug
例子:
public class PeopleA
{
public tmpDelegateOne tmpDelegatA; //建立
public void LendMoney()
{
//tmpDelegatA = new tmpDelegateOne(Debug); //建立委托
tmpDelegatA(); //访问委托
Console.WriteLine("PeopleA LendMoney");
}
public void Debug()
{
Console.WriteLine("This is the Deubug");
}
public PeopleA()
{
}
public PeopleA(tmpDelegateOne tmpDelegate)
{
this.tmpDelegatA = tmpDelegate;
tmpDelegatA(); //访问委托
}
}
public class PeopleB
{
PeopleA peopleA;
public tmpDelegateOne tmpDelegatA;
public void MakeMoney()
{
Console.WriteLine("PeopleB MakeMoney");
peopleA.LendMoney();
}
public void SadBadWordA()
{
Console.WriteLine("Say Bad Word A");
}
public void SadBadWordB()
{
Console.WriteLine("Say Bad Word B");
}
public PeopleB()
{
//实例化
peopleA = new PeopleA(SadBadWordA); //初始化,将PeopleA的委托指向SadBadWordA方法函数
peopleA.tmpDelegatA = new tmpDelegateOne(SadBadWordA);
peopleA.tmpDelegatA += SadBadWordB; //再SadBadWord的基础上 加上 SadBadWordTwo
// -=为去除 例如 wife.tmpDelegate -= SadBadWordTwo;
//先运行第一次的指向,随后再运行第二次的指向,可一直指下去
}
}
测试代码:
PeopleA peopleA = new PeopleA();
//peopleA.LendMoney(); //调用LendMoney函数方法
Console.WriteLine("===============================");
PeopleB peopleB = new PeopleB();
peopleB.MakeMoney();
Console.ReadLine();
测试结果:
Part 3 匿名方法:
匿名方法(Anonymous methods) 提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法。
在匿名方法中您不需要指定返回类型
定义方式为:
delegate { 方法 内容}
例如:
//实例化
peopleA = new PeopleA(delegate {
Console.WriteLine("This is delegate!");
}); //初始化,定义了一个 输出的 匿名方法
则调用后:
peopleB.MakeMoney();
输出结果为 :
Part 4 Lamda表达式:
Lamda表达式 与匿名方法相似,命名方法为:
() => { 方法内容 }
例如:
peopleA = new PeopleA( () => {
Console.WriteLine("Lamda 1 Test");
} ); //定义了一个输出 的Lamda表达式
运行后结果为: