python初级(302) 6 对象(一)
作业:
1、编写一个Dog类,并生成对象dog,属性包含颜色,大小,重量,可以汪汪叫,摇尾巴,跑
# -*- coding: utf-8 -*- class Dog: def __init__(self, color, size, weight): self.color = color self.size = size self.weight = weight def wangwang(self): text = "dog color:{},size:{},wieght:{} wangwang".\ format(self.color, self.size, self.weight) print(text) def shaketail(self): print("dog shake tail.") def run(self): print("dog run.") dog = Dog("white", 20, 5) dog.wangwang() dog.shaketail() dog.run()
2、编写一个Cat类,并生成cat对象,属性包含颜色,年龄,重量,可以喵喵叫,可以捉老鼠,会爬树
# -*- coding: utf-8 -*- class Cat: def __init__(self, color, age, weight): self.color = color self.age = age self.weight = weight self.mouse_count = 0 def miaomiao(self): print("miao miao") def catch_mouse(self): self.mouse_count += 1 print("catch a mouse, have mouse count:", self.mouse_count) def climb_tree(self): print("cat climb tree.") cat = Cat("white", 3, 2) cat.miaomiao() cat.catch_mouse() cat.climb() cat.catch_mouse()