对象 = 属性 + 方法
- class Potato:
- def __init__(self, name):
- self.name = name
- def kick(self):
- print("我叫 %s, 噢,~ 谁踢我?!" % self.name)
- p = Potato("土豆")
- p.kick()
对象的方法调用
- class Turtle:
- color = 'green'
- weight = 10
- legs = 4
- shell = True
- mouth = '大嘴'
- def climb(self):
- print("我正在很努力地向前爬......")
- def run(self):
- print("我正在飞快的向前跑......")
- def bite(self):
- print("咬死你咬死你!!")
- def eat(self):
- print("有的吃, 真满足 ^_^")
- def sleep(self):
- print("困了, 睡了, 晚安, Zzzz")
- tt = Turtle()
- tt.climb()
- tt.bite()
- tt.eat()
- tt.run()
- tt.sleep()
- if __name__ == "__main__":
- tt.sleep()
导入与继承
- from second.duixiang001 import *
- class Frog(Turtle):
- def run(self):
- print("哈哈, 我不是跑, 我是跳")
- shell = False
- weight = 0.5
- def climb(self):
- print("我的体重是" + str(Frog.weight) + 'kg')
- aa = Turtle()
- print("888888888888888888888")
- bb = Frog()
- bb.climb()
来源: http://www.bubuko.com/infodetail-2979022.html