Abstract 封装,继承,多态

封装

把一些 数据(字段,属性) 和 行为(方法) 包裹在一个单元---Class里;对外屏蔽细节(private...);对内公开一些属性和方法,具体是怎么做的不需要告诉用户。

访问修饰符:

public 公开访问

protected 子类访问

internal 当前类库访问

protected internal 子类 且 是 当前类库 访问

private 私有访问

继承

通过继承,子类可以拥有父类一切的属性和方法,

任何父类出现的地方都可以用子类代替。

英文原话其实说的是:
The Liskov Substitution Principle : 
 (LSP, lsp) is a concept in Object Oriented Programming that states: Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
 使用任何父类指针或者引用的地方都可以用子类来替换而不用知道子类是什么。

还可以增加一些父类没有的属性和方法。

好处:代码的重用

多态

多态是面向对象的重要特性,

简单点说:“一个接口,多种实现”,就是同一种事物表现出的多种形态。

非虚方法的调用,由编译时决定
   虚方法的调用,由运行时决定
ParentClass instance = new ChildClass();

instance.CommonMethod(); 普通方法的调用(非虚方法),调用父类的,由父类指针决定。
instance.VirtualMethod(); 虚方法的调用,调用子类的,运行时决定。
instance.AbstractMethod(); 抽象方法的调用,父类未实现,调用子类的。
  1 using MyAbstract.Abstract;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace MyAbstract
  9 {
 10     /// <summary>
 11     /// 封装
 12     /// </summary>
 13     public class People
 14     {
 15         public int Id { get; set; }
 16         public string Name { get; set; }
 17         protected int Salary { get; set; }
 18 
 19         /// <summary>
 20         /// 呼吸:其实是身体多个部位协同工作的
 21         /// </summary>
 22         public void Breath()
 23         {
 24             Console.WriteLine("Breath");
 25         }
 26 
 27         // 呼   吸
 28         //public  
 29         //protected 子类访问
 30         //internal   当前类库访问
 31         //protected internal   
 32         //private 
 33     }
 34     /// <summary>
 35     /// 学生
 36     /// </summary>
 37     public class Student : People
 38     {
 39         public new void Breath()
 40         {
 41             Console.WriteLine("Breath");
 42         }
 43 
 44         private int Tall { get; set; }
 45 
 46         public void Study()
 47         {
 48             //base.Salary
 49             Console.WriteLine("学习.Net高级开发");
 50         }
 51         public void PlayLumia(Lumia phone)
 52         {
 53             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 54             phone.Call();
 55             phone.Photo();
 56         }
 57         public void PlayiPhone(iPhone phone)
 58         {
 59             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 60             phone.Call();
 61             phone.Photo();
 62         }
 63 
 64         public void PlayiPhone(Oppo phone)
 65         {
 66             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 67             phone.Call();
 68             phone.Photo();
 69         }
 70 
 71         /// <summary>
 72         /// 面向基类  
 73         /// </summary>
 74         /// <param name="phone"></param>
 75         public void PlayPhone(AbstractPhone phone)
 76         {
 77             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 78             phone.Call();
 79             phone.Photo();
 80         }
 81 
 82         public void PlayPhone<T>(T t) where T : AbstractPhone//基类
 83         {
 84             Console.WriteLine("This is {0} play {1}", this.Name, t.GetType());
 85             t.Call();
 86             t.Photo();
 87         }
 88 
 89     }
 90 
 91     /// <summary>
 92     /// 老师
 93     /// </summary>
 94     public class Teacher : People
 95     {
 96         private int Tall { get; set; }
 97 
 98         public void Teach()
 99         {
100             Console.WriteLine("教授.Net高级开发");
101         }
102 
103         public void PlayiPhone(iPhone phone)
104         {
105             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
106             phone.Call();
107             phone.Photo();
108         }
109     }
110 }
View Code

 -----------------------------------------------------

方法类型:

void,int...普通方法----大家都是一样的

virtual 虚方法 (可以用override重写)---大家都有,但个别子类不一样,需要重写。

abstract抽象方法 (需要添加override覆写)---用于约束子类,大家都有,但又各不相同

 重载方法:方法名一样,但参数不一致(个数,位置,类型)

 在派生类里,出现有与父类一样的方法,添加 new 则可以隐藏父类的方法。

sealed 密封,可以防止 override 覆写

--------------------------------------------------------

 抽象类:一个类,里面可以包含一切类的东西。

接口与抽象类:

1 抽象:(可以是实现的,也可以是没有实现的。还可以包括字段,属性,方法)

 1     public abstract class AbstractPhone //有抽象方法必须是抽象类
 2     {
 3         public int Id { get; set; }
 4         protected string Brand;
 5 
 6         /// <summary>
 7         /// 系统
 8         /// </summary>
 9         /// <returns></returns>
10         public abstract string System(); //抽象方法---不同的子类有不同的实现---不同的操作系统
11 
12         /// <summary>
13         /// 打电话
14         /// </summary>
15         public void Call()
16         {
17             Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand, this.System());
18         }
19 
20         /// <summary>
21         /// 拍照:oppo唯一柔光双摄
22         /// </summary>
23         public virtual void Photo()
24         {
25             Console.WriteLine("User{0} {1} {2} Photo", this.GetType().Name, this.Brand, this.System());
26         }
27     }

