class persion: def info (self,name,age): print "Your name is %s,you are %s years old" %(name,age) P=persion() P.info(‘TOM‘,25)
类和函数的区别就是,多一个参数 self, 指对象本身。
实例:
方法:
类的初始化
class persion: def __init__(self,nationnality): self.country=nationnality def info (self,name,age): print "Your name is %s,you are %s years old, you are from %s" %(name,age,self.country) P=persion(‘CHINA‘) P.info(‘TOM‘,25)
时间: 2024-10-10 08:27:31