poly.md 3.7 KB
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
# Python 类多态

Student 和 Teacher 类都继承 People 类。Student 和 Teacher 类可以动态改变 People 类的 infos 方法,添加子类新增信息。程序入口可以对它们统一处理,打印他们的基本信息

```python
# -*- coding: UTF-8 -*-
class People:
    def __init__(self, role, name, age):
        self.name = name
        self.age = age
        self.role = role

    def infos(self):
        return [
            f"name:{self.name}",
            f"age:{self.age}",
            f"role:{self.role}"
        ]

class Student(People):
    def __init__(self, no, name, age):
        super().__init__("student", name, age)
        self.no = no

    def infos(self):
        # TODO(You): 请在此实现学生类 infos 方法


class Teacher(People):
    def __init__(self, access_key, name, age):
        super().__init__("teacher", name, age)
        self.access_key = access_key

    def infos(self):
        # TODO(You): 请在此实现教师类 infos 方法


if __name__ == '__main__':
    peoples = [
        Teacher("ajladfjkadf", "Linus", 0)
    ]

    for i in range(0, 3):
        s = Student(i, f'somebody_{i}', 20+i)
        peoples.append(s)

    for p in peoples:
        print()
        for info in p.infos():
            print(info)
```

以下对 Student 和 Teacher 的 `infos` 方法实现正确的是? 

## template

```python
class People:
    def __init__(self, role, name, age):
        self.name = name
        self.age = age
        self.role = role

    def infos(self):
        return [
            f"name:{self.name}",
            f"age:{self.age}",
            f"role:{self.role}"
        ]


class Student(People):
    def __init__(self, no, name, age):
        super().__init__("student", name, age)
        self.no = no

    def infos(self):
        info_list = super().infos()
        info_list.append(f'no:{self.no}')
        return info_list


class Teacher(People):
    def __init__(self, access_key, name, age):
        super().__init__("teacher", name, age)
        self.access_key = access_key

    def infos(self):
        info_list = super().infos()
        info_list.append(f'access_key:{self.access_key}')
        return info_list

if __name__ == '__main__':
    peoples = [
        Teacher("ajladfjkadf", "Linus", 0)
    ]

    for i in range(0, 3):
        s = Student(i, f'somebody_{i}', 20+i)
        peoples.append(s)
    for p in peoples:
        print()
        for info in p.infos():
            print(info)
```

## 答案

```python
class Student(People):
    ...
    def infos(self):
        info_list = super().infos()
        info_list.append(f'no:{self.no}')
        return info_list

class Teacher(People):
    ...
    def infos(self):
        info_list = super().infos()
        info_list.append(f'access_key:{self.access_key}')
        return info_list
```

## 选项

### A

```python
class Student(People):
    ...
    def infos(self):
        info_list = super.infos()
        info_list.append(f'no:{self.no}')
        return info_list

class Teacher(People):
    ...
    def infos(self):
        info_list = super.infos()
        info_list.append(f'access_key:{self.access_key}')
        return info_list
```

### B

```python
class Student(People):
    ...
    def infos(self):
        info_list = self.infos()
        info_list.append(f'no:{self.no}')
        return info_list

class Teacher(People):
    ...
    def infos(self):
        info_list = self.infos()
        info_list.append(f'access_key:{self.access_key}')
        return info_list
```

### C

```python
class Student(People):
    ...
    def infos(self):
        return [f'no:{self.no}']

class Teacher(People):
    ...
    def infos(self):
        return [f'access_key:{self.access_key}']
```