创建一个“Person”基类,具有属性“name, age, home”属性,具有“introduceMyself”方法
时间: 2023-09-27 07:05:35 浏览: 81
java代码-使用java构造Person类。包括姓名(name),性别和年龄(age)的源代码
5星 · 资源好评率100%
好的,这是一个简单的Python代码来创建一个“Person”基类,具有“name, age, home”属性,和“introduceMyself”方法:
```python
class Person:
def __init__(self, name, age, home):
self.name = name
self.age = age
self.home = home
def introduceMyself(self):
print(f"My name is {self.name}, I am {self.age} years old and I live in {self.home}.")
```
你可以通过下面的代码来创建一个“Person”对象并调用“introduceMyself”方法来测试:
```python
person = Person("Tom", 25, "New York")
person.introduceMyself()
```
输出应该是:
```
My name is Tom, I am 25 years old and I live in New York.
```
阅读全文