C#---多态

一、委托与事件

C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。

委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。

事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。应用程序需要在事件发生时响应事件。

namespace Pratise
{
    //带一个参数并且无返回值的委托
    public delegate void SayHeyKKK(string name);
    class Program
    {
        //声明一个SayHeyKKK 类型的事件
        public static event SayHeyKKK ColEvent;
        static void Main(string[] args)
        {
            //委托的声明
            //委托的使用方法
            //将方法以变量的形式传递,并且以方法的形式执行
            SayHeyKKK hey = SayHey;
            hey += SayHey222;//注册顺序编译
            // hey -= SayHey222;//注销
            hey("rose");

            //匿名函数-----一次性的
            SayHeyKKK h2 = delegate(string name)
            {
                Console.WriteLine("{0}匿名函数",name);
            };
            h2("jokson");
            // lamda语句
            SayHeyKKK h3 = (string name) =>
            {
                Console.WriteLine("lamda语句{0}",name);
            };
            h3("sdads");

             //事件

            //注册事件
            ColEvent += bl_colEvent;
           // ColEvent -= bl_colEvent;//注销一个事件
            if (ColEvent != null)
            {
                ColEvent("logen");//调用事件
            }
            Console.ReadKey();
        }
        private static void bl_colEvent(string name)
        {
            Console.WriteLine("{0}是事件",name);
        }
        public static void SayHey(string name)
        {
            Console.WriteLine("hi {0},koko",name);
        }
        public static void SayHey222(string name)
        {
            Console.WriteLine("{0} 走吧走吧,", name);
        }
       
    }
}

二、list泛型集合

            //创建泛型集合
            List list=new List();
            list.Add(67);
            list.Add(64);
            list.Add(2);
            list.AddRange(new int[]{1,2,3,4,5,6});
            list.AddRange(list);
            //list泛型集合可以转换为数组
            int[] nums=list.ToArray();
           // List list22=  nums.ToList();//转换成集合

            char[] chs = new char[] { 'a', 'b', 'd' };
            List listChar = chs.ToList();
            //打印
            for (int i = 0; i < listChar.Count; i++)
            {
                Console.WriteLine(listChar[i]);
            }
            Console.ReadKey();

三、装箱和拆箱

装箱:就是将值类型转换为引用类型。
拆箱:将引用类型转换为值类型。

看两种类型是否发生了装箱或者拆箱,要看,这两种类型是否存在继承关系。

 //int n = 10;
            //object o = n;//装箱
            //int nn = (int)o;//拆箱
            //ArrayList list = new ArrayList();
            这个循环发生了100次装箱操作
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            //for (int i = 0; i < 100; i++)
            //{
            //    list.Add(i);
            //}
            //sw.Stop();
            //Console.WriteLine(sw.Elapsed);
            
            //这个地方没有发生拆箱或者插箱
            string str = "123";
            int n = Convert.ToInt32(str);

            //装箱
            int k = 10;
            IComparable i = k;
            Console.ReadKey();

四、字典集合

Dictionary<键值类型,数据类型> 变量 = new Dictionary<键值类型,数据类型>();
Dictionary dic = new Dictionary();

访问键值对: 一对对遍历键值和数据

            //泛型字典
            Dictionary dic = new Dictionary();
            dic.Add(1, "张三");
            dic.Add(2, "kukiu");
            dic.Add(3, "dsfs");
            dic[3] = "李四";
            //一对一对来遍历
            foreach (KeyValuePair kv in dic)
            {
                Console.WriteLine("{0}---{1}", kv.Key, kv.Value);
            }
            //根据键来查询
            //foreach (var item in dic.Keys)
            //{
            //    Console.WriteLine("{0}---{1}", item, dic[item]);
            //}
            Console.ReadKey();

