我是一个菜鸟,我只是在努力,2021正视自己,面对2021!

C#基础系列第四篇

前言: 完全是我在学习过程中记录的笔记,只不过分享一下让很多刚开始学习.net编程的人能够很快的学会C#语言

  1. 继承

(1) 继承中有两大重点

            1)构造方法的执行

            2)里氏转换原则

                   ->子类对象可以直接赋值给父类对象

                   ->指向子类对象的父类对象可以强制转换为对应的子类对象

                class MyBase

                   {

                   }

                   class MySub:MyBase

                   {

                   }

                   class Program

                   {

                          static void Main(string[] args)

                          {

                                 MySub msub = new MySub();

                                 //里氏转换原则第一条

                                 MyBase mBase = msub;

                                 //里氏转换原则第二条

                                 MySub msub1 = (MySub)mBase;

                                 MyBase m = new MyBase();

                                 m = new MySub();

                                 MySub mm = (MySub)m;

                          }

                   }

  1. is运算符

(1) 语法: 变量 is 类型 返回值是一个bool值

(2)如果变量可以转换为指定类型,则为true,否则为false

(3)如果在判断is运算符的时候对象中也有父类对象

            Person[] p = { ch, am, ko, new Person() };

            if(p[i] is 父类)

            {

                   //

            }

//应该放在最后,由于所有的对象都来源于父类,因此在循环中这个判断永远为真,参考下面的案例

  1. as运算符

(1) 使用is运算符很多情况是为了实现下面的方法

            子类 subObj;

            if(instance is 父类)

            {

                   subObj=(子类)instance;

            }

            else

            {

                   subObj=null;

            }

(2) 举例说明:

            class MyBase

            {   

            }

            class MySub : MyBase

            {

            }

            class Program

            {

                   static void Main(string[] args)

                   {

                          MySub ms = new MySub();

                          MyBase mb = ms;

                          //is用法

                          //MySub m;

                          //if (mb is MySub)

                          //{

                          //    m = (MySub)mb;

                          //}

                          //else

                          //{

                          //    m = null;

                          //}

                          //as用法

                          MySub m = mb as MySub;

                          if (m == null)

                          {

                                 Console.WriteLine("不能转换");

                          }

                          else

                          {

                                 Console.WriteLine("可以转换");

                          }

                   }

            }

  1. 一个关于继承和里氏转换原则的用法

(1) 父类只能访问父类的成员

(2)子类可以访问父类和子类的成员

(3)父类如果需要访问子类中的方法,这要考虑多态

  1. 多态

(1) 多态就是多种状态

(2)同一个函数在不同对象里有不同的实现

            1)对象没变,在不同的环境中充当不同的角色

                   ->写一个父类,写一个子类

                   ->让子类和父类有一个相同的方法

                   ->new一个子类对象

                   ->调用这个方法

                   ->将其转换为父类对象在调用这个方法

(3)隐藏基类方法,多态实现的案例

class Child

            {

                   public void Play()

                   {

                          Console.WriteLine("和同学玩等");

                   }

            }

            class Adult : Child

            {

                   public new void Play()

                   {

                          Console.WriteLine("玩游戏等");

                   }

            }

            class Program

            {

                   static void Main(string[] args)

                   {

                          #region ----多态1的实现

                          //Adult adt = new Adult();

                          //adt.Play();

                          ////第一种写法

                          ////Child ch = adt;

                          ////ch.Play();

                          ////第二种写法

                          //(adt as Child).Play();

                          #endregion

                          Child ch = new Adult();

                          ch.Play();

                          Adult ad = (Adult)ch;

                          ad.Play();

                   }

            }

