python的class是什么

590次阅读
没有评论

python的class是什么

面向对象的设计思想是从自然界中来的,因为在自然界中,类(Class)和实例(Instance)的概念是很自然的。Class是一种抽象概念,比如我们定义的Class——Student,是指学生这个概念,而实例(Instance)则是一个个具体的Student,比如,Bart Simpson和Lisa Simpson是两个具体的Student。

类和实例

类(Class)和实例(Instance)是面向对象最重要的概念。

类是指抽象出的模板。实例则是根据类创建出来的具体的“对象”,每个对象都拥有从类中继承的相同的方法,但各自的数据可能不同。

在python中定义一个类:

class Student(object):
    pass

关键字class后面跟着类名,类名通常是大写字母开头的单词,紧接着是(object),表示该类是从哪个类继承下来的。通常,如果没有合适的继承类,就使用object类,这是所有类最终都会继承下来的类。

定义好了 类,就可以根据Student类创建实例:

>>> class Student(object):
...     pass
...
>>> bart = Student() # bart是Student()的实例
>>> bart
<__main__.Student object at 0x101be77f0>
>>> Student # Student 本身是一个类
<class '__main__.Student'>

可以自由地给一个实例变量绑定属性,比如,给实例bart绑定一个name属性:

>>> bart.name = "diggzhang"
>>> bart.name'diggzhang'

类同时也可以起到模板的作用,我们可以在创建一个类的时候,把一些认为公共的东西写进类定义中去,在python中通过一个特殊的__init__方法实现:

class Student(object):
    """__init__ sample."""
    def __init__(self, name, score):
        self.name = name
        self.score = score

__init__方法的第一个参数永远都是self,表示创建实例本身,在__init__方法内部,可以把各种属性绑定到self,因为self指向创建的实例本身。

有了__init__方法,在创建实例的时候,就不能传入空的参数了,必须传入与__init__方法匹配的参数,但self不需要传,Python解释器自己会把实例变量传进去。如下面的类,在新建实例的时候,需要把name和score属性捆绑上去:

class Student(object):
    """example for __init__ function passin args."""
    def __init__(self, name, score):
        self.name = name
        self.score = score

我们直接看个实例,如果我们老老实实传name和score进去的时候,成功声明了这个实例,但是只传一个值的时候,报错:

In [1]: class Student(object):
   ...:     def __init__(self, name, score):
   ...:         self.name = name
   ...:         self.score = score
   ...:

In [2]: bart = Student('diggzhang', 99)

In [3]: bart.name
Out[3]: 'diggzhang'

In [4]: bart.score
Out[4]: 99

In [5]: bart_test = Student('max')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-97f4e2f67951> in <module>()
----> 1 bart_test = Student('max')

TypeError: __init__() takes exactly 3 arguments (2 given)

和普通函数相比,在类中定义的函数只有一点不同,就是第一个参数永远是实例变量self,并且,调用时,不用传递该参数。除此之外,类的方法和普通函数没有什么区别。

面向对象编程的一个重要特点就是数据封装。在上面的Student类中,每个实例就拥有各自的name和score这些数据。我们可以通过函数来访问这些数据,比如打印一个学生的成绩:

def print_socre(std):
    print("%s: %s" % (std.name, std.score))

print_socre(bart)

# 实际执行效果
In [7]: def print_socre(std):
   ...:         print("%s: %s" % (std.name, std.score))
   ...:

In [8]: print_socre(bart)
diggzhang: 99

既然我们创建的实例里有自身的数据,如果想访问这些数据,就没必要从外面的函数去访问,可以在Student类内部去定义这样一个访问数据的函数,这样就把“数据”给封装起来了。这些封装数据的函数和Student类本身关联起来的,我们称之为类的方法:

class Student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score
    def print_socre(self):
        print("%s: %s" % (self.name, self.score))

要定义一个类的方法,除了传入的第一个参数是self外,其它和普通函数一样。如果想调用这个方法,直接在实例变量上调用,除了self不用传递,其余参数正常传入:

>>> bart.print_score()Bart Simpson: 59

实际代码,需要在Python3环境中测试,Python2.7会报错(NameError: global name 'name' is not defined)

$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Student(object):
...     def __init__(self, name, score):
...         self.name = name
...         self.score = score
...     def print_score(self):
...         print("%s: %s" % (self.name, self.score))
...
>>> bart = Student('zhang', 99)
>>> bart.print_score()
zhang: 99
>>>

数据和逻辑都被封装起来,直接调用方法即可,但却可以不用知道内部的细节。

总结一下。

类 是创建实例的模板,而 实例 则是一个一个具体的对象,各个实例拥有的数据都互相独立,互不影响;

方法 就是与实例绑定的函数,和普通函数不同,方法可以直接访问实例的数据;

通过在实例上调用方法,我们就直接操作了对象内部的数据,但无需知道方法内部的实现细节。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2020-01-06发表,共计3485字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)