• 多态、接口


    1、c#中的访问修饰符

    public :公开的公共的

    private:私有的,只能在当前类的内部访问

    protected:受保护的,只能在当前类的内部以及该类的子类中访问。

    internal:只能在当前项目中访问。在同一个项目中,internal和public的权限是一样。

    protected internal:protected+internal

    1)、能够修饰类的访问修饰符只有两个:public、internal。

    2)、可访问性不一致。 子类的访问权限不能高于父类的访问权限,会暴漏父类的成员。

    2、设计模式 设计这个项目的一种方式。

    3、简单工厂设计模式

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _03简单工厂设计模式
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine("请输入您想要的笔记本品牌");
    14             string brand = Console.ReadLine();
    15             NoteBook nb = GetNoteBook(brand);
    16             nb.SayHello();
    17             Console.ReadKey();
    18         }
    19 
    20 
    21         /// <summary>
    22         /// 简单工厂的核心 根据用户的输入创建对象赋值给父类
    23         /// </summary>
    24         /// <param name="brand"></param>
    25         /// <returns></returns>
    26         public static NoteBook GetNoteBook(string brand)
    27         {
    28             NoteBook nb = null;
    29             switch (brand)
    30             {
    31                 case "Lenovo": nb = new Lenovo();
    32                     break;
    33                 case "IBM": nb = new IBM();
    34                     break;
    35                 case "Acer": nb = new Acer();
    36                     break;
    37                 case "Dell": nb = new Dell();
    38                     break;
    39             }
    40             return nb;
    41         }
    42     }
    43 
    44     public abstract class NoteBook
    45     {
    46         public abstract void SayHello();
    47     }
    48 
    49     public class Lenovo : NoteBook
    50     {
    51         public override void SayHello()
    52         {
    53             Console.WriteLine("我是联想笔记本,你联想也别想");
    54         }
    55     }
    56 
    57 
    58     public class Acer : NoteBook
    59     {
    60         public override void SayHello()
    61         {
    62             Console.WriteLine("我是鸿基笔记本");
    63         }
    64     }
    65 
    66     public class Dell : NoteBook
    67     {
    68         public override void SayHello()
    69         {
    70             Console.WriteLine("我是戴尔笔记本");
    71         }
    72     }
    73 
    74     public class IBM : NoteBook
    75     {
    76         public override void SayHello()
    77         {
    78             Console.WriteLine("我是IBM笔记本");
    79         }
    80     }
    81 }
    View Code

    4、值类型在复制的时候,传递的是这个值得本身。    引用类型在复制的时候,传递的是对这个对象的引用。   

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _04值类型和引用类型
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //值类型:int double char decimal bool enum struct 
    14             //引用类型:string 数组  自定义类 集合 object 接口
    15 
    16             //值传递和引用传递
    17             //int n1 = 10;
    18             //int n2 = n1;
    19             //n2 = 20;
    20             //Console.WriteLine(n1);
    21             //Console.WriteLine(n2);
    22             //Console.ReadKey();
    23 
    24             //Person p1 = new Person();
    25             //p1.Name = "张三";
    26             //Person p2 = p1;
    27             //p2.Name = "李四";
    28             //Console.WriteLine(p1.Name);
    29             //Console.ReadKey();
    30 
    31             //Person p = new Person();
    32             //p.Name = "张三";
    33             //Test(p);
    34             //Console.WriteLine(p.Name);
    35             //Console.ReadKey();
    36 
    37             //string s1 = "张三";
    38             //string s2 = s1;
    39             //s2 = "李四";
    40             //Console.WriteLine(s1);
    41             //Console.WriteLine(s2);
    42             //Console.ReadKey();
    43 
    44             int number = 10;
    45             TestTwo(ref  number);
    46             Console.WriteLine(number);
    47             Console.ReadKey();
    48         }
    49         //int n=number;
    50         public static void TestTwo(ref  int n)
    51         {
    52             n += 10;
    53         }
    54 
    55         //Person pp=p;
    56         public static void Test(Person pp)
    57         {
    58             Person p = pp;
    59             p.Name = "李四";
    60         }
    61     }
    62 
    63     public class Person
    64     {
    65         private string _name;
    66         public string Name
    67         {
    68             get { return _name; }
    69             set { _name = value; }
    70         }
    71     }
    72 }
    View Code

    5、序列化:就是将对象转换为二进制   

    反序列化:就是将二进制转换为对象   

    作用:传输数据。  

    序列化:   1)、将这个类标记为可以被序列化的。    

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.IO;
     7 using System.Runtime.Serialization.Formatters.Binary;
     8 namespace _05序列化和反序列化
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             //要将p这个对象 传输给对方电脑
    15             //Person p = new Person();
    16             //p.Name = "张三";
    17             //p.Age = 19;
    18             //p.Gender = '男';
    19             //using (FileStream fsWrite = new FileStream(@"C:UsersSpringRainDesktop111.txt", FileMode.OpenOrCreate, FileAccess.Write))
    20             //{ 
    21             //    //开始序列化对象
    22             //    BinaryFormatter bf = new BinaryFormatter();
    23             //    bf.Serialize(fsWrite, p);
    24             //}
    25             //Console.WriteLine("序列化成功");
    26             //Console.ReadKey();
    27 
    28             //接收对方发送过来的二进制 反序列化成对象
    29             Person p;
    30             using (FileStream fsRead = new FileStream(@"C:UsersSpringRainDesktop111.txt", FileMode.OpenOrCreate, FileAccess.Read))
    31             {
    32                 BinaryFormatter bf = new BinaryFormatter();
    33                 p = (Person)bf.Deserialize(fsRead);
    34             }
    35             Console.WriteLine(p.Name);
    36             Console.WriteLine(p.Age);
    37             Console.WriteLine(p.Gender);
    38             Console.ReadKey();
    39         }
    40     }
    41 
    42 
    43     [Serializable]
    44     public class Person
    45     {
    46         private string _name;
    47 
    48         public string Name
    49         {
    50             get { return _name; }
    51             set { _name = value; }
    52         }
    53 
    54 
    55         private char _gender;
    56 
    57         public char Gender
    58         {
    59             get { return _gender; }
    60             set { _gender = value; }
    61         }
    62 
    63         private int _age;
    64 
    65         public int Age
    66         {
    67             get { return _age; }
    68             set { _age = value; }
    69         }
    70     }
    71 }
    View Code

    6、partial部分类

    7、sealed密封类 不能够被其他类继承,但是可以继承于其他类。  

    8、接口 [public] interface I..able {  成员; }

    1)语法特征

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _10接口的语法特征
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13 
    14         }
    15     }
    16 
    17     public interface IFlyable
    18     {
    19         //接口中的成员不允许添加访问修饰符 ,默认就是public
    20         void Fly();
    21         string Test();
    22         //不允许写具有方法体的函数
    23 
    24      //   string _name;
    25          string Name
    26         {
    27             get;
    28             set;
    29         }
    30     
    31     }
    32 }
    View Code

    2)自动属性和普通属性

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _11_自动属性和普通属性
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13         }
    14     }
    15     public class Person
    16     {
    17         private string _name;
    18 
    19         public string Name
    20         {
    21             get { return _name; }
    22             set { _name = value; }
    23         }
    24 
    25         /// <summary>
    26         /// 自动属性
    27         /// </summary>
    28         public int Age
    29         {
    30             get;
    31             set;
    32         }
    33     }
    34 }
    View Code

    3)接口特点

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _12接口特点
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             IFlyable fly = new Bird();//new Person();// new IFlyable();
    14             fly.Fly();
    15             Console.ReadKey();
    16         }
    17     }
    18     public class Person:IFlyable
    19     {
    20         public void Fly()
    21         {
    22             Console.WriteLine("人类在飞");
    23         }
    24     }
    25 
    26 
    27     public class Student 
    28     {
    29         public void Fly()
    30         {
    31             Console.WriteLine("人类在飞");
    32         }
    33     }
    34 
    35     public class Bird : IFlyable
    36     {
    37         public void Fly()
    38         {
    39             Console.WriteLine("鸟在飞");
    40         }
    41     }
    42 
    43     public interface IFlyable
    44     {
    45         //不允许有访问修饰符 默认为public
    46         //方法、自动属性
    47         void Fly();
    48     }
    49 
    50 
    51 
    52     public interface M1
    53     {
    54         void Test1();
    55     }
    56 
    57     public interface M2
    58     {
    59         void Test2();
    60     }
    61 
    62     public interface M3
    63     {
    64         void Test3();
    65     }
    66 
    67 
    68     public interface SupperInterface : M1, M2, M3
    69     { 
    70         
    71     }
    72 
    73     public class Car : SupperInterface
    74     {
    75 
    76         public void Test1()
    77         {
    78             throw new NotImplementedException();
    79         }
    80 
    81         public void Test2()
    82         {
    83             throw new NotImplementedException();
    84         }
    85 
    86         public void Test3()
    87         {
    88             throw new NotImplementedException();
    89         }
    90     }
    91 
    92 }
    View Code

    4)接口练习

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _13接口练习
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //麻雀会飞  鹦鹉会飞  鸵鸟不会飞 企鹅不会飞  直升飞机会飞
    14             //用多态来实现
    15             //需方法、抽象类、接口
    16 
    17 
    18             IFlyable fly = new Plane();//new MaQue();//new YingWu();
    19             fly.Fly();
    20             Console.ReadKey();
    21 
    22 
    23         }
    24     }
    25 
    26     public class Bird
    27     {
    28         public double Wings
    29         {
    30             get;
    31             set;
    32         }
    33         public void EatAndDrink()
    34         {
    35             Console.WriteLine("我会吃喝");
    36         }
    37     }
    38 
    39     public class MaQue : Bird,IFlyable
    40     {
    41 
    42         public void Fly()
    43         {
    44             Console.WriteLine("麻雀会飞");
    45         }
    46     }
    47 
    48     public class YingWu : Bird, IFlyable,ISpeak
    49     {
    50         
    51 
    52         public void Fly()
    53         {
    54             Console.WriteLine("鹦鹉会飞");
    55         }
    56 
    57         public void Speak()
    58         {
    59             Console.WriteLine("鹦鹉可以学习人类说话");
    60         }
    61     }
    62 
    63 
    64     public class TuoBird : Bird
    65     { 
    66         
    67     }
    68 
    69     public class QQ : Bird
    70     { 
    71         
    72     }
    73 
    74 
    75     public class Plane : IFlyable
    76     {
    77         public void Fly()
    78         {
    79             Console.WriteLine("直升飞机转动螺旋桨飞行");
    80         }
    81     }
    82 
    83 
    84 
    85     public interface IFlyable
    86     {
    87         void Fly();
    88        
    89     }
    90 
    91     public interface ISpeak
    92     {
    93         void Speak();
    94     }
    95 
    96 
    97 }
    View Code

    5)显示实现接口

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _14_显示实现接口
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //显示实现接口就是为了解决方法的重名问题
    14             IFlyable fly = new Bird();
    15             fly.Fly();
    16             Bird bird = new Bird();
    17             bird.Fly();
    18 
    19             Console.ReadKey();
    20         }
    21     }
    22 
    23 
    24     public class Bird : IFlyable
    25     {
    26         public void Fly()
    27         {
    28             Console.WriteLine("鸟飞会");
    29         }
    30         /// <summary>
    31         /// 显示实现接口
    32         /// </summary>
    33          void IFlyable.Fly()
    34         {
    35             Console.WriteLine("我是接口的飞");
    36         }
    37 
    38     }
    39 
    40     public interface IFlyable
    41     {
    42         void Fly();
    43     }
    44 }
    View Code

    6)总结

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _15_总结
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //什么时候用虚方法来实现多态?
    14             //什么使用用抽象类来实现多态?
    15             //什么时候用接口来实现多态?
    16 
    17             //真的鸭子会游泳 木头鸭子不会游泳 橡皮鸭子会游泳
    18 
    19             ISwimming swim = new XPDuck();//new RealDuck();
    20             swim.Swim();
    21             Console.ReadKey();
    22         }
    23     }
    24 
    25     public class RealDuck:ISwimming
    26     {
    27 
    28         public void Swim()
    29         {
    30             Console.WriteLine("真的鸭子靠翅膀游泳");
    31         }
    32     }
    33 
    34     public class MuDuck 
    35     { 
    36     
    37     }
    38 
    39     public class XPDuck : ISwimming
    40     {
    41 
    42         public void Swim()
    43         {
    44             Console.WriteLine("橡皮鸭子飘着游泳");
    45         }
    46     }
    47 
    48     public interface ISwimming
    49     {
    50         void Swim();
    51     }
    52 }
    View Code
  • 相关阅读:
    vue(七)--监听属性(watch)
    vue(六)--计算属性(computed)
    JVM参数配置&&命令工具
    GC回收算法&&GC回收器
    JVM宏观认知&&内存结构
    分布式配置中心Apollo——QuickStart
    了解敏捷开发
    服务链路跟踪 && 服务监控
    数据读写API——IO流
    Nginx+Zuul集群实现高可用网关
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4478905.html
Copyright © 2020-2023  润新知