读书人

Python 入门教程 17 - Introduction t

发布时间: 2013-10-06 18:25:14 作者: rapoo

Python 入门教程 17 ---- Introduction to Classes


第一节

1 介绍了Python中类的结构

class NewClass(object):

stratement

第二节

1 介绍了类的初始化函数__init__(self)

2 Python中所有的类的初始化函数都是__init__(self),第一个参数表示的是本身的对象,就像C++的this指针

3 练习:写一个类名为Animal,初始化函数的函数体内容为pass

class Employee(object):    """Models real-life employees!"""    def __init__(self, employee_name):        self.employee_name = employee_name    def calculate_wage(self, hours):        self.hours = hours        return hours * 20.00# Add your code below!class PartTimeEmployee(Employee):    def calculate_wage(self, hours):        self.hours = hours        return hours*12.00


第七节

1 类名应该严格是以大写字母开头

2 对于类的所有成员函数来说,至少都要有一个参数是self

3 对于类的变量来说值应该是事先就确定的,就像C++的静态成员变量,我们可以通过类名直接访问到,不用实例化

4 对于类的实例化变量来说我们是需要实例化出对象后才能访问到

5 在类的所有成员函数中,无论是访问类的变量还是类的实例化变量我们都是通过self.variable





读书人网 >perl python

热点推荐