用python随机定义一个girlfriend类
时间: 2023-06-12 12:07:44 浏览: 164
以下是一个随机定义的Girlfriend类的代码示例:
```python
import random
class Girlfriend:
def __init__(self):
self.name = self.generate_name()
self.age = self.generate_age()
self.hobby = self.generate_hobby()
def generate_name(self):
names = ["Alice", "Bella", "Cindy", "Daisy", "Emily", "Fiona"]
return random.choice(names)
def generate_age(self):
return random.randint(18, 30)
def generate_hobby(self):
hobbies = ["reading", "traveling", "cooking", "dancing", "painting"]
return random.choice(hobbies)
def introduce(self):
print(f"Hi, my name is {self.name}. I'm {self.age} years old, and I love {self.hobby}.")
# 测试
girlfriend = Girlfriend()
girlfriend.introduce()
```
输出结果可能为:
```
Hi, my name is Cindy. I'm 21 years old, and I love cooking.
```
阅读全文