父类:
class People(): # 最好用大写
# 定义属性
def __init__(self, name, age): # 当调用People这个类时,__init__会自动执行,不需要调用:输出print语句,同时将()内的初始化属性进行自动传递
# print("Very Good")
self.mingzi = name # 获取到name的值并自动传递给self.mingzi
self.age = age
self.country = "China"
self.eat = "banana"
self.live="earth" #属性默认值,人类生存地:地球。山东人:国籍中国
# 定义行为
def speak(self):
print(self.mingzi + ": I love Chain")
print("I am eating " + self.eat)
def run(self):
print("I am running and I am " + str(self.age))
def change(self, x):
self.eat = x # 修改前提,__init__里面有self.eat
def think(self,what):
print("I am thinking"+what)
子类:
使用前提:
子类与父类在同一py文档下
class Robot(People): #继承格式
def __init__(self,name,age,what): #所带属性,若没有新的属性可以不写
super().__init__(name,age) #继承属性
self.what=what
self.need="ele" #默认属性
def walk(self):
print(self.mingzi+" :I am walking")
def think(self): #修改父类
print("Sorry,Ican't think")