练习

 class Program
    {
        static void Main(string[] args)
        {
            //1.将一个数组的奇数放到一个集合中,再将偶数放到另一个集合中
            //最终将两个集合合并为一个集合,并且奇数显示在左边偶数显示在右边。
            //int[] nums={1,2,3,4,5,6,7,8,9};
            //List listO = new List();
            //List listJ = new List();
            //for (int i = 0; i < nums.Length; i++)
            //{
            //    if(nums[i]%2==0){
            //       listO.Add(nums[i]);//存放在偶数集合
            //    }
            //    else
            //    {
            //        listJ.Add(nums[i]);//存放在奇数集合
            //    }
            //}
            //listJ.AddRange(listO);//把偶数集合转换到奇数集合
            //foreach (int item in listJ)
            //{
            //    Console.Write(item + " ");
            //}

            //2.提示用户输入一个字符串,通过foreach循环将用户输入的字符串赋值给一个字符数组
            //Console.WriteLine("请输入一个字符串");
            //string input=Console.ReadLine();
            //char[] chs = new char[input.Length];
            //int i = 0;
            //foreach (var item in input)
            //{
            //    chs[i] = item;
            //    i++;
            //}
            //foreach (var item in chs)
            //{
            //    Console.WriteLine(item);
            //}
           //3.统计welcome to china中每个字符出现的次数不考虑大小写
            string str = "welcome to china";
            //字符---------出现次数
            //键-----------值
            Dictionary dic = new Dictionary();

            for (int i = 0; i < str.Length; i++)
            {
                //空格
                if(str[i]==' '){
                    continue;
                }
                //如果dic已经包含了当前循环到的这个键
                if(dic.ContainsKey(str[i])){
                    //值再次加一
                    dic[str[i]]++;
                }
                else//这个字符在集合当中是第一次出现
                {
                    dic[str[i]] = 1;
                }
            }
            //打印
            foreach (KeyValuePair kv in dic)
            {
                Console.WriteLine("字母{0}出现了{1}次", kv.Key, kv.Value);
            }
            //w 1
            Console.ReadKey();
        }
    }

五、文件流读取文件
FileStream  操作字节的---可以操作任何文件
StreamReader和streamWriter 操作字符的----只能操作文本文件

 class Program
    {
        static void Main(string[] args)
        {

            使用FileStream来读取数据
            1.创建FileStream对象
            //FileStream fsRead = new FileStream(
            //                  @"C:\Users\Hey\Desktop\a.txt",//路径
            //                  FileMode.OpenOrCreate,
            //                  FileAccess.Read);
            //byte[] buffer = new byte[1024 * 1024 * 5];
            r 返回本次实际读取到的有效字节数
            //int r = fsRead.Read(buffer, 0, buffer.Length);

            将字节数组中每一个元素按照指定的编码格式解码成字符串
            //String s = System.Text.Encoding.UTF8.GetString(buffer, 0, r);
            关闭流
            //fsRead.Close();
            释放流所占用的资源
            //fsRead.Dispose();
            //Console.WriteLine(s);
            //Console.ReadKey();
            //WriteTxt();
            
            //使用FileStream 来写入数据
            using(FileStream fsWrite=new FileStream(@"C:\Users\Hey\Desktop\a.txt",FileMode.OpenOrCreate,FileAccess.Write))
            {
                string str = "可不可已覆盖";
                byte[] buffer = Encoding.UTF8.GetBytes(str);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            Console.WriteLine("写入成功");
            Console.ReadKey();
        }
        //写入数据
        private static bool WriteTxt() 
        {
            bool isResult = false;
            FileStream fs = new FileStream(@"C:\Users\Hey\Desktop\a.txt", FileMode.OpenOrCreate, FileAccess.Write); 
            try 
            {
                byte[] byteData = new byte[] { 65, 66 };
                fs.Write(byteData, 0, 2);
            }
            catch { }
            finally 
            {
                fs.Dispose();
            }
            //Console.ReadKey();
            return isResult;
        
        }
    }

使用StreamRead和StreamWriter读取文本文件

  //使用streamReader来读取一个文本文件
            using(StreamReader sr=new StreamReader(@"C:\Users\Hey\Desktop\a.txt"))
            {
                while (!sr.EndOfStream)
                {
                  Console.WriteLine( sr.ReadLine());
                }
               
            }
           // 使用StreamWriter来写入一个文本文件
            using (StreamWriter sw = new StreamWriter(@"C:\Users\Hey\Desktop\a.txt"))
            {
                sw.Write("jfkjjfhsk");
            }
            Console.WriteLine("写入ok");
            Console.ReadKey();

对多媒体文件的复制
    class CCopy
    {
        static void Main(string[] arg)
        {
            //思路︰就是先将要复制的多媒体文件读取出来,然后再写入到你指定的位置
            string source = @"C:\Users\Hey\Desktop\p4.jpg";
            string target = @"C:\Users\Hey\Desktop\new.jpg";
            CopyFile(source, target);
            Console.WriteLine("复制成功");
            Console.ReadKey();
        }
       
        public static void CopyFile(string source, string target)
        {
           //1.创建一个负责读取的流
            using (FileStream fsRead = new FileStream(source,FileMode.Open,FileAccess.Read))
            {
                //2.创建一个负责写入的流
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    //因为文件肯会比较大,所以我们在读取的时候 应该通过一个循环读取
                    while(true){
                         //返回本次读取数据实际大小
                        int r= fsRead.Read(buffer, 0, buffer.Length);
                        //如果返回一个0,也就意味着什么都没有读取到,读取完了
                        if (r == 0)
                        {
                            break;
                        }
                        //开始写入
                        fsWrite.Write(buffer,0,r);
                    }   
                }
            }
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值