MIT6.0001 笔记,LECTURE 9:Python Classes and Inheritance
本课的内容分类两个部分。 第一部分讲 getters and setters methods ,其本质还是在于 information hiding ,使用setter() 和 getter() 方法对类的数据进行读写,尤其是setter() 方法,可以对写入的数据进行判断,防止错误类型和错误范围的数据进入实体。 第二部分讲 inheritance ,定义一个父类animal,然后再得到他的子类,animal->person->student,或者 animal -> rabit。着重解释了class variable和instance variable的区别。 本篇笔记和代码都来自于MIT的公开课,第九课,Python的类和继承。 《Introduction to Computer Science and Programming in Python》 。 Getter and Setter Methods 这一部分用三张ppt内容可以概括。 推荐 使用getter和setter来访问类的变量。 出于information hiding的考虑,类的作者自定义变量名,调用者不知道,直接访问会出错。 使用 a.get_age() 而不是 a.age 是一种更好的变成风格,易于维护,不易出错 python允许我们在类的定义之外,直接读,写,增加实体的变量。但这是 不推荐 的。 点赞 收藏