python面向对象(一切皆对象)

使用面向对象的思想设计一个乌龟的角色:

  1. 表面特征:绿色、有4条腿、重10kg、有外壳等等
  2. 行为特征:爬、吃、睡觉、将头和四肢缩到壳里等等
class tortoise:
    bodycolor = "绿色"
    footnum = 4
    weight = 10
    hashshell = True

    def crawl(self):
        print("乌龟会爬")
    def eat(self):
        print("乌龟会吃东西")
    def sleep(self):
        print("乌龟在睡觉")
    def protect(self)
        print("乌龟缩进了壳里")

面向对象编程常用术语:类、对象、属性、方法

你可能感兴趣的:(python面向对象(一切皆对象))