python 访问对象的属性与方法
class Person(object): name = "stu" age = 10 height = 160 weight = 90 def run(self): print("run") def eat(self, food): print("eat " + food) def openDoor(self): print("我已经打开了冰箱门") def fillEle(self): print("我已经把大象装进冰箱了") def closeDoor(self): print("我已经关闭了冰箱门")per = Person()‘‘‘访问属性格式:对象名.属性名赋值:对象名.属性名 = 新值‘‘‘per.name = "tom"per.age = 18per.height = 160per.weight = 80print(per.name, per.age, per.height, per.weight)‘‘‘访问方法格式:对象名.方法名(参数列表)‘‘‘per.openDoor()per.fillEle()per.closeDoor()per.eat("apple")#问题:目前来看Person创建的所有对象属性都是一样的per2 = Person()print(per2.age)per3 = Person()print(per3.age)
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16