class ren():
‘‘‘this class is about ren class。类的说明,使用三个单引号‘‘‘
def __init__(self,name,sex): # 构造器
# 注意到 __init__ 方法的第一个参数永远是 self ,表示创建的实例本身
# 因此,在 __init__ 方法内部,就可以把各种属性绑定到 self
# 因为self 就指向创建的实例本身
# 有了 __init__ 方法,在创建实例的时候,就不能传入空的参数了
# 必须传入与 __init__ 方法匹配的参数,
# 但 self 不需要传,python解释器会把实例变量传进去
self.name=name
self.sex=sex
def hello(self):
print(‘hello {0}‘.format(self.name))
test=ren(‘zhouyuyao‘,‘F‘) # 需输入参数
test.hello()返回结果:
hello zhouyuyao
原文:http://shaoniana.blog.51cto.com/11471609/1980561