• 0505.Net基础班第十三天(面向对象多态)


    1、c#中的访问修饰符 public :公开的公共的 private:私有的,只能在当前类的内部访问 protected:受保护的,只能在当前类的内部以及该类的子类中访问。 internal:只能在当前项目中访问。在同一个项目中,internal和public的权限是一样。 protected internal:protected+internal

    1)、能够修饰类的访问修饰符只有两个:public、internal。 2)、可访问性不一致。 子类的访问权限不能高于父类的访问权限,会暴漏父类的成员。

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

    3、简单工厂设计模式

    4、值类型在复制的时候,传递的是这个值得本身。    引用类型在复制的时候,传递的是对这个对象的引用。    5、序列化:就是将对象转换为二进制    反序列化:就是将二进制转换为对象    作用:传输数据。   序列化:   1)、将这个类标记为可以被序列化的。     6、partial部分类

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

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

    接口是一种规范。 只要一个类继承了一个接口,这个类就必须实现这个接口中所有的成员

    为了多态。 接口不能被实例化。 也就是说,接口不能new(不能创建对象)

    接口中的成员不能加“访问修饰符”,接口中的成员访问修饰符为public,不能修改。

    (默认为public) 接口中的成员不能有任何实现(“光说不做”,只是定义了一组未实现的成员)。

    接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。

    接口与接口之间可以继承,并且可以多继承。

    接口并不能去继承一个类,而类可以继承接口  (接口只能继承于接口,而类既可以继承接口,也可以继承类)

    实现接口的子类必须实现该接口的全部成员。

    一个类可以同时继承一个类并实现多个接口,如果一个子类同时继承了父类A,并实现了接口IA,那么语法上A必须写在IA的前面。

    class MyClass:A,IA{},因为类是单继承的。

    显示实现接口的目的:解决方法的重名问题 什么时候显示的去实现接口: 当继承的借口中的方法和参数一摸一样的时候,要是用显示的实现接口

    当一个抽象类实现接口的时候,需要子类去实现接口。

    01复习

      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 namespace _01复习
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             /*
     14              泛型集合
     15              * List<T>
     16              * Dictionary<T,T>
     17              * 装箱和拆箱
     18              * 装箱:将值类型转换为引用类型
     19              * 拆箱:将引用类型转换为值类型
     20              * 我们应该尽量避免在代码中发生装箱或者拆箱
     21              * 文件流
     22              * FileStream StreamReader和StreamWriter
     23              * 多态:虚方法、
     24              * //抽象类、接口
     25              * 虚方法:
     26              * 抽象类:
     27              
     28              */
     29             List<int> list = new List<int>();
     30 
     31             //Dictionary<int, string> dic = new Dictionary<int, string>();
     32             //dic.Add(1, "张三");
     33             //dic[2] = "李四";
     34             //foreach (KeyValuePair<int,string> kv in dic)
     35             //{
     36             //    Console.WriteLine("{0}-----{1}",kv.Key,kv.Value);
     37             //}
     38             //Console.ReadKey();
     39 
     40             //File  FileStream StreamReader StreamWriter
     41             //using (FileStream fsRead = new FileStream("0505远程版机器码.txt", FileMode.OpenOrCreate, FileAccess.Read))
     42             //{
     43             //    byte[] buffer = new byte[1024 * 1024 * 5];
     44             //    //本次读取实际读取到的字节数
     45             //    int r = fsRead.Read(buffer, 0, buffer.Length);
     46 
     47             //    //将字节数组中的每个元素按照我们指定的编码格式解析成字符串
     48             //    string s = Encoding.Default.GetString(buffer, 0, r);
     49             //    Console.WriteLine(s);
     50             //}
     51             //Console.ReadKey();
     52 
     53             using (FileStream fsWrite = new FileStream(@"new.txt", FileMode.OpenOrCreate, FileAccess.Write))
     54             {
     55                 string s = "abc";
     56                 byte[] buffer = Encoding.UTF8.GetBytes(s);
     57                 fsWrite.Write(buffer, 0, buffer.Length);
     58             }
     59             Console.WriteLine("OK");
     60             Console.ReadKey();
     61 
     62 
     63 
     64 
     65 
     66 
     67             //虚方法和抽象类
     68 
     69             //老师可以起立  学生也可以起立  校长也可以起立
     70 
     71             //声明父类去指定子类对象
     72 
     73             Person p = new HeadMaster();//new Teacher();//new Student();
     74             p.StandUp();
     75             Console.ReadKey();
     76 
     77         }
     78     }
     79 
     80 
     81     public abstract class Person
     82     {
     83         public abstract void StandUp();
     84     }
     85 
     86     public class Student : Person
     87     {
     88         public override void StandUp()
     89         {
     90             Console.WriteLine("学生起立,说老师好");
     91         }
     92     }
     93 
     94 
     95     public class Teacher : Person
     96     {
     97         public override void StandUp()
     98         {
     99             Console.WriteLine("老师起立,说校长好");
    100         }
    101     }
    102 
    103     public class HeadMaster : Person
    104     {
    105         public override void StandUp()
    106         {
    107             Console.WriteLine("校长起来说领导好");
    108         }
    109     }
    110 
    111 
    112 
    113 }
    View Code

    02访问修饰符

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _02访问修饰符
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Person p = new Person();
    14         }
    15     }
    16 
    17     public class Person
    18     {
    19         //protected string _name;
    20         //public int _age;
    21         //private char _gender;
    22         //internal int _chinese;
    23         //protected internal int _math;
    24     }
    25 
    26     public class Student : Person
    27     { 
    28         
    29     }
    30 
    31     //public class Student:Person
    32     //{ 
    33         
    34     //}
    35 }
    View Code

    03简单工厂设计模式

     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

    04值类型和引用类型

     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

    05序列化和反序列化

     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

    06部分类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _06部分类
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13         }
    14     }
    15 
    16     public partial class Person
    17     {
    18         private string _name;
    19         public void Test()
    20         { 
    21             
    22         }
    23     }
    24 
    25     public partial class Person
    26     {
    27         public void Test(string name)
    28         { 
    29            // _name
    30         }
    31     }
    32 }
    View Code

    07密封类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _07密封类
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13 
    14         }
    15     }
    16 
    17     public sealed class Person:Test
    18     { 
    19         
    20     }
    21 
    22     public class Test
    23     { 
    24         
    25     }
    26 }
    View Code

    08重写ToString()方法

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _08重写ToString__方法
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Person p = new Person();
    14             Console.WriteLine(p.ToString());
    15             Console.ReadKey();
    16         }
    17     }
    18 
    19     public class Person
    20     {
    21         public override string ToString()
    22         {
    23             return "Hello World";
    24         }
    25     }
    26 }
    View Code

    09接口

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _09接口
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //接口就是一个规范、能力。
    14         }
    15     }
    16 
    17     public class Person
    18     {
    19         public void CHLSS()
    20         {
    21             Console.WriteLine("我是人类,我可以吃喝拉撒睡");
    22         }
    23     }
    24     public class NBAPlayer
    25     {
    26         public void KouLan()
    27         {
    28             Console.WriteLine("我可以扣篮");
    29         }
    30     }
    31 
    32     public class Student : Person,IKouLanable
    33     {
    34         public void KouLan()
    35         {
    36             Console.WriteLine("我也可以扣篮");
    37         }
    38     }
    39 
    40 
    41     public interface IKouLanable
    42     {
    43         void KouLan();
    44     }
    45 
    46 }
    View Code

    10接口的语法特征

     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

    11、自动属性和普通属性

     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

    12接口特点

     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:Person, IFlyable
    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

    13接口练习

     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

    14、显示实现接口

     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

    15、总结

     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             RealDuck rd = new RealDuck();
    22             rd.Swim();
    23             RealDuck xd = new XPDuck();
    24             xd.Swim();
    25             Console.ReadKey();
    26         }
    27     }
    28 
    29     public class RealDuck:ISwimming
    30     {
    31 
    32         public virtual void Swim()
    33         {
    34             Console.WriteLine("真的鸭子靠翅膀游泳");
    35         }
    36     }
    37 
    38     public class MuDuck 
    39     { 
    40     
    41     }
    42 
    43     public class XPDuck : RealDuck, ISwimming
    44     {
    45 
    46         public override void Swim()
    47         {
    48             Console.WriteLine("橡皮鸭子飘着游泳");
    49         }
    50     }
    51 
    52     public interface ISwimming
    53     {
    54         void Swim();
    55     }
    56 }
    View Code

    16超市收银系统

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     class Acer:ProductFather
    10     {
    11         public Acer(string id, double price, string Name)
    12             : base(id, price, Name)
    13         { 
    14             
    15         }
    16     }
    17 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     class Banana : ProductFather
    10     {
    11         public Banana(string id, double price, string Name)
    12             : base(id, price, Name)
    13         {
    14 
    15         }
    16     }
    17 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9 
    10     /// <summary>
    11     /// 打折的父类
    12     /// </summary>
    13     abstract class CalFather
    14     {
    15 
    16         /// <summary>
    17         /// 计算打折后应付多少钱
    18         /// </summary>
    19         /// <param name="realMoney">打折前应付的价钱</param>
    20         /// <returns>打折后应付的前</returns>
    21         public abstract double GetTotalMoney(double realMoney);
    22     }
    23 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9 
    10     /// <summary>
    11     /// 买M元 送N元
    12     /// </summary>
    13     class CalMN:CalFather
    14     {
    15         //买500送100
    16         public double M
    17         {
    18             get;
    19             set;
    20         }
    21 
    22         public double N
    23         {
    24             get;
    25             set;
    26         }
    27 
    28         public CalMN(double m, double n)
    29         {
    30             this.M = m;
    31             this.N = n;
    32         }
    33         public override double GetTotalMoney(double realMoney)
    34         {
    35             //600 -100
    36             //1000-200
    37             //1200 
    38             if (realMoney >= this.M)
    39             {
    40                 return realMoney - (int)(realMoney / this.M) * this.N;
    41             }
    42             else
    43             {
    44                 return realMoney;
    45             }
    46         }
    47     }
    48 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9 
    10     /// <summary>
    11     /// 不打折 该多少钱就多少钱
    12     /// </summary>
    13     class CalNormal:CalFather
    14     {
    15         public override double GetTotalMoney(double realMoney)
    16         {
    17             return realMoney;
    18         }
    19     }
    20 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     /// <summary>
    10     /// 按折扣率打折
    11     /// </summary>
    12     class CalRate:CalFather
    13     {
    14 
    15         /// <summary>
    16         /// 折扣率
    17         /// </summary>
    18         public double Rate
    19         {
    20             get;
    21             set;
    22         }
    23 
    24         public CalRate(double rate)
    25         {
    26             this.Rate = rate;
    27         }
    28         public override double GetTotalMoney(double realMoney)
    29         {
    30             return realMoney * this.Rate;
    31         }
    32     }
    33 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     class CangKu
    10     {
    11 
    12         List<List<ProductFather>> list = new List<List<ProductFather>>();
    13 
    14 
    15 
    16         /// <summary>
    17         ///向用户展示货物
    18         /// </summary>
    19         public void ShowPros()
    20         {
    21             foreach (var item in list)
    22             {
    23                 Console.WriteLine("我们超市有:" + item[0].Name + "," + "	" + "" + item.Count + "个," + "	" + "每个" + item[0].Price + "");
    24             }
    25         }
    26         //list[0]存储Acer电脑
    27         //list[1]存储三星手机
    28         //list[2]存储酱油
    29         //list[3]存储香蕉
    30         /// <summary>
    31         /// 在创建仓库对象的时候 像仓库中添加货架
    32         /// </summary>
    33         public CangKu()
    34         {
    35             list.Add(new List<ProductFather>());
    36             list.Add(new List<ProductFather>());
    37             list.Add(new List<ProductFather>());
    38             list.Add(new List<ProductFather>());
    39         }
    40         /// <summary>
    41         /// 进货
    42         /// </summary>
    43         /// <param name="strType">货物的类型</param>
    44         /// <param name="count">货物的数量</param>
    45         public void JinPros(string strType, int count)
    46         {
    47             for (int i = 0; i < count; i++) 
    48             {
    49                 switch (strType)
    50                 {
    51                     case "Acer": list[0].Add(new Acer(Guid.NewGuid().ToString(), 1000, "宏基笔记本"));
    52                         break;
    53                     case "SamSung": list[1].Add(new SamSung(Guid.NewGuid().ToString(), 2000, "棒之手机"));
    54                         break;
    55                     case "JiangYou": list[2].Add(new JiangYou(Guid.NewGuid().ToString(), 10, "老抽酱油"));
    56                         break;
    57                     case "Banana": list[3].Add(new Banana(Guid.NewGuid().ToString(), 50, "大香蕉"));
    58                         break;
    59                 }
    60             }
    61         }
    62         /// <summary>
    63         /// 从仓库中提取货物
    64         /// </summary>
    65         /// <param name="strType"></param>
    66         /// <param name="count"></param>
    67         /// <returns></returns>
    68         public ProductFather[] QuPros(string strType, int count)
    69         {
    70             ProductFather[] pros = new ProductFather[count];
    71             for (int i = 0; i < pros.Length; i++)
    72             {
    73                 switch (strType)
    74                 {
    75                     case "Acer":
    76                         pros[i] = list[0][0];
    77                         list[0].RemoveAt(0);
    78                         break;
    79                     case "SamSung": pros[i] = list[1][0];
    80                         list[1].RemoveAt(0);
    81                         break;
    82                     case "JiangYou": pros[i] = list[2][0];
    83                         list[2].RemoveAt(0);
    84                         break;
    85                     case "Banana": pros[i] = list[3][0];
    86                         list[3].RemoveAt(0);
    87                         break;
    88                 }
    89             }
    90             return pros;
    91         }
    92 
    93 
    94     }
    95 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     class JiangYou:ProductFather
    10     {
    11         public JiangYou(string id, double price, string Name)
    12             : base(id, price, Name)
    13         { 
    14             
    15         }
    16     }
    17 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     class ProductFather
    10     {
    11         public double Price
    12         {
    13             get;
    14             set;
    15         }
    16 
    17         public string Name
    18         {
    19             get;
    20             set;
    21         }
    22 
    23         public string ID
    24         {
    25             get;
    26             set;
    27         }
    28 
    29         public ProductFather(string id, double price, string Name)
    30         {
    31             this.ID = id;
    32             this.Price = price;
    33             this.Name = Name;
    34         }
    35     }
    36 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //创建超市对象
    14             SupperMarket sm = new SupperMarket();
    15             //展示货物
    16             sm.ShowPros();
    17             //跟用户交互
    18             sm.AskBuying();
    19             Console.ReadKey();
    20         }
    21     }
    22 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _16超市收银系统
     8 {
     9     class SamSung:ProductFather
    10     {
    11         public SamSung(string id, double price, string Name)
    12             : base(id, price, Name)
    13         { 
    14             
    15         }
    16     }
    17 }
    View Code
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace _16超市收银系统
      8 {
      9     class SupperMarket
     10     {
     11         //创建仓库对象
     12         CangKu ck = new CangKu();
     13         /// <summary>
     14         /// 创建超市对象的时候,给仓库的货架上导入货物
     15         /// </summary>
     16         public SupperMarket()
     17         {
     18             ck.JinPros("Acer", 1000);
     19             ck.JinPros("SamSung", 1000);
     20             ck.JinPros("JiangYou", 1000);
     21             ck.JinPros("Banana", 1000);
     22         }
     23 
     24 
     25         /// <summary>
     26         /// 跟用户交互的过程
     27         /// </summary>
     28         public void AskBuying()
     29         {
     30             Console.WriteLine("欢迎观临,请问您需要些什么?");
     31             Console.WriteLine("我们有 Acer、SamSung、Jiangyou、Banana");
     32             string strType = Console.ReadLine();
     33             Console.WriteLine("您需要多少?");
     34             int count = Convert.ToInt32(Console.ReadLine());
     35             //去仓库取货物
     36             ProductFather[] pros = ck.QuPros(strType, count);
     37             //下面该计算价钱了
     38             double realMoney = GetMoney(pros);
     39             Console.WriteLine("您总共应付{0}元", realMoney);
     40             Console.WriteLine("请选择您的打折方式 1--不打折 2--打九折  3--打85 折  4--买300送50  5--买500送100");
     41             string input = Console.ReadLine();
     42             //通过简单工厂的设计模式根据用户的舒服获得一个打折对象
     43             CalFather cal = GetCal(input);
     44             double totalMoney = cal.GetTotalMoney(realMoney);
     45             Console.WriteLine("打完折后,您应付{0}元", totalMoney);
     46             Console.WriteLine("以下是您的购物信息");
     47             foreach (var item in pros)
     48             {
     49                 Console.WriteLine("货物名称:"+item.Name+","+"	"+"货物单价:"+item.Price+","+"	"+"货物编号:"+item.ID);
     50             }
     51 
     52         }
     53 
     54 
     55 
     56        
     57 
     58 
     59 
     60         /// <summary>
     61         /// 根据用户的选择打折方式返回一个打折对象
     62         /// </summary>
     63         /// <param name="input">用户的选择</param>
     64         /// <returns>返回的父类对象 但是里面装的是子类对象</returns>
     65         public CalFather GetCal(string input)
     66         {
     67             CalFather cal = null;
     68             switch (input)
     69             {
     70                 case "1": cal = new CalNormal();
     71                     break;
     72                 case "2": cal = new CalRate(0.9);
     73                     break;
     74                 case "3": cal = new CalRate(0.85);
     75                     break;
     76                 case "4": cal = new CalMN(300, 50);
     77                     break;
     78                 case "5": cal = new CalMN(500, 100);
     79                     break;
     80             }
     81             return cal;
     82         }
     83 
     84 
     85 
     86 
     87         /// <summary>
     88         /// 根据用户买的货物计算总价钱
     89         /// </summary>
     90         /// <param name="pros"></param>
     91         /// <returns></returns>
     92         public double GetMoney(ProductFather[] pros)
     93         {
     94             double realMoney = 0;
     95             //realMoney = pros[0].Price * pros.Length;
     96 
     97             for (int i = 0; i < pros.Length; i++)
     98             {
     99                 realMoney += pros[i].Price;
    100 
    101                 // realMoney = pros[i] * pros.Length;
    102             }
    103             return realMoney;
    104         }
    105 
    106 
    107 
    108 
    109         public void ShowPros()
    110         {
    111             ck.ShowPros();
    112         }
    113 
    114     }
    115 }
    View Code

    17GUID

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _17GUID
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //产生一个不会重复的编号
    14             Console.WriteLine(Guid.NewGuid().ToString());
    15             Console.WriteLine(Guid.NewGuid().ToString());
    16             Console.WriteLine(Guid.NewGuid().ToString());
    17             Console.WriteLine(Guid.NewGuid().ToString());
    18             Console.WriteLine(Guid.NewGuid().ToString());
    19             Console.ReadKey();
    20         }
    21     }
    22 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _17GUID
     8 {
     9     class SamSung
    10     {
    11     }
    12 }
    View Code
  • 相关阅读:
    面向使用的软件设计随笔13
    面向使用的软件设计随笔12
    面向使用的软件设计随笔11
    面向使用的软件设计随笔10
    面向使用的软件设计随笔09
    面向使用的软件设计随笔08
    面向使用的软件设计随笔07
    Tensorflow入门----占位符、常量和Session
    关于卷积
    tensorflow学习笔记
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4713505.html
Copyright © 2020-2023  润新知