面向对象继承---复习:包含继承关系,里氏转换,ArrayList与Hashtable集合,文件处理

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 面向对象继承_复习
{
    class Program
    {
        static void Main(string[] args)
        {
            //里氏转换:1、子类可以赋值给父类(如果一个地方需要父类作为参数,我们可以传递一个子类对象)
            //2、如果父类中装的是子类对象,则可以将这个父类强转为子类对象

            Person p = new Student();
            //is  as
            Student ss = p as Student;//as 是先判断,如果能转换,那就转换过去,如果不能,那就返回一个空
            ss.StudentSayHi();

            if (p is Student)//is 是仅作为判断,返回bool值,true或者false
            {
                ((Student)p).StudentSayHi();
            }
            else
            {
                Console.WriteLine("转换失败");
            }
            


            ArrayList list = new ArrayList();
            //Remove RemoveAt RemoveRange Clear Insert InsertRange
            //Reverse Sort
            Hashtable ht = new Hashtable();
            ht.Add(1, "张三");
            ht.Add(2, '');
            ht.Add(3, 50000m);
            //在键值对集合中,键必须是唯一的
            //ht.Add(1,"李四")
            ht[1] = "李四";
            //ht.ContainsKey 这个判断很重要,判断是否有键,键不可重复,值可以重复
            foreach (var item in ht.Keys)
            {
                Console.WriteLine("{0},{1}", item, ht[item]);
            }
            

            //Path类
            //File类
            //Create Delete Copy Move
            byte[] buffer = File.ReadAllBytes(@"F:\程序测试文件夹\new.txt");
            //将字节数组中的每一个元素都 要按照我们指定的编码格式解码成字符串
            //UTF-8  GB2312 GBK Unicode
            string s = Encoding.GetEncoding("UTF-8").GetString(buffer);
            Console.WriteLine(s);

            //没有这个文件的时候创建一个,有的话就覆盖掉
            string str = "今天天气好晴朗,处处好风光";
            //需要将字节转换成字节数组
            byte[] buffer2 = Encoding.Default.GetBytes(str);
            File.WriteAllBytes(@"F:\程序测试文件夹\ceshi.txt",buffer2);
            Console.WriteLine("写入成功");

            Console.ReadKey();
        }
    }
    public class Person
    {
        public void PersonSayHi()
        {
            Console.WriteLine("我是人类");
        }
    }
    public class Student : Person
    {
        public void StudentSayHi()
        {
            Console.WriteLine("我是学生");
        }
    }
}

 

posted @ 2021-07-11 16:56  静态类  阅读(32)  评论(0编辑  收藏  举报