一、单例模式
1)、将构造函数私有化
2)、提供一个静态方法,返回一个对象
3)、创建一个单例
二、xml 可扩展的标记语言
xml: 存储数据
注意:
xml是严格区分大小写的。
xml标签也是成对出现的。
xml文裆有且只能有一个根节点
节点:
元素:
1.创建一个普通Xml
//通过代码来创建XL文档
//1、引用命名空间
//2、创建XML文档对象
XmlDocument doc=new XmlDocument();
//3.创建第一行描述信息,并添加到doc文档中
XmlDeclaration dec=doc.CreateXmlDeclaration("1.0","utf-8",null);
doc.AppendChild(dec);
//4.创建根节点
XmlElement books=doc.CreateElement("Books");
doc.AppendChild(books);
//5.给根节点Books创建子节点
XmlElement book1 = doc.CreateElement("Book");
books.AppendChild(book1);
//6.给Book1添加子节点
XmlElement name1 = doc.CreateElement("Name");
name1.InnerText = "三体";
book1.AppendChild(name1);
XmlElement price1 = doc.CreateElement("price");
price1.InnerText = "69.2";
book1.AppendChild(price1);
XmlElement des1 = doc.CreateElement("des");
des1.InnerText = "哈伦裤的";
book1.AppendChild(des1);
//给根节点Books创建子节点
XmlElement book2 = doc.CreateElement("Book");
books.AppendChild(book2);
//给Book2添加子节点
XmlElement name2 = doc.CreateElement("Name");
name2.InnerText = "三体2";
book2.AppendChild(name2);
XmlElement price2 = doc.CreateElement("price");
price2.InnerText = "6.2";
book2.AppendChild(price2);
XmlElement des2 = doc.CreateElement("des");
des2.InnerText = "dvdvf的";
book2.AppendChild(des2);
doc.Save("Books.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
2.创建一个带属性的XML
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
XmlElement order = doc.CreateElement("Order");
doc.AppendChild(order);
XmlElement customerName = doc.CreateElement("Customer");
customerName.InnerText = "haha";
order.AppendChild(customerName);
XmlElement customerNumber = doc.CreateElement("customerNumber");
customerNumber.InnerText = "100001";
order.AppendChild(customerNumber);
XmlElement items = doc.CreateElement("item");
order.AppendChild(items);
XmlElement orderItem1 = doc.CreateElement("orderItem");
//给节点添加属性
orderItem1.SetAttribute("Name", "zhangs");
orderItem1.SetAttribute("Count", "100");
items.AppendChild(orderItem1);
XmlElement orderItem2 = doc.CreateElement("orderItem");
//给节点添加属性
orderItem2.SetAttribute("Name", "lis ");
orderItem2.SetAttribute("Count", "1000");
items.AppendChild(orderItem2);
doc.Save("Order.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
3.追加XML
static void Main(string[] arg)
{
//追加XmL文档
XmlDocument doc = new XmlDocument();
XmlElement books;
if (File.Exists("Books.xml"))
{
//如果文件存在 加载XML
doc.Load("Books.xml");
//获得文件的根节点
books = doc.DocumentElement;
}
else
{
//如果文件不存在
//创建第一行
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","utf-8","null");
doc.AppendChild(dec);
//创建根结点
books = doc.CreateElement("Books");
doc.AppendChild(books);
}
//给根节点Books创建子节点
XmlElement book1 = doc.CreateElement("Book");
books.AppendChild(book1);
//6.给Book1添加子节点
XmlElement name1 = doc.CreateElement("Name");
name1.InnerText = "大河";
book1.AppendChild(name1);
XmlElement price1 = doc.CreateElement("price");
price1.InnerText = "66.2";
book1.AppendChild(price1);
XmlElement des1 = doc.CreateElement("des");
des1.InnerText = "景色的";
book1.AppendChild(des1);
doc.Save("Books.xml");
Console.WriteLine("追加成功");
Console.ReadKey();
}
4.读取XML
1)读取普通的
XmlDocument doc = new XmlDocument();
//加载要读取的xml
doc.Load("Books.xml");
//获得根节点
XmlElement books = doc.DocumentElement;
//获得子节点
XmlNodeList xl=books.ChildNodes;
foreach (XmlNode item in xl)
{
Console.WriteLine(item.InnerText);
}
Console.ReadKey();
2)读取带属性的
//Xpath
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNodeList xl=doc.SelectNodes("/Order/item/orderItem");
foreach (XmlNode node in xl)
{
Console.WriteLine(node.Attributes["Name"].Value);
Console.WriteLine(node.Attributes["Count"].Value);
}
Console.ReadKey();
5.删除XML
//Xpath
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNode xl=doc.SelectSingleNode("/Order/item");
xl.RemoveAll();
doc.Save("Order.xml");
Console.WriteLine("删除成功");
Console.ReadKey();
三、委托
1、为什么要使用委托
将一个方法作为参数传递给另一个方法。
2、委托概念
声明一个委托类型
委托所指向的函数必须跟委托具有相同的签名
public delegate string DelProStr(string name);
class WeiT
{
static void Main(String[] args)
{
//三个需求
//1、将—个字符串数组中每个元素都转换成大写
//2、将一个字符串数组中每个元素都转换成小写
//3、将一个字符串数组中每个元素两边都加上双引号
string[] names = { "dsFFsf", "dfgdg", "vsvFsss" };
// ProStrToUpper(names);
//ProStrToLower(names);
// ProStrSYH(names);
//ProStr(names, StrToUpper);
ProStr(names, StrToLower);
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
public static void ProStr(string[] name,DelProStr del)
{
for (int i = 0; i < name.Length; i++)
{
name[i] = del(name[i]);
}
}
public static string StrToUpper(string name)
{
return name.ToUpper();
}
public static string StrToLower(string name)
{
return name.ToLower();
}
public static string StrSYH(string name)
{
return "\""+name+"\"";
}
//public static void ProStrToUpper(String[] name)
//{
// for (int i = 0; i < name.Length; i++)
// {
// name[i] = name[i].ToUpper();
// }
//}
//public static void ProStrToLower(string[] name)
//{
// for (int i = 0; i < name.Length; i++)
// {
// name[i] = name[i].ToLower();
// }
//}
//public static void ProStrSYH(string[] name)
//{
// for (int i = 0; i < name.Length; i++)
// {
// name[i]="\""+name[i]+"\"";
// }
//}
}
3、匿名函数
没有名字的函数
public delegate void DelSay(string name);
class HHH
{
static void Main(string[] args)
{
// 1.SayHi("里斯", SayChinese);
// 2.DelSay del = SayChinese;
//3.DelSay del = delegate(String name)
//{
// Console.WriteLine("你好," + name);
//};
//del("掌声");
//lamda表达式 => goes to
DelSay del = (string name) => { Console.WriteLine("你好" + name); };
del("hello");
Console.ReadKey();
}
//public static void SayHi(string name,DelSay del)
//{
// del(name);
//}
//public static void SayChinese(String name)
//{
// Console.WriteLine("你好," + name);
//}
//public static void SayEnglish(string name)
//{
// Console.WriteLine("hello," + name);
//}
}
4、练习:使用委托求数组的最大值
5、练习:使用委托求任意数组的最大值
public delegate int DelCompare(Object o1,Object o2);
class Max
{
static void Main(string[] arg)
{
object[] o = { 1, 2, 3, 4, 5, 6 };
object[] o2 = { "dsf", "sfffkk", "ooooooooooo" };
//object result=GetMax(o2,Compare2);
object result = GetMax(o2, (Object o3, Object o4) =>
{
string s1 = (string)o3;
string s2 = (string)o4;
return s1.Length - s2.Length;
});
Console.WriteLine(result);
Console.ReadKey();
}
public static Object GetMax(Object[] nums,DelCompare del)
{
Object max=nums[0];
for (int i = 0; i < nums.Length; i++)
{
//要传一个比较的方法
if (del(max,nums[i])<0)
{
max = nums[i];
}
}
return max;
}
public static int Compare1(Object o1,Object o2){
int n1=(int) o1;
int n2=(int) o2;
return n1-n2;
}
public static int Compare2(Object o1, Object o2)
{
string s1 = (string)o1;
string s2 = (string)o2;
return s1.Length - s2.Length;
}
}
6、泛型委托
public delegate int DelCompare<T>(T t1,T t2);
class Fanxinh
{
static void Main(string[] arg)
{
int[] nums={1,2,3,5};
int Max = GetMax<int>(nums,Compare1);
string[] str = { "sfsdfds", "sdfsaaaa", "ieurwoheig" };
string Max1 = GetMax<string>(str, Compare2);
Console.WriteLine(Max1);
Console.ReadKey();
}
public static T GetMax<T>(T[] nums,DelCompare<T> del)
{
T max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
//要传一个比较的方法
if (del(max, nums[i]) < 0)
{
max = nums[i];
}
}
return max;
}
public static int Compare1(int n1,int n2)
{
return n1 - n2;
}
public static int Compare2(string s1, string s2)
{
return s1.Length - s2.Length;
}
}
7、多播委托
8、lamda表达式
public delegate void DelOne();
public delegate void DelTwo(string name);
public delegate string DelThree(string name);
class Mutil
{
static void Main(string[] arg)
{
DelOne del = () => { };//delegate() { };
DelTwo del2 = (string name) => { };//delegate(string name) { };
DelThree del3 = (string name) => { return name; };//delegate(string name) { return name; };
List<int> list = new List<int>() { 1, 9, 8, 7, 5, 3, 4, 6 };
list.RemoveAll(n => n > 4);
foreach (var item in list)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
9、使用委托来实现窗体传值。