具体实例:

  1 /// <summary>
  2     /// 封装
  3     /// </summary>
  4     public class People
  5     {
  6         public int Id { get; set; }
  7         public string Name { get; set; }
  8         protected int Salary { get; set; }
  9 
 10         /// <summary>
 11         /// 呼吸:其实是身体多个部位协同工作的
 12         /// </summary>
 13         public void Breath()
 14         {
 15             Console.WriteLine("Breath");
 16         }
 17 
 18         // 呼   吸
 19         //public  
 20         //protected 子类访问
 21         //internal   当前类库访问
 22         //protected internal   
 23         //private 
 24     }
 25     /// <summary>
 26     /// 学生
 27     /// </summary>
 28     public class Student : People
 29     {
 30         public new void Breath()
 31         {
 32             Console.WriteLine("Breath");
 33         }
 34 
 35         private int Tall { get; set; }
 36 
 37         public void Study()
 38         {
 39             //base.Salary
 40             Console.WriteLine("学习.Net高级开发");
 41         }
 42         public void PlayLumia(Lumia phone)
 43         {
 44             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 45             phone.Call();
 46             phone.Photo();
 47         }
 48         public void PlayiPhone(iPhone phone)
 49         {
 50             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 51             phone.Call();
 52             phone.Photo();
 53         }
 54 
 55         public void PlayiPhone(Oppo phone)
 56         {
 57             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 58             phone.Call();
 59             phone.Photo();
 60         }
 61 
 62         /// <summary>
 63         /// 面向基类  
 64         /// </summary>
 65         /// <param name="phone"></param>
 66         public void PlayPhone(AbstractPhone phone)
 67         {
 68             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 69             phone.Call();
 70             phone.Photo();
 71         }
 72 
 73         public void PlayPhone<T>(T t) where T : AbstractPhone//基类
 74         {
 75             Console.WriteLine("This is {0} play {1}", this.Name, t.GetType());
 76             t.Call();
 77             t.Photo();
 78         }
 79 
 80     }
 81 
 82     /// <summary>
 83     /// 老师
 84     /// </summary>
 85     public class Teacher : People
 86     {
 87         private int Tall { get; set; }
 88 
 89         public void Teach()
 90         {
 91             Console.WriteLine("教授.Net高级开发");
 92         }
 93 
 94         public void PlayiPhone(iPhone phone)
 95         {
 96             Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
 97             phone.Call();
 98             phone.Photo();
 99         }
100     }
类之间的继承
 1 {
 2                         //AbstractPhone phone = new AbstractPhone();
 3                         AbstractPhone iPhone = new iPhone();
 4                         AbstractPhone lumia = new Lumia();
 5                         AbstractPhone oppo = new Oppo();
 6                         {
 7                             Teacher teacher = new Teacher()
 8                             {
 9                                 Id = 11,
10                                 Name = "Eleven"
11                             };
12 
13                             //teacher.PlayiPhone(iPhone);
14 
15                             Student student = new Student()
16                             {
17                                 Id = 809,
18                                 Name = "彭彭"
19                             };
20                             
21 
22                             student.PlayPhone(iPhone);
23                             student.PlayPhone(lumia);
24                             student.PlayPhone(oppo);
25                         }
26                     }

2 接口:告诉你能做什么,不实现

在子类中,有的有某个方法,有的又没有(某个方法在子类中是同一个)

 这就需要用到接口了,子类可以根据需要来是否继承实现该方法。

 1 /// <summary>
 2     /// 接口:不是类,里面不能包含具体实现
 3     /// 接口可以多实现
 4     /// 必须显式的实现接口的方法
 5     /// 
 6     /// can do
 7     /// </summary>
 8     public interface IExtend : IExtendExtend
 9     {
10         /// <summary>
11         /// 指纹支付
12         /// </summary>
13         void FingerprintPayment();
14 
15         string Remark { get; set; }
16         //string Description;
17 
18         event Action NothingEvent;
19 
20         //delegate void DoNothing();
21 
22     }
23     public interface IExtendExtend
24     {
25         //void Do();
26     }
接口

小结:对于共有的东西,用抽象类。对于有的有,有的又没有的,根据具体需求而定的用接口,在继承它实现。

 比如手机共有的功能可以写在基类里(是抽象类),子类继承实现。

对于有的手机又指纹支付功能,有的又没有,则用接口封装。对于有此功能的手机,继承它并且实现它即可。

原文地址:https://www.cnblogs.com/anwser-jungle/p/8511466.html