学习C#的一点一滴(34)

序列化和反序列化

      Person p = new Person();
      p.Name = "张三";
      p.Gender = '男';
      p.Age = 22;
      using (FileStream fwrite = new FileStream(@"C:\Users\ASUS\Desktop\112.txt", FileMode.OpenOrCreate, FileAccess.Write))
      {
        //开始序列化对象
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fwrite, p);//序列化转换成二进制
      }
      Console.WriteLine("序列化成功");
      Console.ReadKey();

      接受对方发过来的二进制代码 然后反序列化成为对象
      Person p;
      using (FileStream fsread = new FileStream(@"C:\Users\ASUS\Desktop\112.txt", FileMode.OpenOrCreate, FileAccess.Read))
      {
        BinaryFormatter bf = new BinaryFormatter();
        p = (Person)bf.Deserialize(fsread);//反序列化成对象
      }
      Console.WriteLine(p.Name);
      Console.WriteLine(p.Gender);
      Console.WriteLine(p.Age);
      Console.ReadKey();

      序列化和反序列化的作用就是传输数据

    [Serializable]//指示一个类可以被序列化
    public class Person
    {
      private string _name;
      public string Name
      {
        get { return _name; }
        set { _name = value; }
      }
      private char _gender;
      public char Gender
      {
        get { return _gender; }
        set { _gender = value; }
      }
      private int _age;
      public int Age
      {
        get { return _age; }
        set { _age = value; }
      }

    }

接口

      //接口就是一个规范,能力
      //接口不能被实例化
      //student s = new student();//子类可以用父类里面的方法
      //s.CHLSS();
      //Console.ReadKey();
      person p = new student();
      p.CHLSS();//父类不能用子类里面的方法 也就是不能用扣篮

      Ikoulanable kou = new student();//父类new出来子类
      kou.koulan();
      Ikoulanable k = new Nba();
      k.koulan();//接口里面的方法  要和子类里面的方法 相同  子类里面必须要和接口里面的方法名字一样  比如说koulan

    public class person
    {
      public void CHLSS()
      {
        Console.WriteLine("吃屎吧你");
      }
    }
    public class Nba : Ikoulanable
    {
      public void koulan()
      {
        Console.WriteLine("扣篮");
      }
    }
    public class student : person, Ikoulanable//接口是干爹
    {
      //一个类继承一个接口必须实现这个接口中的成员
      public void koulan()
      {
        Console.WriteLine("我也可以扣篮");
      }
    }
    public interface Ikoulanable
    {
      //接口中不允许添加修饰符 默认就是public
      //不允许写有方法体的函数
      //接口中不能包含字段
      //允许方法和自动属性
      void koulan();
    }

接口的练级

      //麻雀会飞  鹦鹉会飞  鸵鸟不会飞   企鹅不会飞  直升机会飞
      //用多态来实现
      //虚方法、抽象类、接口
      IFlyable Fly = new yingwu();//new zhishengfeiji();//new Maque();
      Fly.Fly();
      ISpeakable Speak = new yingwu();
      Speak.Speak();
      bird B = new yingwu();
      B.CHIHELAS();
      Console.ReadKey();

//public class bird
    //{
    //  public double wings
    //  {
    //    get;
    //    set;
    //  }
    //  public void CHIHELAS()
    //  {
    //    Console.WriteLine("吃喝拉瑟");
    //  }
    //}
    //public class Maque : bird, IFlyable
    //{
    //  public void Fly()
    //  {
    //    Console.WriteLine("麻雀会飞");
    //  }
    //}
    //public class yingwu : bird, IFlyable, ISpeakable
    //{
    //  public void Fly()
    //  {
    //    Console.WriteLine("鹦鹉会飞");
    //  }

    //  public void Speak()
    //  {
    //    Console.WriteLine("鹦鹉会学人说话");
    //  }
    //}
    //public class tuoniao : bird
    //{

    //}
    //public class qie : bird
    //{

    //}
    //public class zhishengfeiji : IFlyable
    //{
    //  public void Fly()
    //  {
    //    Console.WriteLine("直升机会飞");
    //  }
    //}
    //public interface IFlyable
    //{
    //  void Fly();
    //}
    //public interface ISpeakable
    //{
    //  void Speak();
    //}

显示实现接口

      //接口中的 方法 有时候会和 类中的方法重名
      //为了解决调用的不知道是类里面的还是接口里面的方法这个问题  采取以下操作
      IFlyable Fly = new Bird();
      Fly.Fly();
      Bird F = new Bird();
      F.Fly();
      Console.ReadKey();

    public class Bird : IFlyable
    {
      public void Fly()
      {
        Console.WriteLine("鸟会飞");
      }
      /// <summary>
      /// 显示实现接口
      /// </summary>
      void IFlyable.Fly()
      {
        Console.WriteLine("接口会飞");
      }
    }
    public interface IFlyable
    {
      void Fly();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值