zoukankan      html  css  js  c++  java
  • 0505.Net基础班第十二天(面向对象多态)

    1、绝对路径和相对路径 绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件。 相对路径:文件相对于应用程序的路径。 结论: 我们在开发中应该去尽量的使用相对路径。

    2、装箱、拆箱 装箱:就是将值类型转换为引用类型。 拆箱:将引用类型转换为值类型。 看两种类型是否发生了装箱或者拆箱,要看,这两种类型是否存在继承关系。

    3、将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源。

    4、实现多态的手段 1)、虚方法 步骤: 1、将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍。

    2)、抽象类 当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法。

    1、绝对路径和相对路径 绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件。 相对路径:文件相对于应用程序的路径。 结论: 我们在开发中应该去尽量的使用相对路径。

    2、装箱、拆箱 装箱:就是将值类型转换为引用类型。 拆箱:将引用类型转换为值类型。 看两种类型是否发生了装箱或者拆箱,要看,这两种类型是否存在继承关系。

    3、将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源。

    4、实现多态的手段 1)、虚方法 步骤: 1、将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍。

    2)、抽象类 当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法。

    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.Collections;
      7 using System.IO;
      8 namespace _01复习
      9 {
     10     class Program
     11     {
     12         static void Main(string[] args)
     13         {
     14             /*
     15              里氏转换:
     16              * 1、子类可以赋值给父类(如果有一个方法需要一个父类作为参数,我们可以传第一个子类对象)
     17              * 2、如果父类中装的是子类对象,则可以将这个父类强转为子类对象
     18              */
     19 
     20             //Person p = new Student();
     21             ////is as
     22             //Student ss = p as Student;
     23             //ss.StudentSayHello();
     24             //Console.ReadKey();
     25             //if (p is Student)
     26             //{
     27             //    ((Student)p).StudentSayHello();
     28             //}
     29             //else
     30             //{
     31             //    Console.WriteLine("转换失败");
     32             //}
     33             //Console.ReadKey();
     34 
     35 
     36             //ArrayList list = new ArrayList();
     37             //Remove RemoveAt  RemoveRange  Clear  Insert  InsertRange
     38             //Reverse  Sort
     39 
     40             //Hashtable ht = new Hashtable();
     41             //ht.Add(1, "张三");
     42             //ht.Add(true, '男');
     43             //ht.Add(3.14, 5000m);
     44             ////在键值对集合中 键必须是唯一的
     45             ////ht.Add(1, "李四");
     46             //ht[1] = "李四";
     47             ////ht.ContainsKey
     48             //foreach (var item in ht.Keys)
     49             //{
     50             //    Console.WriteLine("{0}------------{1}",item,ht[item]);
     51             //}
     52             //Console.ReadKey();
     53 
     54             //Path
     55 
     56 
     57             //File
     58             //Create  Delete Copy Move
     59 
     60             //byte[] buffer = File.ReadAllBytes(@"C:UsersSpringRainDesktop抽象类特点.txt");
     61             ////将字节数组中的每一个元素都要按照我们指定的编码格式解码成字符串
     62             ////UTF-8  GB2312 GBK ASCII  Unicode
     63             //string s = Encoding.Default.GetString(buffer);
     64 
     65             //Console.WriteLine(s);
     66             //Console.ReadKey();
     67 
     68             //没有这个文件的话 会给你创建一个 有的话 会给你覆盖掉
     69             //string str="今天天气好晴朗处处好风光";
     70             ////需要将字符串转换成字节数组
     71             //byte[] buffer= Encoding.Default.GetBytes(str);
     72             //File.WriteAllBytes(@"C:UsersSpringRainDesktop
    ew.txt", buffer);
     73             //Console.WriteLine("写入成功");
     74             //Console.ReadKey();
     75 
     76 
     77             //string[] contents = File.ReadAllLines(@"C:UsersSpringRainDesktop抽象类特点.txt", Encoding.Default);
     78             //foreach (string item in contents)
     79             //{
     80             //    Console.WriteLine(item);
     81             //}
     82             //Console.ReadKey();
     83 
     84 
     85             string str = File.ReadAllText("抽象类特点.txt", Encoding.Default);
     86             Console.WriteLine(str);
     87             Console.ReadKey();
     88 
     89             //File.WriteAllLines(@"C:UsersSpringRainDesktop
    ew.txt", new string[] { "aoe", "ewu" });
     90             //Console.WriteLine("OK");
     91             //Console.ReadKey();
     92 
     93 
     94             //File.WriteAllText(@"C:UsersSpringRainDesktop
    ew.txt", "张三李四王五赵六");
     95             //Console.WriteLine("OK");
     96             //Console.ReadKey();
     97 
     98             //File.AppendAllText(@"C:UsersSpringRainDesktop
    ew.txt", "看我有木有把你覆盖掉");
     99             //Console.WriteLine("OK");
    100             //Console.ReadKey();
    101 
    102         }
    103     }
    104 
    105 
    106     //public class Person
    107     //{
    108     //    public void PersonSayHello()
    109     //    {
    110     //        Console.WriteLine("我是老师");
    111     //    }
    112     //}
    113 
    114     //public class Student : Person
    115     //{
    116     //    public void StudentSayHello()
    117     //    {
    118     //        Console.WriteLine("我是学生");
    119     //    }
    120     //}
    121 
    122 }
    View Code

    02、List泛型集合

     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_List泛型集合
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //创建泛型集合对象
    14             //List<int> list = new List<int>();
    15             //list.Add(1);
    16             //list.Add(2);
    17             //list.Add(3);
    18 
    19             //list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });
    20             //list.AddRange(list);
    21 
    22             //List泛型集合可以转换为数组
    23             //int[] nums = list.ToArray();
    24 
    25             //List<string> listStr = new List<string>();
    26 
    27             //string[] str = listStr.ToArray();
    28 
    29 
    30             //char[] chs = new char[] { 'c', 'b', 'a' };
    31             //List<char> listChar = chs.ToList();
    32             //for (int i = 0; i < listChar.Count; i++)
    33             //{
    34             //    Console.WriteLine(listChar[i]);
    35             //}
    36 
    37             ////   List<int> listTwo = nums.ToList();
    38 
    39 
    40             //for (int i = 0; i < list.Count; i++)
    41             //{
    42             //    Console.WriteLine(list[i]);
    43             //}
    44             Console.ReadKey();
    45         }
    46     }
    47 }
    View Code

    03、装箱和拆箱

     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.Diagnostics;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace _03_装箱和拆箱
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             //int n = 10;
    16             //object o = n;//装箱
    17             //int nn = (int)o;//拆箱
    18 
    19 
    20             //ArrayList list = new ArrayList();
    21             //List<int> list = new List<int>();
    22             ////这个循环发生了100万次装箱操作
    23             //Stopwatch sw = new Stopwatch();
    24             ////00:00:02.4370587
    25             ////00:00:00.2857600
    26             //sw.Start();
    27             //for (int i = 0; i < 10000000; i++)
    28             //{
    29             //    list.Add(i);
    30             //}
    31             //sw.Stop();
    32             //Console.WriteLine(sw.Elapsed);
    33             //Console.ReadKey();
    34 
    35 
    36             //这个地方没有发生任意类型的装箱或者拆箱
    37             //string str = "123";
    38             //int n = Convert.ToInt32(str);
    39 
    40 
    41             int n = 10;
    42             IComparable i = n;//装箱
    43 
    44             //发生
    45         }
    46     }
    47 }
    View Code

    04Dictionary

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _04Dictionary
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Dictionary<int, string> dic = new Dictionary<int, string>();
    14             dic.Add(1, "张三");
    15             dic.Add(2, "李四");
    16             dic.Add(3, "王五");
    17             dic[1] = "新来的";
    18             //foreach (KeyValuePair<int,string> kv in dic)
    19             //{
    20             //    Console.WriteLine("{0}---{1}",kv.Key,kv.Value);
    21             //}
    22 
    23             foreach (KeyValuePair <int,string> kv in dic)
    24             {
    25                 Console.WriteLine("{0}---{1}",kv.Key,kv.Value);
    26             }
    27 
    28             //foreach (var item in dic.Keys)
    29             //{
    30             //    Console.WriteLine("{0}---{1}",item,dic[item]);
    31             //}
    32             Console.ReadKey();
    33         }
    34     }
    35 }
    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 
      7 namespace _05泛型集合的练习
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             //将一个数组中的奇数放到一个集合中,再将偶数放到另一个集合中
     14             //最终将两个集合合并为一个集合,并且奇数显示在左边 偶数显示在右边。
     15 
     16             int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     17             List<int> listOu = new List<int>();
     18             List<int> listJi = new List<int>();
     19             for (int i = 0; i < nums.Length; i++)
     20             {
     21                 if (nums[i] % 2 == 0)
     22                 {
     23                     listOu.Add(nums[i]);
     24                 }
     25                 else
     26                 {
     27                     listJi.Add(nums[i]);
     28                 }
     29             }
     30 
     31 
     32             ////listOu.AddRange(listJi);
     33             ////foreach (int item in listOu)
     34             ////{
     35             ////    Console.Write(item+"  ");
     36             ////}
     37 
     38 
     39             listJi.AddRange(listOu);
     40             foreach (var item in listJi)
     41             {
     42                 Console.Write(item + "  ");
     43             }
     44             Console.ReadKey();
     45             ////List<int> listSum = new List<int>();
     46             ////listSum.AddRange(listOu);
     47             ////listSum.AddRange(listJi);
     48 
     49             //提手用户输入一个字符串,通过foreach循环将用户输入的字符串赋值给一个字符数组
     50 
     51             //Console.WriteLine("请输入一个字符串");
     52             //string input = Console.ReadLine();
     53             //char[] chs = new char[input.Length];
     54             //int i = 0;
     55             //foreach (var item in input)
     56             //{
     57             //    chs[i] = item;
     58             //    i++;
     59             //}
     60 
     61             //foreach (var item in chs)
     62             //{
     63             //    Console.WriteLine(item);
     64             //}
     65             //Console.ReadKey();
     66 
     67             //统计 Welcome to china中每个字符出现的次数 不考虑大小写
     68 
     69             //string str = "Welcome to China";
     70             ////字符 ------->出现的次数
     71             ////键---------->值
     72             //Dictionary<char, int> dic = new Dictionary<char, int>();
     73             //for (int i = 0; i < str.Length; i++)
     74             //{
     75             //    if (str[i] == ' ')
     76             //    {
     77             //        continue;
     78             //    }
     79             //    //如果dic已经包含了当前循环到的这个键 
     80             //    if (dic.ContainsKey(str[i]))
     81             //    {
     82             //        //值再次加1
     83             //        dic[str[i]]++;
     84             //    }
     85             //    else//这个字符在集合当中是第一次出现
     86             //    {
     87             //        dic[str[i]] = 1;
     88             //    }
     89             //}
     90 
     91             //foreach (KeyValuePair<char,int> kv in dic)
     92             //{
     93             //    Console.WriteLine("字母{0}出现了{1}次",kv.Key,kv.Value);
     94             //}
     95             //Console.ReadKey();
     96 
     97             //w  1
     98 
     99 
    100         }
    101     }
    102 }
    View Code

    06文件流

     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace _06文件流
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14 
    15             //使用FileStream来读取数据
    16             FileStream fsRead = new FileStream(@"C:UsersSpringRainDesktop
    ew.txt", FileMode.OpenOrCreate, FileAccess.Read);
    17             byte[] buffer = new byte[1024 * 1024 * 5];
    18             //3.8M  5M
    19             //返回本次实际读取到的有效字节数
    20             int r = fsRead.Read(buffer, 0, buffer.Length);
    21             //将字节数组中每一个元素按照指定的编码格式解码成字符串
    22             string s = Encoding.UTF8.GetString(buffer, 0, r);
    23             //关闭流
    24             fsRead.Close();
    25             //释放流所占用的资源
    26             fsRead.Dispose();
    27             Console.WriteLine(s);
    28             Console.ReadKey();
    29 
    30 
    31 
    32             //使用FileStream来写入数据
    33             //using (FileStream fsWrite = new FileStream(@"C:UsersSpringRainDesktop
    ew.txt", FileMode.OpenOrCreate, FileAccess.Write))
    34             //{
    35             //    string str = "看我游牧又把你覆盖掉";
    36             //    byte[] buffer = Encoding.UTF8.GetBytes(str);
    37             //    fsWrite.Write(buffer, 0, buffer.Length);
    38             //}
    39             //Console.WriteLine("写入OK");
    40             //Console.ReadKey();
    41         }
    42     }
    43 }
    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 using System.IO;
     7 
     8 namespace _07使用文件流来实现多媒体文件的复制
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             //思路:就是先将要复制的多媒体文件读取出来,然后再写入到你指定的位置
    15             string source = @"C:UsersSpringRainDesktop1、复习.wmv";
    16             string target = @"C:UsersSpringRainDesktop
    ew.wmv";
    17             CopyFile(source, target);
    18             Console.WriteLine("复制成功");
    19             Console.ReadKey();
    20         }
    21 
    22         public static void CopyFile(string soucre, string target)
    23         {
    24             //1、我们创建一个负责读取的流
    25             using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read))
    26             {
    27                 //2、创建一个负责写入的流
    28                 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
    29                 {
    30                     byte[] buffer = new byte[1024 * 1024 * 5];
    31                     //因为文件可能会比较大,所以我们在读取的时候 应该通过一个循环去读取
    32                     while (true)
    33                     {
    34                         //返回本次读取实际读取到的字节数
    35                         int r = fsRead.Read(buffer, 0, buffer.Length);
    36                         //如果返回一个0,也就意味什么都没有读取到,读取完了
    37                         if (r == 0)
    38                         {
    39                             break;
    40                         }
    41                         fsWrite.Write(buffer, 0, r);
    42                     }
    43 
    44                  
    45                 }
    46             }
    47         }
    48 
    49 
    50     }
    51 }
    View Code

    08StreamReader和StreamWriter

     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 _08StreamReader和StreamWriter
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //使用StreamReader来读取一个文本文件
    14             using (StreamReader sr = new StreamReader(@"D:桌面a.txt", Encoding.Default))
    15             {
    16                 using (StreamWriter sw = new StreamWriter(@"D:桌面.txt",true))
    17                 {
    18                     while (!sr.EndOfStream)
    19                     {
    20                         Console.WriteLine(sr.ReadLine());
    21                         sw.Write(sr.ReadLine());
    22                     }
    23                 }
    24                 Console.WriteLine("OK");
    25                 Console.ReadKey();
    26             }
    27 
    28 
    29             //使用StreamWriter来写入一个文本文件
    30             //using (StreamWriter sw = new StreamWriter(@"C:UsersSpringRainDesktop
    ewnew.txt",true))
    31             //{
    32             //    sw.Write("看我有木有把你覆盖掉");
    33             //}
    34             //Console.WriteLine("OK");
    35             //Console.ReadKey();
    36         }
    37     }
    38 }
    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             //实现多态的3种手段:1、虚方法 2、抽象类 3、接口
     15 
     16             Chinese cn1 = new Chinese("韩梅梅");
     17             Chinese cn2 = new Chinese("李雷");
     18             Japanese j1 = new Japanese("树下君");
     19             Japanese j2 = new Japanese("井边子");
     20             Korea k1 = new Korea("金秀贤");
     21             Korea k2 = new Korea("金贤秀");
     22             American a1 = new American("科比布莱恩特");
     23             American a2 = new American("奥尼尔");
     24             Person[] pers = { cn1, cn2, j1, j2, k1, k2, a1, a2, new English("格林"), new English("玛利亚") };
     25 
     26             for (int i = 0; i < pers.Length; i++)
     27             {
     28                 //if (pers[i] is Chinese)
     29                 //{
     30                 //    ((Chinese)pers[i]).SayHello();
     31                 //}
     32                 //else if (pers[i] is Japanese)
     33                 //{
     34                 //    ((Japanese)pers[i]).SayHello();
     35                 //}
     36                 //else if (pers[i] is Korea)
     37                 //{
     38                 //    ((Korea)pers[i]).SayHello();
     39                 //}
     40                 //else
     41                 //{
     42                 //    ((American)pers[i]).SayHello();
     43                 //}
     44 
     45 
     46                 pers[i].SayHello();
     47             }
     48             Console.ReadKey();
     49         }
     50     }
     51 
     52     public class Person
     53     {
     54         private string _name;
     55         public string Name
     56         {
     57             get { return _name; }
     58             set { _name = value; }
     59         }
     60 
     61         public Person(string name)
     62         {
     63             this.Name = name;
     64         }
     65         public virtual void SayHello()
     66         {
     67             Console.WriteLine("我是人类");
     68         }
     69 
     70     }
     71 
     72     public class Chinese : Person
     73     {
     74         public Chinese(string name)
     75             : base(name)
     76         {
     77 
     78         }
     79 
     80         public override void SayHello()
     81         {
     82             Console.WriteLine("我是中国人,我叫{0}", this.Name);
     83         }
     84     }
     85     public class Japanese : Person
     86     {
     87         public Japanese(string name)
     88             : base(name)
     89         { }
     90 
     91         public override void SayHello()
     92         {
     93             Console.WriteLine("我是脚盆国人,我叫{0}", this.Name);
     94         }
     95     }
     96     public class Korea : Person
     97     {
     98         public Korea(string name)
     99             : base(name)
    100         {
    101 
    102         }
    103 
    104 
    105         public override void SayHello()
    106         {
    107             Console.WriteLine("我是棒之思密达,我叫{0}", this.Name);
    108         }
    109     }
    110     public class American : Person
    111     {
    112         public American(string name)
    113             : base(name)
    114         {
    115 
    116         }
    117 
    118         public override void SayHello()
    119         {
    120             Console.WriteLine("我叫{0},我是米国人", this.Name);
    121         }
    122     }
    123 
    124 
    125     public class English : Person
    126     {
    127         public English(string name)
    128             : base(name)
    129         { }
    130 
    131         public override void SayHello()
    132         {
    133             Console.WriteLine("我是英国人");
    134         }
    135     }
    136 
    137 }
    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             //RealDuck rd = new RealDuck();
     15             //rd.Bark();
     16             //MuDuck md = new MuDuck();
     17             //md.Bark();
     18             //XPDuck xd = new XPDuck();
     19             //xd.Bark();
     20             //Console.ReadKey();
     21 
     22             RealDuck rd = new RealDuck();
     23             MuDuck md = new MuDuck();
     24             XPDuck xd = new XPDuck();
     25             RealDuck[] ducks = { rd, md, xd };
     26             for (int i = 0; i < ducks.Length; i++)
     27             {
     28                 ducks[i].Bark();
     29             }
     30             Console.ReadKey();
     31 
     32 
     33             //经理十一点打卡 员工9点打卡 程序猿不打卡
     34             //Employee emp = new Employee();
     35             //Manager mg = new Manager();
     36             //Programmer pm = new Programmer();
     37             //Employee[] emps = { emp, mg, pm };
     38             //for (int i = 0; i < emps.Length; i++)
     39             //{
     40             //    emps[i].DaKa();
     41             //}
     42             //Console.ReadKey();
     43 
     44 
     45 
     46             //狗狗会叫 猫咪也会叫
     47 
     48         }
     49     }
     50 
     51     public class Animal
     52     {
     53         public void Bark()
     54         { 
     55             
     56         }
     57     }
     58 
     59 
     60     public class Employee
     61     {
     62         public virtual void DaKa()
     63         {
     64             Console.WriteLine("九点打卡");
     65         }
     66     }
     67 
     68     public class Manager : Employee
     69     {
     70         public override void DaKa()
     71         {
     72             Console.WriteLine("经理11点打卡");
     73         }
     74     }
     75 
     76     public class Programmer : Employee
     77     {
     78         public override void DaKa()
     79         {
     80             Console.WriteLine("程序猿不打卡");
     81         }
     82     }
     83 
     84 
     85 
     86 
     87 
     88     public class RealDuck
     89     {
     90         public virtual void Bark()
     91         {
     92             Console.WriteLine("真的鸭子嘎嘎叫");
     93         }
     94     }
     95 
     96     public class MuDuck : RealDuck
     97     {
     98         public override void Bark()
     99         {
    100             Console.WriteLine("木头鸭子吱吱叫");
    101         }
    102     }
    103 
    104     public class XPDuck : RealDuck
    105     {
    106         public override void Bark()
    107         {
    108             Console.WriteLine("橡皮鸭子唧唧叫");
    109         }
    110     }
    111 }
    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             Animal a = new Cat();//new Dog();
     16             a.Bark();
     17             
     18             Console.ReadKey();
     19         }
     20     }
     21 
     22     public abstract class Animal
     23     {
     24 
     25         public virtual void T()
     26         {
     27             Console.WriteLine("动物有声明");
     28         }
     29 
     30         private int _age;
     31 
     32         public int Age
     33         {
     34             get { return _age; }
     35             set { _age = value; }
     36         }
     37 
     38         public Animal(int age)
     39         {
     40             this.Age = age;
     41         }
     42         public abstract void Bark();
     43         public abstract string Name
     44         {
     45             get;
     46             set;
     47         }
     48 
     49      //   public abstract string TestString(string name);
     50 
     51 
     52         public Animal()
     53         { 
     54             
     55         }
     56         //public void Test()
     57         //{ 
     58         //    //空实现
     59         //}
     60     }
     61 
     62 
     63     public abstract class Test : Animal
     64     { 
     65         
     66     }
     67 
     68     public class Dog : Animal
     69     {
     70        // public abstract void Test();
     71 
     72 
     73         public override void Bark()
     74         {
     75             Console.WriteLine("狗狗旺旺的叫");
     76         }
     77 
     78         public override string Name
     79         {
     80             get
     81             {
     82                 throw new NotImplementedException();
     83             }
     84             set
     85             {
     86                 throw new NotImplementedException();
     87             }
     88         }
     89 
     90         //public override string TestString(string name)
     91         //{
     92         //    //throw new NotImplementedException();
     93         //}
     94     }
     95 
     96     public class Cat : Animal
     97     {
     98         public override void Bark()
     99         {
    100             Console.WriteLine("猫咪喵喵的叫");
    101         }
    102 
    103         public override string Name
    104         {
    105             get
    106             {
    107                 throw new NotImplementedException();
    108             }
    109             set
    110             {
    111                 throw new NotImplementedException();
    112             }
    113         }
    114     }
    115 }
    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             //使用多态求矩形的面积和周长以及圆形的面积和周长
    14             Shape shape = new Square(5, 6); //new Circle(5);
    15             double area = shape.GetArea();
    16             double perimeter = shape.GetPerimeter();
    17             Console.WriteLine("这个形状的面积是{0},周长是{1}", area, perimeter);
    18             Console.ReadKey();
    19 
    20         }
    21     }
    22 
    23     public abstract class Shape
    24     {
    25         public abstract double GetArea();
    26         public abstract double GetPerimeter();
    27     }
    28     public class Circle : Shape
    29     {
    30 
    31         private double _r;
    32         public double R
    33         {
    34             get { return _r; }
    35             set { _r = value; }
    36         }
    37 
    38         public Circle(double r)
    39         {
    40             this.R = r;
    41         }
    42         public override double GetArea()
    43         {
    44             return Math.PI * this.R * this.R;
    45         }
    46 
    47         public override double GetPerimeter()
    48         {
    49             return 2 * Math.PI * this.R;
    50         }
    51     }
    52     public class Square : Shape
    53     {
    54         private double _height;
    55 
    56         public double Height
    57         {
    58             get { return _height; }
    59             set { _height = value; }
    60         }
    61 
    62         private double _width;
    63 
    64         public double Width
    65         {
    66             get { return _width; }
    67             set { _width = value; }
    68         }
    69 
    70         public Square(double height, double width)
    71         {
    72             this.Height = height;
    73             this.Width = width;
    74         }
    75 
    76         public override double GetArea()
    77         {
    78             return this.Height * this.Width;
    79         }
    80 
    81         public override double GetPerimeter()
    82         {
    83             return (this.Height + this.Width) * 2;
    84         }
    85     }
    86 
    87 }
    View Code

    13、电脑、移动硬盘、U盘、MP3

      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_电脑_移动硬盘_U盘_MP3
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             //用多态来实现 将 移动硬盘或者U盘或者MP3插到电脑上进行读写数据
     14 
     15             //MobileDisk md = new MobileDisk();
     16             //UDisk u = new UDisk();
     17             //Mp3 mp3 = new Mp3();
     18             //Computer cpu = new Computer();
     19             //cpu.CpuRead(u);
     20             //cpu.CpuWrite(u);
     21             //Console.ReadKey();
     22 
     23             MobileStorage ms = new UDisk();//new Mp3();//new MobileDisk();//new UDisk();
     24             Computer cpu = new Computer(ms);
     25             //cpu.Ms = ms;
     26             cpu.CpuRead();
     27             cpu.CpuWrite();
     28             //Computer cpu = new Computer();
     29             //cpu.CpuRead(ms);
     30             //cpu.CpuWrite(ms);
     31             Console.ReadKey();
     32 
     33         }
     34     }
     35 
     36 
     37     /// <summary>
     38     /// 抽象的父类
     39     /// </summary>
     40     public abstract class MobileStorage
     41     {
     42         public abstract void Read();
     43         public abstract void Write();
     44     }
     45 
     46 
     47     public class MobileDisk : MobileStorage
     48     {
     49         public override void Read()
     50         {
     51             Console.WriteLine("移动硬盘在读取数据");
     52         }
     53         public override void Write()
     54         {
     55             Console.WriteLine("移动硬盘在写入数据");
     56         }
     57     }
     58     public class UDisk : MobileStorage
     59     {
     60         public override void Read()
     61         {
     62             Console.WriteLine("U盘在读取数据");
     63         }
     64 
     65         public override void Write()
     66         {
     67             Console.WriteLine("U盘在写入数据");
     68         }
     69     }
     70     public class Mp3 : MobileStorage
     71     {
     72         public override void Read()
     73         {
     74             Console.WriteLine("MP3在读取数据");
     75         }
     76 
     77         public override void Write()
     78         {
     79             Console.WriteLine("Mp3在写入数据");
     80         }
     81 
     82         public void PlayMusic()
     83         {
     84             Console.WriteLine("MP3自己可以播放音乐");
     85         }
     86     }
     87 
     88 
     89 
     90     public class Computer
     91     {
     92         private MobileStorage _ms;
     93 
     94         public MobileStorage Ms
     95         {
     96             get { return _ms; }
     97             set { _ms = value; }
     98         }
     99         public void CpuRead()
    100         {
    101             Ms.Read();
    102         }
    103 
    104         public void CpuWrite()
    105         {
    106             Ms.Write();
    107         }
    108         public Computer(MobileStorage ms)
    109         {
    110             Ms = ms;
    111         }
    112     }
    113 }
    View Code
  • 相关阅读:
    LInux环境变量讲解
    w
    ubuntu和win10双系统,关于wife问题
    python字符串的maketrans方法和translate方法实现加密解密
    ubuntu中jdk8的配置
    Java第四十五天,SpringBoot框架系列,SpringBoot基础(四)
    Java第四十五天,SpringBoot框架系列,SpringBoot基础(三)
    Java第四十五天,SpringBoot框架系列,SpringBoot基础(二)
    Java第四十五天,SpringBoot框架系列,SpringBoot基础(一)
    Java第三十七天,Mybatis框架系列,Mybatis 的连接池以及事务控制
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4713471.html
Copyright © 2011-2022 走看看