zoukankan      html  css  js  c++  java
  • python学习日记(OOP数据封装)

    class Student(object):
        def __init__(self,name,score):
            self.name = name
            self.score = score
    li = Student('libai','99')
    print(li.name)
    print(li.score)

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

    def print_score(student):
        print('{}的成绩是:{}'.format(student.name,student.score))
    print_score(li)

    但是,既然Student实例本身就拥有这些数据,要访问这些数据,就没有必要从外面的函数去访问,可以直接在Student类的内部定义访问数据的函数,这样,就把“数据”给封装起来了。这些封装数据的函数是和Student类本身是关联起来的,我们称之为类的方法:

    class Student(object):
        def __init__(self,name,score):
            self.name = name
            self.score = score
    
        def print_score(self):
            print('{}的成绩是:{}'.format(self.name, self.score))

    要定义一个方法,除了第一个参数是self外,其他和普通函数一样。要调用一个方法,只需要在实例变量上直接调用,除了self不用传递,其他参数正常传入:

    li.print_score()

    这样一来,我们从外部看Student类,就只需要知道,创建实例需要给出namescore,而如何打印,都是在Student类的内部定义的,这些数据和逻辑被“封装”起来了,调用很容易,但却不用知道内部实现的细节。

    封装的另一个好处是可以给Student类增加新的方法,比如get_grade

    class Student(object):
        def __init__(self,name,score):
            self.name = name
            self.score = score
        def print_score(self):
            print('{}的成绩是:{}'.format(self.name, self.score))
        
        def get_grade(self):
            if self.score >= 90:
                return 'A'
            elif self.score >=60:
                return 'B'
            else:
                return 'C'

    同样的,get_grade方法可以直接在实例变量上调用,不需要知道内部实现细节:

    class Student(object):
        def __init__(self,name,score):
            self.name = name
            self.score = score
        def print_score(self):
            print('{}的成绩是:{}'.format(self.name, self.score))
    
        def get_grade(self):
            if self.score >= 90:
                return 'A'
            elif self.score >= 60:
                return 'B'
            else:
                return 'C'
    wang = Student('WangDong',92)
    liang = Student('LiangMeng',46)
    print(wang.name,wang.get_grade())
    print(liang.name,liang.get_grade())

    小结:

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

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

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

    和静态语言不同,Python允许对实例变量绑定任何数据,也就是说,对于两个实例变量,虽然它们都是同一个类的不同实例,但拥有的变量名称都可能不同:

    >>> bart = Student('Bart Simpson', 59)
    >>> lisa = Student('Lisa Simpson', 87)
    >>> bart.age = 8
    >>> bart.age
    8
    >>> lisa.age
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Student' object has no attribute 'age'

    参考源码

     student.py

  • 相关阅读:
    zookeeper基础
    4. Zookeeper单机版安装
    3. 服务中间件Dubbo
    2. Maven工程的搭建
    Maven的优点
    应用架构的演进历史 MVC、 RPC、SOA 和 微服务架构
    js 正则表达式验证
    后台正则表达式验证部分部分总结
    Linux下记录登录用户历史操作
    Django使用Ace实现在线编辑器
  • 原文地址:https://www.cnblogs.com/smallfoot/p/10172444.html
Copyright © 2011-2022 走看看