小白的Python学习笔记(十二)面向对象编程OOP深度总结《1》

Python面向对象编程学习备忘录

OOP编程是什么

你们好,做为小白,最近学习了不少Python OOP编程的知识,由于脑容量有限,特此一一按照学习顺序记录下来,若是哪里有错误,还请大神尽快指出,以避免误导他人。。。java

首先让咱们简单了解一下何为面向对象编程:python

把一组数据结构和处理它们的方法组成对象(object),把相同行为的对象概括为类(class),经过类的封装(encapsulation)隐藏内部细节,经过继承(inheritance)实现类的特化(specialization)和泛化(generalization),经过多态(polymorphism)实现基于对象类型的动态分派。c++

这样一说貌似有些复杂,简单来看的话能够参考下面的解释:git

常见概念一览

概念 解释
类(Class) 用来描述具备相同的属性和方法的对象的集合。它定义了该集合中每一个对象所共有的属性和方法。对象是类的实例
类变量 类变量在整个实例化的对象中是公用的。类变量定义在类中且在函数体以外。类变量一般不做为实例变量使用
数据成员 类变量或者实例变量, 用于处理类及其实例对象的相关的数据
方法重写 若是从父类继承的方法不能知足子类的需求,能够对其进行改写,这个过程叫方法的覆盖(override),也称为方法的重写
局部变量 定义在方法中的变量,只做用于当前实例的类
实例变量 在类的声明中,属性是用变量来表示的。这种变量就称为实例变量,是在类声明的内部可是在类的其余成员方法以外声明的
继承 即一个派生类(derived class)继承基类(base class)的字段和方法。继承也容许把一个派生类的对象做为一个基类对象对待。例如,有这样一个设计:一个Dog类型的对象派生自Animal类,这是模拟"是一个(is-a)"关系(例图,Dog是一个Animal)
实例化 建立一个类的实例,类的具体对象
方法 类中定义的函数
对象 经过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法

定义一个类

下面让咱们简单定义一个汽车类:github

class Car:
    def __init__(self, color, model, year):

        self.color = color
        self.model = model
        self.year = year
复制代码

这里咱们建立了一个汽车类Car,它有三个公共属性,分别是color(颜色),model(型号),year(生产年份)编程

建立实例对象,访问属性

如今让咱们新建一个对象my_car:bash

my_car = Car("yellow", "beetle", 1967)
复制代码

查看一下my_car的属性数据结构

print(f" My {my_car.color} car {my_car.model} is made in {my_car.year}")
# My yellow car beetle is made in 1967
复制代码

添加新属性

咱们想要给my_car添加一个新属性wheelsssh

my_car.wheels = 5
print(f"Wheels: {my_car.wheels}")
# Wheels: 5
复制代码

使用dir(my_car)可让咱们确认一下属性是否存在:ide

dir(my_car)

Out:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'color',
 'model',
 'wheels',     <====已经添加成功啦
 'year']

复制代码

类变量,修改类变量的值

在Python中,咱们在类外声明一个类变量,下面让咱们修改一下Car类:

class Car:
    wheels = 0
    def __init__(self, color, model, year):
        self.color = color
        self.model = model
        self.year = year
复制代码

这样的话,咱们在调用wheels这个变量时,能够经过实例,或者直接调用Car.wheels:

my_car = Car("yellow", "beetle", 1967)
print(f"My car is {my_car.color}")
print(f"It has {Car.wheels} wheels")
print(f"It has {my_car.wheels} wheels")

Out:
My car is yellow
It has 0 wheels
It has 0 wheels

复制代码

这里须要注意一下,若是想要经过my_car.wheels =xxx来修改wheels的值,不会真正修改类变量wheels的值,咱们来看一个具体的例子:

my_car = Car("yellow", "Beetle", "1966")
my_other_car = Car("red", "corvette", "1999")

print(f"My car is {my_car.color}")
print(f"It has {my_car.wheels} wheels")


print(f"My other car is {my_other_car.color}")
print(f"It has {my_other_car.wheels} wheels")

Out:
My car is yellow
It has 0 wheels
My other car is red
It has 0 wheels
复制代码

咱们首先建立两个实例my_car 和my_other_car ,默认的wheels=0,下面咱们首先直接经过Car这个类来修改类变量的值:

# Change the class variable value

Car.wheels = 4

print(f"My car has {my_car.wheels} wheels")
print(f"My other car has {my_other_car.wheels} wheels")

Out:
My car has 4 wheels
My other car has 4 wheels

复制代码

能够看到这样修改的话,Car类拥有的全部实例中的wheels值会被所有修改,若是咱们经过my_other_car 来修改呢?

# Change the instance variable value for my_car

my_car.wheels = 5
print(f"My car has {my_car.wheels} wheels")
print(f"My other car has {my_other_car.wheels} wheels")

Out:
My car has 5 wheels
My other car has 4 wheels

复制代码