隐藏基类方法

            1)隐藏基类方法使用new来引导子类方法

            2)保证子类和父类有同样的方法

            3)具体在使用的时候,对象被赋予什么类型,就调用谁定义的方法

                   class MyBase

                   {

                          private string name = "杨总监";

                          public string Name

                          {

                                 get { return name; }

                                 set { name = value; }

                          }

                          public void Func()

                          {

                                 Console.WriteLine("我是父类");

                          }

                   }

                   class MyClass:MyBase

                   {

                          public new string Name

                          {

                                 get { return "韩迎龙"; }

                          }

                          public new void Func()

                          {

                                 Console.WriteLine("我是一个类");

                          }

                   }

                   class MySub:MyClass

                   {

                          public new void Func()

                          {

                                 Console.WriteLine("我是子类");

                          }

                   }

                   class Program

                   {

                          static void Main(string[] args)

                          {

                                 MySub ms = new MySub();

                                 ms.Func();  //我是子类

                                 ((MyClass)ms).Func();  //我是一个类

                                 ((MyBase)ms).Func();  //我是父类

 

                                 MyClass m = new MyClass();

                                 MyBase mb = m;

                                 Console.WriteLine(m.Name);  //韩迎龙

                                 Console.WriteLine(mb.Name); //杨总监

                          }

                   }

(5) 重写基类方法

            1)USB接口的小案例

                   ->有一个标准的方法

                   ->方法由具体的对象来实现

                   ->实际使用的时候,动作是一样的做,但是具体的过程有实现这个方法的对象的决定

            2)如何写

                   ->写一个父类和子类

                   ->子类与父类有相同的方法

                   ->在父类方法前面加上一个virtual

                   ->在子类方法前面加上override

                   ->在使用过程中,new出子类对象,那么无论是什么类型调用方法的时候均执行子类实现的方法

                       class myBase

                          {

                                 public virtual void Func()

                                 {

                                        Console.WriteLine("我是父类提供的方法");

                                 }

                          }

                          class MySub : myBase

                          {

                                 public override void Func()

                                 {

                                        Console.WriteLine("我是子类重写的方法");

                                 }

                          }

                          class Program

                          {

                                 static void Main(string[] args)

                                 {

                                        MySub ms = new MySub();

                                        ms.Func();

                                        myBase mb = ms;

                                        mb.Func();

                                        //输出结果都是:我是子类重写的方法

                                        Console.ReadKey();

                                 }

                          }

(6) USB接口的案例代码

            class Deal

            {

                   public virtual void USB()

                   {

                          Console.WriteLine("USB协议的接口");

                   }

            }

            class iPhone5S:Deal

            {

                   public override void USB()

                   {

                          Console.WriteLine("假设只能充电");

                   }

            }

            class UDisk:Deal

            {

                   public override void USB()

                   {

                          Console.WriteLine("传输数据");

                   }

            }

            class Program

            {

                   static void Main(string[] args)

                   {

                          Console.WriteLine("请选择要使用的电子原件:1代表iPhone5S,2代表u盘");

                          Deal deal = null;

                          switch (Console.ReadLine())

                          {

                                 case "1":

                                        deal = new iPhone5S();

                                        break;

                                 case"2":

                                        deal = new UDisk();

                                        break;

                                 default:

                                        Console.WriteLine("没有使用任何可用原件");

                                        break;

                          }

                          if (deal != null)

                          {

                                 deal.USB();

                          }

                   }

            }

(7)利用重写基类方法的多态可以实现父类访问子类的方法

            1)父类定义虚方法,由子类去实现

            2)如果父类指向子类调用这个方法,那么实现的具体代码就是父类指向的那个子类所提供的方法

(8)有关new与override

     new则隐藏,over则重写

     隐藏看类型,重写只管新

(9)重写的一点说明

            1)父类virtual,子类可以不将其重写,还是可以隐藏的

            2)如果不喜欢在被重写,可以使用sealed将其变为密封的

            3)无论是重写还是隐藏,都是对子类的"直系"类进行处理

                   class MyBase

                   {

                          public virtual void Func()

                          {

                                 Console.WriteLine("MyBase");

                          }

                   }

                   class MySub : MyBase

                   {

                          public new void Func()

                          {

                                 Console.WriteLine("隐藏MySub");

                          }

                   }

                   class MySubSub : MySub

                   {

                          public override void  Func()  //这样写是错误的

                          {

                                 Console.WriteLine("隐藏MySub");

                          }

                   }

                   class Program

                   {

                          static void Main(string[] args)

                          {

                          }

                   }

posted @ 2012-10-02 00:52  Kencery  阅读(1183)  评论(0编辑  收藏  举报
友情链接:初心商城