Python---面向對象1
阿新 • • 發佈:2019-03-07
土豆 out cli pri rtl shel 正在 imp sel
對象=屬性+方法
1 class Potato: 2 def __init__(self, name): 3 self.name = name 4 5 def kick(self): 6 print("我叫%s,噢,~ 誰踢我?!" % self.name) 7 8 9 p = Potato("土豆") 10 p.kick()
對象的方法調用
1 class Turtle: 2 color = ‘green‘ 3 weight = 10 4 legs = 4 5shell = True 6 mouth = ‘大嘴‘ 7 8 def climb(self): 9 print("我正在很努力地向前爬……") 10 11 def run(self): 12 print("我正在飛快的向前跑……") 13 14 def bite(self): 15 print("咬死你咬死你!!") 16 17 def eat(self): 18 print("有的吃,真滿足^_^") 19 20 def sleep(self): 21 print("困了,睡了,晚安,Zzzz") 22 23 24 tt = Turtle() 25 26 tt.climb() 27 tt.bite() 28 tt.eat() 29 tt.run() 30 tt.sleep() 31 32 if __name__ == "__main__": 33 tt.sleep()
導入與繼承
from second.duixiang001 import * class Frog(Turtle): defrun(self): print("哈哈,我不是跑,我是跳") shell = False weight = 0.5 def climb(self): print("我的體重是" + str(Frog.weight) + ‘kg‘) aa = Turtle() print("888888888888888888888") bb = Frog() bb.climb()
Python---面向對象1