zoukankan      html  css  js  c++  java
  • .NET基础回顾(三)

    一. 里氏替换原则:LSP

    定义:子类可以替换父类的位置,并且程序的功能不受影响(父类变量指向子类对象)。因为父类有的功能子类都有,所以不影响程序的功能。

    程序示例:

    1 Person p = new Person();
    2 p.SayHi();//调用父类的
    3 Student s = new Student();
    4 s.SayHi(); //调用子类的
    5 //里氏替换原则
    6 Person p1 = new Student();
    7 P1.SayHi(); //调用父类的

    二. 类型转换

    1. bool b = p is Person;判断变量所指的类的类型。

    2. as转换

             Person p = new Person();

             Student s1 = (Student)p;//可能会报异常

             //如果转换成功,引用指向s2,如果转换失败,返回null

             Student s2 = p as Student;

    三.虚方法、多态

    1. 虚方法:允许子类覆盖父类的方法。

    程序示例:

     1 //Person.cs
     2 class Person
     3     {
     4         public virtual void SayHi()
     5         {
     6             Console.WriteLine("我是人。");
     7         }
     8 }
     9 
    10 //Chinese.cs
    11 class Chinese:Person
    12     {
    13         public override void SayHi()
    14         {
    15             Console.WriteLine("我是中国人。");
    16         }
    17 }
    18 
    19 //GuangDongRen.cs
    20 class GuangDongRen:Chinese
    21     {
    22         public override void SayHi()
    23         {
    24             Console.WriteLine("我是广东人。");
    25         }
    26     }
    27 
    28 //Main
    29 static void Main(string[] args)
    30         {
    31             Person p = new GuangDongRen();
    32             p.SayHi();
    33 //输出“广东人”
    34             //如果GuangDongRen类里面没有重写SayHi()方法,则输出“中国人”
    35 
    36             Console.ReadKey();
    37         }

    2. 多态,同种行为,对于不同事物,有不同的表现。

    程序示例:

     1 //Person.cs
     2 public virtual void WashBody()
     3         {
     4             Console.WriteLine("人在洗澡");
     5         }
     6 
     7 //Boy.cs
     8 class Boy:Person
     9     {
    10         public override void WashBody()
    11         {
    12             Console.WriteLine("男孩在洗澡");
    13         }
    14     }
    15 
    16 //Girl.cs
    17 class Girl:Person
    18     {
    19         public override void WashBody()
    20         {
    21             Console.WriteLine("女孩在洗澡");
    22         }
    23 }
    24 
    25 //WashMachine.cs
    26 class WashMachine
    27     {
    28         public void Wash(Person p)
    29         {
    30             Console.WriteLine("开始洗澡");
    31             p.WashBody();
    32             Console.WriteLine("洗澡结束");
    33         }
    34     }
    35 
    36 //Main
    37 static void Main(string[] args)
    38         {
    39             Boy p1 = new Boy();
    40             Girl p2 = new Girl();
    41             WashMachine wm = new WashMachine();
    42             wm.Wash(p1);//男孩洗澡        
    43             wm.Wash(p2);//女孩洗澡
    44             Console.ReadKey();

    3. 将父类类型作为方法的返回值。(原理是什么?)

    程序示例:

     1 //Pet.cs
     2 class Pet
     3 {
     4     public virtual void Shout()
     5     {
     6         Console.WriteLine("宠物叫");
     7     }
     8 }
     9 
    10 //Cat.cs
    11 class Cat:Pet
    12 {
    13     public override void Shout()
    14     {
    15         Console.WriteLine("猫咪叫");
    16     }
    17 }
    18 
    19 //Dog.cs
    20 class Dog:Pet
    21 {
    22     public override void Shout()
    23     {
    24         Console.WriteLine("狗狗叫");
    25     }
    26 }
    27 
    28 //Shop.cs
    29 class Shop
    30 {
    31     public Pet SellPet(string type)
    32     {
    33         switch(type)
    34         {
    35             case "dog":
    36                 return new Dog();
    37             case "cat"
    38                 return new Cat();
    39             default:
    40                 return null;
    41         }
    42     }
    43 }
    44 
    45 //Main
    46 static void Main(string[] args)
    47 {
    48     Shop shop = new Shop();
    49     Pet pet = shop.SellPet("dog");
    50     pet.Shout();//输出"狗狗叫"
    51 }
  • 相关阅读:
    protobuf自解释message
    protobuf编码
    proto3语法
    proto2语法
    protobuf简介
    poi处理大EXCEL文件总结
    POI-处理大Excel文件(xlsx写)
    POI-处理大Excel文件(xlsx)
    POI-处理大Excel文件(xls)
    RedHat 6.4 RHCS GFS2安装
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4046658.html
Copyright © 2011-2022 走看看