Python的面向对象编程(一, 基本的概念,OOP的特点) 您所在的位置:网站首页 oop袋是什么意思 Python的面向对象编程(一, 基本的概念,OOP的特点)

Python的面向对象编程(一, 基本的概念,OOP的特点)

2024-01-14 17:26| 来源: 网络整理| 查看: 265

在这篇blog, 讲解Python中的面向对象编程(Object-Oriented Programming, OOP) 和基本的概念。

首先,我们来讲下面向对象编程(OOP)。

一个对象有两个特点: (1)属性(attributes) (2)行为(behavior) 一讲到面向对象编程,那么就要知道什么是类(class),什么是什么是方法(Method),什么是对象(obejct)?

Class 类(class)就是对一类有共同属性和行为的对象的总称。

比如:小轿车,面包车,客运汽车,货车,等等这一些我们可以定义一个共同的类来描述它们,Class automobile;在这个类里面我们可以定义一些描述这个车的属性和行为。

class automobile: pass Attributes 属性(attributes)是描述这个对象的一些特点,是这一类事物共有的属性。

例如:上面定义的车的类,所拥有的属性可以有size(大小), speed(速度)等等。

class automobile: def __init__(self, size, speed): self.size = size self.speed = speed

(这里的__init__()是一个初始化方法)

Method 方法(method)是描述这类对象所共有的方法(行为)。

比如:上面定义的class automobile中,我们可以定义一些描述汽车的行为,像

class automobile: def __init__(self, size, speed): self.size = size self.speed = seppd def disp_info(self): print("My size is {} t, and speed is {} KM/H".format(self.size, self.speed)) 面向对象编程有下面三个基本原则: 原则描述继承(Inheritance)A process of using details from a new class without modifying existing class.封装(Encapsulation)Hiding the private details of a class from other objects.多态(Polymorphism)A concept of using common operation in different ways for different data input. A.继承(Inheritance): (1)什么是继承?

Inheritance is a way of creating new class for using details of existing class without modifying it. The newly formed class is a derived class(or child class). Similarly, the existing class is a base class(or parent class).

(2)实例: 例1:Python中的继承 # parent class class Bird: def __init__(self): print("Bird is ready") def whoisThis(self): print("Bird") def swim(self): print("Swim faster") # child class class Penguin(Bird): def __init__(self): # call super() function super().__init__() # 这里我要补充下,上面的 super().__init__() 这种方法在单继承中是可行的,在多继承中可以用下面的方法 # Bird.__init__(self) print("Penguin is ready") def whoisThis(self): # 方法重写 print("Penguin") def run(self): print("Run faster") peggy = Penguin() peggy.whoisThis() peggy.swim() peggy.run()

当我们运行上面的程序,输出如下:

Bird is ready Penguin is ready Penguin Swim faster Run faster

(3)说明:

在上面的程序中,我们创建了两个类,Bird(父类), Penguin(子类)。子类继承父类的行为,比如 swim() 。再来,子类修改了父类的方法,比如whoisThis()。而且我们还拓展了子类,比如重新定义一个方法run()。

B.封装(Encapsulation) (1)什么是封装?

Using OOP in Python, we can restrict access to methods and variables. This prevent data from direct modification which is called encapsulation. In Python, we denote private attribute using underscore as prefix i.e single “_” or double"__".

(2)实例 例2:Pytho中的数据封装 class Computer: def __init__(self): self.__maxprice = 900 def sell(self): print("Selling Price: {}".format(self.__maxprice)) def setMaxprice(self, price): self.__maxprice = price c = Computer() c.sell() # change the price c.__maxprice = 1000 c.sell() # using setter function c.setMaxprice(1000) c.sell()

运行上面程序,结果输出为:

Selling Price: 900 Selling Price: 900 Selling Price: 1000

(3)说明:

在上面的程序,我们定义了一个类 Computer, 我们用 init() 的方法来存储电脑最高的售卖价格。我们尝试修改价格,但是我们不行,以为Python将 __maxprice 作为一个私有属性。 为了修改这个值,我们用一个设置器函数,例如这里的 setMaxPrice(), 将价格作为这个函数的参数。

多态(Polymorphism) (1)什么是多态?

Polymorphism is an ability (in OOP) to use common interface for multiple form (darta types).

Suppose, we need to color a shape, there are multiple shape option (rectangle, square, circle). However we cloud use same method to color any shape. This concept is called Polymorphism.

(2)实例: 例三:Python中的多态 class Parrot: def fly(self): print("Parrot can fly") def swim(self): print("Parrot can't swim") class Penguin: def fly(self): print("Penguin can't fly") def swim(self): print("Penguin can swim") # common interface def fly_test(bird): bird.fly() # instantiate objects blu = Parrot() peggy = Penguin() # passing the object fly_test(blu) fly_test(peggy)

运行上面的程序,输出结果如下:

Parrot can fly Penguin can’t fly

(3)说明:

在上面的程序中,我们定义了两个类 Parrot 和 Penguin。 它们都有一个共同的方法 fly(). 然而, 它们的方法内部是不同的。为了允许多态,我么创建了一个公共的接口 flying_test(),我们测试了两个对象,blu 和 peggy,flying_test() 有效。

关键点记住: @ The programming gets easy and efficient. @ The class is sharable, so code can be reused. @ The productivity of programmar increases. @ Data is safe and secure with data abstraction.

参考文献: Python Object Oriented Programming



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有