/// <summary>
/// 4.序列化与反序列化:在检视面板中可以看到的,就是被成功序列化的参数,与序列化相关的常用的关键字SerializeField,HideInInspector,NonSerialized,Serializable
/// 可以组合起来使用。。。注意:const,static不能被序列化
/// (1)SerializeField:表示变量可以被序列化,可以与private,protect相结合使用,使变量在检视面板中可视化,同时保护他的私有性和保护性
/// (2)HideInInspector:将原本显示在检视面板中的参数隐藏起来
/// (3)NonSerialized:不需要序列化
/// (4)Serializable:放在类前,序列化类
/// </summary>
///
[Serializable]
public class UnityTest4
{
public int ID;
public string name;
public UnityTest4() { }
public UnityTest4(int ID, string name) { this.ID = ID; this.name = name; }
public void OutPut()
{
Console.WriteLine(ID + " " + name);
}
public void WriteFile()
{
UnityTest4 u = new UnityTest4(1, "lili");
FileStream file = new FileStream("unitytest4.bin", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, u);
file.Close();
Console.WriteLine("write Done");
}
public void ReadFile()
{
FileStream file = new FileStream("unitytest4.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
UnityTest4 u = bf.Deserialize(file) as UnityTest4;
u.OutPut();
}
public void WriteXML()
{
UnityTest4 u = new UnityTest4(2, "lll");
FileStream file = new FileStream("unitytest4.xml", FileMode.OpenOrCreate);
XmlSerializer xml = new XmlSerializer(typeof(UnityTest4));//class必须共有
xml.Serialize(file, u);
file.Close();
Console.WriteLine("XML Done");
}
public void ReadXML()
{
FileStream file = new FileStream("unitytest4.xml", FileMode.Open);
XmlSerializer xml = new XmlSerializer(typeof(UnityTest4));
UnityTest4 u = xml.Deserialize(file) as UnityTest4;
u.OutPut();
}
}
/// <summary>
/// 委托、事件、匿名函数、反射、特性
/// </summary>
///
public delegate void _mydelegate(string name);
class UnityTest5
{
public void CallBack(_mydelegate c)
{
//委托调用
c = new _mydelegate(YouFunc);
c("hdkjdsnasd");
}
public void CallBack2()
{
//委托的多播,绑定
_mydelegate c = new _mydelegate(YouFunc);
c += ContineFunc;
c("dsaldas");
}
public void Call(_mydelegate c)
{
//匿名函数
c = delegate (string name) { Console.WriteLine(name); };
}
public void YouFunc(string name)
{
Console.WriteLine(name + "你妈喊你回家吃饭");
}
public void ContineFunc(string str)
{
Console.WriteLine("\n" + "吃完饭之后,继续出来浪" + str);
}
public void DohomeWork(string str)
{
Console.WriteLine("\n" + "没写完作业就甭出来了");
}
}
class UnityTest6
{
public delegate void eventDelegate(string name);
public event eventDelegate callevent;
public UnityTest6() { }
public void EventChange(string name)
{
if (callevent != null)
{
callevent(name);
} else
Console.WriteLine("event is null");
}
public void Done(int n)
{
if (n == 5)
{
string str = "dasdkasda";
EventChange(str);
}
}
}
class Devet
{
public void print(string name)
{
Console.WriteLine("event fir"+name);
}
}
class EventDone
{
}
//涉及到虚函数的继承,虚函数寻址
class A
{
int a;
int b;
public A() { }
public A(int a, int b) { this.a = a;this.b = b; }
virtual public void PrintA()
{
Console.WriteLine("aaaaaaaaaa "+a);
}
virtual public void Print()
{
Console.WriteLine("bbbbbbbbbb "+b);
}
}
class B : A
{
int c;
public B() { }
public B(int a,int b,int c):base(a,b) { this.c = c; }
override public void PrintA()
{
Console.WriteLine("BBBBBBBB ");
}
override public void Print()
{
Console.WriteLine("BBBBBBBBBBBBBB ");
}
public void PrintC()
{
Console.WriteLine("cccccccccccc "+c);
}
}
01-05
01-27