.Net OOP详解

什么是OOP

  OOP是开发程序中必须要使用的,而且面试的时候也经常会问到这个问题。应该很多人都知道,OOP的三大特点:封装、继承、多态。

首先,什么是OOP,OOP: Object Oriented Programming,面向对象的程序设计。所谓“对象”在显式支持面向对象的语言中,一般是指类在内存中装载的实例,具有相关的成员变量和成员函数(也称为:方法)。打个比方说,把一个人看做一个对象,那么这个人的年龄、身高、体重等等属性则为成员变量,这个人的特长、爱好(比如我打篮球很厉害)则为成员函数。

OOP的特性

OOP达到了软件工程的三个目标:重用性、灵活性和可扩展性。最后还是从OOP的三大特点封装、继承、多态中体现。

封装:类和对象本身就是封装的体现,比如说:1、属性封装了字段;2、方法的多个参数封装成一个对象;3、把一堆代码封装到一个类里面;4、把一个功能封装到几个类中。总的来说就是将一些具有相同功能的代码封装到一个程序集中,只保留部分接口和方法与外部联系。继承增加了代码的可扩展性。

继承:子类自动继承其父类中的属性和方法,并可以添加新的属性和方法或者对部分属性和方法进行重写,使子类变得更简单。继承增加了代码的重用性。

多态:多个子类中虽然都具有同一个方法,但是这些子类实例化的对象调用这些相同方法后却可以获得完全不同的结果,多态增加的代码的灵活性。

OOP思想

面向对象编程技术的关键性观念是它将数据及对数据的操作行为放在一起,作为一个相互依存、不可分割的整体——对象。对于相同类型的对象进行分类、抽象后,得出共同的特征而形成了类。面向对象编程就是定义这些类。类是描述相同类型的对象集合。类定义好之后将作为数据类型用于创建类的对象。程序的执行表现为一组对象之间的交互通信。对象之间通过公共接口进行通信,从而完成系统功能。类中声明的public成员组成了对象的对外公共接口。

案例

封装:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace What_is_OOP
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Recode(new Student() { Name = "MaxJoker", Age = 22, Phone = "1808208820" });//使用对象初始化器构建一个对象闯入方法中
14         }
15         static void Recode(string name, int age, string phone) { } //用这种方法写调用起来麻烦
16         static void Recode(Student student) { } //这里体现将方法中的多个字段封装成一个对象
17 
18     }
19 
20     class Student
21     {
22         public string Name { get; set; }
23         public int Age { get; set; }
24         public string Phone { get; set; }
25     }
26 
27     class Person
28     {
29         //定义一个字段
30         private int _age;
31         //定义一个属性,此时属性封装了字段
32         public int Age
33         {
34             get { return _age; }
35             set
36             {
37                 //对年龄进行限制,这就是在调用对象是不直接使用字段的原因。
38                 if (value < 0 || value > 150)
39                 {
40                     throw new Exception("年龄过大");
41                 }
42                 else
43                 {
44                     _age = value;
45                 }
46             }
47         }
48     }
49 }
View Code

继承:继承具有单根性和传递性,单根性:只能有一个父类;传递性:比如一个方法可以从父类的父类中获取。继承的话想对好理解,就写一写怎样调用父类中的带参数的构造函数和使用this调用自己的构造函数。

 1 class Person
 2     {
 3         public Person(string name,int age,string phone)
 4         {
 5             this.Name = name;
 6             this.Age = age;
 7             this.Phone = phone;
 8         }
 9         public string Name { get; set; }
10         public int Age { get; set; }
11         public string Phone { get; set; }
12     }
13 
14     class Student : Person
15     {
16         //在子类的构造函数中通过:base()的方式,明确指明要调用父类中的那个构造函数
17         public Student(string name, int age, string phone,string studentCode)
18             : base(name, age, phone) //:base表示调用父类构造函数,父类中的构造函数是不能被继承的,只能通过子类去调用他
19         {
20             this.StudentCode = studentCode;
21         }
22         public string StudentCode { get; set; }
23 
24     }
View Code
 1 class Person
 2     {
 3         public Person(string name, int age, string phone)
 4             : this(name, age, phone, string.Empty)  //使用this调用自己的构造函数
 5         {
 6             //this.Name = name;
 7             //this.Age = age;
 8             //this.Phone = phone;
 9         }
10 
11         public Person(string name, int age)
12             : this(name, age, String.Empty, String.Empty)
13         {
14             //this.Name = name;
15             //this.Age = age;
16         }
17 
18         public Person(string name, int age, string phone, string email)
19         {
20             this.Name = name;
21             this.Age = age;
22             this.Phone = phone;
23             this.Email = email;
24         }
25         public string Name { get; set; }
26         public int Age { get; set; }
27         public string Phone { get; set; }
28         public string Email { get; set; }
29     }
View Code

多态:

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Person p1=new American();
 6             Person p2=new Chinese();
 7             p1.SayNationality();
 8             p2.SayNationality();
 9             Console.ReadKey();
10         }
11 
12     }
13 
14     class Person
15     {
16         public string Name { get; set; }
17         public int Age { get; set; }
18         public string Phone { get; set; }
19         public string Email { get; set; }
20 
21         public virtual void SayNationality() { }
22     }
23 
24     class American : Person
25     {
26         public override void SayNationality()
27         {
28             Console.WriteLine("我是美国人");
29         }
30     }
31 
32     class Chinese:Person
33     {
34         public override void SayNationality()
35         {
36             Console.WriteLine("我是中国人");
37         }
38     }
View Code
原文地址:https://www.cnblogs.com/MaxJoker/p/5645558.html