-
定义一个狗类和一个人类:
狗拥有属性:姓名、性别和品种 拥有方法:叫唤
class Dog(): def __init__(self,name,gender,breed): self.name = name self.gender = gender self.breed = breed def call(self): print("汪汪汪") d1 = Dog('旺财','雄','土狗') print(d1.name) d1.call()
人类拥有属性:姓名、年龄、狗 拥有方法:遛狗
class Person(): def __init__(self,name,age,dog): self.name = name self.age = age self.dog = dog def walk_the_dog(self): print(self.dog+"摇尾巴") p1 = Person('小明',18,'旺财') p1.walk_the_dog()
-
定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Rectangular: def __init__(self,length,width): self.length = length self.width = width def get_area(self): return self.length * self.width def get_perimeter(self):
Python面向对象作业
于 2022-03-08 19:20:31 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)