2. 需求:首先定义一个类Animal,类中定义一个方法play()方法, 创建一个猫Cat类,继承Animal并重写play方法输出“猫,玩老鼠”, 创建一个狗类, 继承Animal并重写play方法输出“狗,玩球”。 最后编写测试类TestDemo,通过多态(向上造型)创建猫,通过多态(向上造型)创建狗, 并调用猫对象的play方,狗对象的play方法
时间: 2024-03-14 09:43:13 浏览: 23
好的,以下是代码实现:
```python
# 定义 Animal 类
class Animal:
def play(self):
pass
# 定义 Cat 类,继承自 Animal 类
class Cat(Animal):
def play(self):
print("猫,玩老鼠")
# 定义 Dog 类,继承自 Animal 类
class Dog(Animal):
def play(self):
print("狗,玩球")
# 测试类
class TestDemo:
def test(self):
# 创建猫对象
cat = Cat()
# 创建狗对象
dog = Dog()
# 调用猫对象的 play 方法
cat.play()
# 调用狗对象的 play 方法
dog.play()
# 创建 TestDemo 对象并调用 test 方法
test_demo = TestDemo()
test_demo.test()
```
输出结果为:
```
猫,玩老鼠
狗,玩球
```
相关问题
设计一个程序实现类、对象、继承、抽象类及接口应用
好的,我来设计一个简单的程序来实现这些概念。
首先,我们定义一个类 Animal,表示动物,其中包含属性 name 和 age,以及方法 eat() 和 sleep()。
```python
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
pass
def sleep(self):
pass
```
接下来,我们定义一个类 Cat,继承自 Animal,表示猫。它重写了 eat() 和 sleep() 方法。
```python
class Cat(Animal):
def eat(self):
print(f"{self.name} is eating fish.")
def sleep(self):
print(f"{self.name} is sleeping on the sofa.")
```
然后,我们定义一个抽象类 Pet,它继承自 Animal,表示宠物。它有一个抽象方法 play(),表示玩耍。
```python
from abc import ABC, abstractmethod
class Pet(Animal, ABC):
def __init__(self, name, age):
super().__init__(name, age)
@abstractmethod
def play(self):
pass
```
最后,我们定义一个接口 Groomable,表示可梳理的。它有一个方法 groom(),表示梳理。
```python
class Groomable(ABC):
@abstractmethod
def groom(self):
pass
```
现在,我们可以定义一个类 Dog,它既是宠物,又是可梳理的。
```python
class Dog(Pet, Groomable):
def __init__(self, name, age):
super().__init__(name, age)
def play(self):
print(f"{self.name} is fetching the ball.")
def groom(self):
print(f"{self.name} is being brushed.")
```
现在,我们可以创建一个猫和一只狗,调用它们的方法。
```python
cat = Cat("Kitty", 3)
dog = Dog("Buddy", 5)
cat.eat() # Kitty is eating fish.
dog.eat() # Buddy is eating.
cat.sleep() # Kitty is sleeping on the sofa.
dog.sleep() # Buddy is sleeping.
cat.play() # TypeError: Can't instantiate abstract class Cat with abstract methods play
dog.play() # Buddy is fetching the ball.
cat.groom() # AttributeError: 'Cat' object has no attribute 'groom'
dog.groom() # Buddy is being brushed.
```
我们可以看到,猫虽然继承了 Pet 类,但它没有实现 play() 方法,所以不能被实例化。而狗实现了 Pet 和 Groomable 接口的方法,所以可以被实例化,并且可以被玩耍和梳理。
首先定义一个类Animal,类中定义一个属性color,一个方法play(),创建 一个猫Cat类,继承Animal并重写play方法输出“我是猫,我玩老鼠”,创建一个狗类, 继承Animal并重写play方法输出“我是狗,我玩球”。最后编写测试类TestDemo,通 过多态创建猫,通过多态创建狗,并调用猫对象的play方法,狗对象的play方法。
好的,以下是代码实现:
```python
# 定义 Animal 类
class Animal:
def __init__(self, color):
self.color = color
def play(self):
pass
# 定义 Cat 类,继承 Animal
class Cat(Animal):
def play(self):
print("我是猫,我玩老鼠")
# 定义 Dog 类,继承 Animal
class Dog(Animal):
def play(self):
print("我是狗,我玩球")
# 定义测试类 TestDemo
class TestDemo:
def play(self, animal):
animal.play()
# 创建猫对象,通过多态调用 Cat 的 play 方法
cat = Cat("white")
test = TestDemo()
test.play(cat)
# 创建狗对象,通过多态调用 Dog 的 play 方法
dog = Dog("black")
test.play(dog)
```
输出结果为:
```
我是猫,我玩老鼠
我是狗,我玩球
```
希望能够帮到你!
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)