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);
}
}
}