派生类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using ClassLibrary1.util; using ClassLibrary1.util.util2; namespace ClassLibrary1 { //声明代理类 public delegate void MyEvent(); public class Class1 { //enum是C#的关键字,定义枚举类型, Enum 是C#的一个类 enum color:int {red=1, blue=2, white=3, gray=4, yellow=5}; static void Main(string[] args) { //ReadOrWriteFile(); //BaseTest(); //TestEnum(); //ExceptionDeal(); //varStringIsNull(); // DeleGateTest(); Class1 c = new Class1(); c.TestEvent(); Console.ReadKey(); } /// <summary> /// 类,抽象类以及接口 /// </summary> private static void varStringIsNull() { //String s = ""; //bool b = StringUtil.IsNullOrEmpty(s); //Console.WriteLine(b); //Cars mycar = new Cars(); //mycar.Name1 = "BMW"; //mycar.Number1 = "888888"; //Console.WriteLine("我是{0},牌号为{1}.", mycar.Name1, mycar.Number1); //基类和派生类 //Bmw bmw = new Bmw(); //bmw.Name1 = "BMW"; //bmw.Number1 = "888888"; //Console.WriteLine("我是{0},牌号为{1}.", bmw.Name1, bmw.Number1); //bmw.Country = "Germany"; //Console.WriteLine("生产地:{0}", bmw.Country); //Console.WriteLine(bmw.getReturnValue()); //抽象类 和 接口 Bmw bmw = new Bmw(); bmw.Name1 = "BMW"; bmw.Number1 = "888888"; Console.WriteLine("我是{0},牌号为{1}.", bmw.Name1, bmw.Number1); bmw.Country = "Germany"; bmw.Count = 4; Console.WriteLine("生产地:{0}", bmw.Country); Console.WriteLine(bmw.getReturnValue()); Console.WriteLine("轮子数目:{0}", bmw.getWheelCount()); bmw.createCar(); bmw.useCar(); bmw.destroyCar(); } /// <summary> /// 声明一个委托类 /// </summary> public delegate void MyDel(String msg); /// <summary> /// 声明调用委托 /// </summary> /// <param name="msg"></param> public static void DelMessage(String msg) { Console.WriteLine(msg); } /// <summary> /// 委托测试方法 /// </summary> public static void DeleGateTest() { MyDel del = DelMessage; del("I am a delegate."); } public event MyEvent DelEvent;//定义事件,绑定代理 /// <summary> /// 事件触发方法 /// </summary> public virtual void FireEvent() { if (DelEvent != null) { DelEvent(); } } /// <summary> /// 事件测试方法 /// </summary> public void TestEvent() { DelEvent += new MyEvent(Test2Event);//注册 FireEvent();//触发事件 } /// <summary> /// 事件处理函数 /// </summary> public void Test2Event() { Console.WriteLine("事件处理"); } /// <summary> /// 捕获异常 /// </summary> private static void ExceptionDeal() { int x = 1; int y = 0; try { int a = x / y; } catch (Exception e) { Console.WriteLine(e.ToString()); } } /// <summary> /// 枚举类型 /// </summary> private static void TestEnum() { Console.WriteLine(color.blue); color c = color.red; switch (c) { case color.blue: Console.WriteLine(color.blue); break; case color.gray: Console.WriteLine(color.gray); break; default: Console.WriteLine("other"); break; } String[] s = { "a", "b" }; foreach (String e in s) { Console.WriteLine(e); } } /// <summary> /// 基础测试 /// </summary> private static void BaseTest() { //float f = 12.23F; //long a = 1266666777777777863L; //String str = @"D:\files\temp\a.txt\t";//逐字符串:按原样输出 //String str1 = "D:\files\temp\a.txt\t"; //String str2 = @""""; //String str3 = """";编译时报错 //Console.WriteLine(f); //Console.WriteLine(a); //Console.WriteLine("\v"); //Console.WriteLine(str); //Console.WriteLine(str1); //Console.WriteLine(str2); //Console.WriteLine(str3); //String str = "XiaoLi"; //Console.WriteLine("My name is {0}, how are you?", str);//字符串格式化, 参数化 //字符串比较,可以直接用==号,也可以用Equals //String s1 = "hello,world"; //String s2 = "hello,world"; //if (s1 == s2) //{ // Console.WriteLine("相等"); //} //if (s1.Equals(s2)) //{ // Console.WriteLine("相等"); //} //else //{ // Console.WriteLine("不相等"); //} //值比较, 也可判断是否指向同一对象 //String s1 = "hello,world"; //String s2 = "hello,wor2ld"; //if (s1.CompareTo(s2) <= 0) //{ // Console.WriteLine("指向同一对象"); //} //else //{ // Console.WriteLine("不是指向同一对象"); //} } /// <summary> /// 读写文件 /// </summary> private static void ReadOrWriteFile() { //@ string path = @"e:\MyTest.txt"; // Delete the file if it exists. if (File.Exists(path)) { File.Delete(path); } //Create the file. //using using (FileStream fs = File.Create(path)) { AddText(fs, "This is some text"); AddText(fs, "This is some more text,"); AddText(fs, "\r\nand this is on a new line"); AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i = 1; i < 120; i++) { AddText(fs, Convert.ToChar(i).ToString()); //Split the output at every 10th character. if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) { AddText(fs, "\r\n"); } } } //Open the stream and read it back. using (FileStream fs = File.OpenRead(path)) { byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while (fs.Read(b, 0, b.Length) > 0) { Console.WriteLine(temp.GetString(b)); } } } /// <summary> /// 写文件 /// </summary> /// <param name="fs"></param> /// <param name="value"></param> private static void AddText(FileStream fs, string value) { byte[] info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, 0, info.Length); } } }
工具类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 6 namespace ClassLibrary1.util 7 { 8 class StringUtil 9 { 10 public static bool IsNullOrEmpty(String str) 11 { 12 if (str == null || "".Equals(str)) return true; 13 return false; 14 } 15 } 16 }
基类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using ClassLibrary1.util.util2; 6 7 namespace ClassLibrary1.util.util2 8 { 9 class Cars:Wheels 10 { 11 private String Name; 12 13 public String Name1 14 { 15 get { return Name; } 16 set { Name = value; } 17 } 18 private String Number; 19 20 public String Number1 21 { 22 get { return Number; } 23 set { Number = value; } 24 } 25 public Cars() 26 { 27 Console.WriteLine("调用基类构造方法:{0}", DateTime.Now); 28 } 29 } 30 }
派生类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using ClassLibrary1.util.util2; 6 7 namespace ClassLibrary1.util.util2 8 { 9 class Bmw:Cars,MyObjects 10 { 11 private String country; 12 13 public String Country 14 { 15 get { return country; } 16 set { country = value; } 17 } 18 public Bmw() 19 { 20 Console.WriteLine("调用派生类构造方法:{0}", DateTime.Now); 21 } 22 public String getReturnValue() 23 { 24 return Name1 + Number1; 25 26 } 27 28 #region MyObjects 成员 29 30 public void createCar() 31 { 32 Console.WriteLine("创建车。"); 33 } 34 35 public void useCar() 36 { 37 Console.WriteLine("车使用中..."); 38 } 39 40 public void destroyCar() 41 { 42 Console.WriteLine("销毁车。"); 43 } 44 45 #endregion 46 } 47 }
接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ClassLibrary1.util.util2 7 { 8 interface MyObjects 9 { 10 void createCar(); 11 void useCar(); 12 void destroyCar(); 13 14 } 15 }