面向对象详解

一.什么是面向对象

1>面向对象是一种程序设计思想

2>面向过程和面向对象是什么?

例如要把大象放冰箱怎么做?

面向过程:打开冰箱门->把大象扔进去->关上冰箱门(如下图)

C#基础-面向对象详解

面向对象:两个对象,大象和冰箱。大象有自己的固有属性高和重量,还有两个动作分别是进冰箱和出冰箱。冰箱也有自己固有属性高和重量,动作也有开门和关门

 C#基础-面向对象详解

 

区分:

1.面向过程是解决问题的常规思路,更加符合解决问题的想法,但是随着步骤越来越多,就很难满足需求了。

2.面向对象是把东西分拆为各个对象,每个对象完成自己的核心动作,分工合作,对象之间交互完成功能。

 

二.面向对象的三大特性

1.封装
下面几行代码,就是封装一个对象,描述的是一只大象,大象有一些特性(身高和重量)和行为(进冰箱或出冰箱)

 1   public class Elephant
 2     {
 3         public int Height = 0;
 4         public int Weigth = 0;
 5         public void IntoFridge()
 6         {
 7             Console.WriteLine("进冰箱");
 8         }
 9         public void OutFridge()
10         {
11             Console.WriteLine("出冰箱");
12         }
13     }

总结:

1>把数据和行为打包到class中,就叫封装

2>封装的好处有,数据安全,提供重用性,分工合作和方便构建大型复杂的项目


2..继承

下面几行代码,就是实现了继承,大象子类继承了动物父类,其中完全一样的特性(身高,体重)和行为(吃东西)放到父类中,这样做的好处是可以去掉重复代码

大象子类可以拥有动物父类的一切公开的属性和行为,不管大象是否愿意,它都必须拥有父类的公开东西。继承也是实现多态的,接下来会讲到。

 

 1     public class Elephant: Animal
 2     {
 3         //public int Height = 0;
 4         //public int Weigth = 0;
 5         public void IntoFridge()
 6         {
 7             Console.WriteLine("进冰箱");
 8         }
 9         public void OutFridge()
10         {
11             Console.WriteLine("出冰箱");
12         }
13         //public void CanEat()
14         //{
15         //    Console.WriteLine("我可以吃东西");
16         //}
17     }
18     public class Animal
19     {
20         public int Height = 0;
21         public int Weigth = 0;
22         public void Eat()
23         {
24             Console.WriteLine("我可以吃东西");
25         }
26     }

3.多态

1>说白了,可以理解为有相同的变量,相同的操作,但是不同的实现
2>多态种类包括:方法的重载;接口&&实现;抽象&&实现 ;继承

例如下图:都是实例化的Animal,都是调用的Eat方法,一个执行的是大象的吃方法,一个执行的是人的吃方法,这就可以理解为相同的变量,相同的操作,实现方式不同,这就是多态啦!是运行时多态(程序运行时,实现的多态)

C#基础-面向对象详解

C#基础-面向对象详解
 1 using System;
 2 
 3 namespace _002_Attribute
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Animal animal = new Elephant();
10             animal.Eat(); 
11             Animal animal1 = new People();
12             animal1.Eat();
13             Console.Read();
14         }
15     }
16     public class Elephant : Animal
17     {
18         public void IntoFridge()
19         {
20             Console.WriteLine("进冰箱");
21         }
22         public void OutFridge()
23         {
24             Console.WriteLine("出冰箱");
25         }
26         public override void Eat()
27         {
28             Console.WriteLine("我是大象,我吃植物");
29         }
30     }
31     public class People : Animal
32     {
33         public override void Eat()
34         {
35             Console.WriteLine("我是人,我吃米饭");
36         }
37     }
38     public abstract class Animal
39     {
40         public int Height = 0;
41         public int Weigth = 0;
42         public abstract void Eat();//抽象方法,必须在抽象类中
43     }
44 }
View Code

相关文章:

  • 2022-12-23
  • 2022-02-28
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-12
  • 2022-02-04
  • 2021-08-24
  • 2021-07-28
  • 2022-12-23
相关资源
相似解决方案