标签:__init__ color min xiaomi family print class style 添加
1、创建类
class People:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print("%s is eating food" % self.age)
def sing(self):
print("%s is singing a song" % self.name)
def nianling(self):
print("%s is %s years old" % (self.name,self.age))
2、类的实例化(即建立一个具体对象)
p1 = People("xiaoming",22) 单独有这行啥也不显示,而要配合下边的语句才能实现
p1.nianling() 显示:xiaoming is 22 years old
3、类的继承
class Man(People): #People就表示继承了上边的类
def __init__(self,name,age,money): #对父类的初始化添加新功能 money
People.__init__(self,name,age) #继承调用上边的父类,避免输入重复代码
self.money = money
print("%s 一出生就有%s money %(self.name,self.money))
def sleep(self):
print("%s is sleeping" % self.name)
m1 = Man("xiaogang",22,100) 继承之前的类,也就相当于是实例化了,所以每行都会有实现的结果
m1.sleep()
显示:
xiaogang 一出生就有 100 dollers
xiaogang is sleeping
标签:__init__ color min xiaomi family print class style 添加
原文地址:http://www.cnblogs.com/gaoxu366/p/7856780.html