如今你们能够发现区别了,仅仅是修改了my_car中wheels的值,对类自己不会形成影响

私有和公有属性

在Python中的全部属性都是public,可能有c++和java的同窗以为神奇,其实python最初规定了一种特殊的命名方式来区分public仍是private,那就是下划线_

我仍是拿同样的例子说明:

class Car:
    wheels = 0
    def __init__(self, color, model, year):
        self.color = color
        self.model = model
        self.year = year
        self._cupholders = 6

my_car = Car("yellow", "Beetle", "1969")
print(f"It was built in {my_car.year}")

Out:
It was built in 1969
复制代码

这里Car类中的杯托 _cupholders就是“私有“属性,为何我这里加上了引号,是由于Python只是名义上规定这种写法,可是在实际访问上没啥卵用,依然能够直接用._cupholders来访问:

my_car.year = 1966
print(f"It was built in {my_car.year}")
print(f"It has {my_car._cupholders} cupholders.")

Out:
It was built in 1966
It has 6 cupholders.

复制代码

后来Python决定使用双下划线__来替换单下划线,这样能够最大程度避免“意外访问“,然而仍是没有卵用,再来展现一下新方案:

class Car:
    wheels = 0
    def __init__(self, color, model, year):
        self.color = color
        self.model = model
        self.year = year
        self.__cupholders = 6

复制代码

其实某种程度上,这回效果仍是很明显的,若是咱们还像刚才同样尝试调用my_car.cupholders 会报错:

my_car = Car("yellow", "Beetle", "1969")
print(f"It was built in {my_car.year}")
print(f"It has {my_car.__cupholders} cupholders.")


Out:
It was built in 1969

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-108-1efe56f0c054> in <module>
 1 my_car = Car("yellow", "Beetle", "1969")
 2 print(f"It was built in {my_car.year}")
----> 3  print(f"It has {my_car.__cupholders} cupholders.")

AttributeError: 'Car' object has no attribute '__cupholders'
复制代码

这个错误颇有意思,为何会说cupholders这个变量不存在呢 ? 由于当Python看到__ 时,会自动在cupholders前面补上一个下划线_和所属类名,也就是说,这里咱们尝试用my_car.__cupholders 来调用时,Python默认的正确写法是 my_car._Car__cupholders,如今再试一下:

print(f"It has {my_car._Car__cupholders} cupholders")
Out: It has 6 cupholders
复制代码

看见没,依然没拦住。。。。 不过我我的认为这种规定公有私有变量的方式也是好处多多,这里就仁者见仁,智者见智了~

访问权限管理

就像刚刚提到的,Python全部的东西都是公有的,咱们能够随意的新增,修改,甚至删除变量:

my_car = Car("yellow", "beetle", 1969)
print(f"My car was built in {my_car.year}")

my_car.year = 2003
print(f"It was built in {my_car.year}")

del my_car.year
print(f"It was built in {my_car.year}")

Out:
My car was built in 1969
It was built in 2003

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-110-46914b0bae82> in <module>
 6 
 7 del my_car.year
----> 8  print(f"It was built in {my_car.year}")

AttributeError: 'Car' object has no attribute 'year'

复制代码

那咱们如何才能控制属性的访问权限呢?Python给出的答案是装饰器 @property,这个相似于Java中的setter和getter,如今咱们试试:

class Car:
    def __init__(self, color, model, year):
        self.color = color
        self.model = model
        self.year = year
        self._voltage = 12

 @property
    def voltage(self):
        return self._voltage

 @voltage.setter
    def voltage(self, volts):
        print("Warning: this can cause problems!")
        self._voltage = volts

 @voltage.deleter
    def voltage(self):
        print("Warning: the radio will stop working!")
        del self._voltage
复制代码

咱们新增了voltage(电压)这个属性,并用property来控制外部的访问权限,这里咱们定义了三个方法,利用setter方法能够改变voltage的值,利用getter方法来访问,利用deleter方法实现删除,接下来让咱们新建实例来看看propert是如何工做的:

my_car = Car("yellow", "beetle", 1969)
print(f"My car uses {my_car.voltage} volts")

my_car.voltage = 6
print(f"My car now uses {my_car.voltage} volts")

del my_car.voltage

Out:
My car uses 12 volts
Warning: this can cause problems!
My car now uses 6 volts
Warning: the radio will stop working!
复制代码

能够发现,咱们这里直接使用.voltage 而不是._voltage,这样就告诉python去使用property装饰的方法,咱们能够经过使用@.setter and @.deleter 使属性变为read-only(只读),从而保护voltage不会被随意修改和删除

总结

今天主要总结了OOP编程中的类,对象,属性,公有私有属性,访问权限这些基础概念,下一篇文章会进一步深刻,若是本文有哪些语言使用不当,但愿你们能够指出,让咱们一块儿进步!

我以前的一些文章已经放到了Github上,若是感兴趣的朋友能够去看看,连接以下:

相关文章
相关标签/搜索