Student 类继承Person 父类 Person: name, age name 公有属性 age 私有属性 get_age方法 : 输出实例的age Student: Person + 属性add add属性 get_age方法:输出”隐藏” set_add方法(address):修改add属性为address
时间: 2024-03-12 14:47:44 浏览: 39
好的,你的问题是关于一个类的继承和属性的实现。你可以定义一个Person类,包含两个属性,一个是公有属性name,一个是私有属性age。然后定义一个get_age方法,用于输出实例的age属性。接着你可以定义一个Student类,继承Person类,并新增一个公有属性add和一个set_add方法,用于修改add属性为address。
下面是Python代码示例:
```python
class Person:
def __init__(self, name, age):
self.name = name
self.__age = age
def get_age(self):
print("Person age is", self.__age)
class Student(Person):
def __init__(self, name, age, add):
super().__init__(name, age)
self.add = add
def set_add(self, address):
self.add = address
def get_age(self):
print("隐藏")
```
在这个示例中,Person类包含了name和私有属性age,以及一个get_age方法,用于输出实例的age属性。Student类继承自Person类,并新增了一个公有属性add和一个set_add方法,用于修改add属性的值。此外,Student类也重写了父类的get_age方法,实现了不同的输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)