我可以将此代码改进为更面向 OOP 的范例吗?
Can I improve this code to a more oriented OOP paradigm?
我正在为学生改进这段代码,图片如下:
所以我使用列表压缩重写了它并面向正式 class,所以第二个问题是:我可以进一步改进这段代码吗?
class aluno():
def __init__(self):
# cria uma lista de alunos
self.id = 0
self.alunos = {}
def adiciona_aluno(self, name, info=None):
# cria um novo aluno com o id incremental
self.alunos[self.id] = {
'name':name,
'matricula':self.id,
'info': info if info else "Sem informação"
}
self.id += 1
def view(self,id):
#visualiza um aluno no formato especificado
aluno = self.alunos[id]
print(f'({aluno["matricula"]}) Nome:{aluno["name"]} - Info:{aluno["info"]}')
def view_all(self):
# itera em todos os alunos
[self.view(aluno) for aluno in self.alunos]
def main():
lista_de_alunos = aluno()
lista_de_alunos.adiciona_aluno("Franz")
lista_de_alunos.adiciona_aluno("Pedro", "Aluno exemplar em amatematica")
lista_de_alunos.view_all()
if __name__ == "__main__":
main()
是的,您可以改进代码以使其更加“面向对象”。
Aluno 包含一个列表没有意义,Aluno 应该代表一个学生实例。
下面是一个示例代码。请注意,我的示例代码可以改进,例如,您可以添加一个 Person class 来存储 Student 和 Professor class 的共同属性,例如出生日期、id 等
class Aluno:
_id = 0
def __init__(self, name: str, age: int):
self.name = name
self.age = age
self.id = Aluno._id
Aluno._id += 1
def __cmp__(self, other):
return self.id == other.id
def __str__(self):
return f"- ID: {self.id}; - Name {self.name}; - Age: {self.age};"
class Professor(Aluno):
def __init__(self, name: str, age: int, wage: float):
super().__init__(name=name, age=age)
self.wage = wage
class Turma:
def __init__(self, year: int, letter: str, director: Professor, alunos: list = None):
if alunos is None:
alunos = []
self.year = year
self.letter = letter
self.director = director
self.alunos = alunos
def add_aluno(self, aluno):
# TODO Check if the aluno alerady exists (or work with a set)
self.alunos.append(aluno)
def remove_aluno(self, aluno):
# TODO Check if the aluno can be removed
self.alunos.remove(aluno)
def __str__(self):
_str = f"{self.year}{self.letter}\n" \
f"* Director:\n" \
f"- {self.director}\n" \
f"* Alunos:\n"
for aluno in self.alunos:
_str += str(aluno)+"\n"
return _str
if __name__ == "__main__":
aluno1 = Aluno(name="Foo Bar", age=18)
aluno2 = Aluno(name="Joe", age=16)
prof = Professor(name="Dr. Phill", age=54, wage=1256)
turma = Turma(year=10, letter="C", director=prof, alunos=[aluno1])
turma.add_aluno(aluno2)
print(f"ALUNO 1: {aluno1}")
print(f"ALUNO 2: {aluno2}")
print(f"PROFESSOR: {prof}")
print(f"TURMA: {turma}")
turma.remove_aluno(aluno2)
print(f"TURMA: {turma}")
输出:
ALUNO 1: - ID: 0; - Name Foo Bar; - Age: 18;
ALUNO 2: - ID: 1; - Name Joe; - Age: 16;
PROFESSOR: - ID: 2; - Name Dr. Phill; - Age: 54;
TURMA: 10C
* Director:
- - ID: 2; - Name Dr. Phill; - Age: 54;
* Alunos:
- ID: 0; - Name Foo Bar; - Age: 18;
- ID: 1; - Name Joe; - Age: 16;
TURMA: 10C
* Director:
- - ID: 2; - Name Dr. Phill; - Age: 54;
* Alunos:
- ID: 0; - Name Foo Bar; - Age: 18;
我正在为学生改进这段代码,图片如下:
所以我使用列表压缩重写了它并面向正式 class,所以第二个问题是:我可以进一步改进这段代码吗?
class aluno():
def __init__(self):
# cria uma lista de alunos
self.id = 0
self.alunos = {}
def adiciona_aluno(self, name, info=None):
# cria um novo aluno com o id incremental
self.alunos[self.id] = {
'name':name,
'matricula':self.id,
'info': info if info else "Sem informação"
}
self.id += 1
def view(self,id):
#visualiza um aluno no formato especificado
aluno = self.alunos[id]
print(f'({aluno["matricula"]}) Nome:{aluno["name"]} - Info:{aluno["info"]}')
def view_all(self):
# itera em todos os alunos
[self.view(aluno) for aluno in self.alunos]
def main():
lista_de_alunos = aluno()
lista_de_alunos.adiciona_aluno("Franz")
lista_de_alunos.adiciona_aluno("Pedro", "Aluno exemplar em amatematica")
lista_de_alunos.view_all()
if __name__ == "__main__":
main()
是的,您可以改进代码以使其更加“面向对象”。
Aluno 包含一个列表没有意义,Aluno 应该代表一个学生实例。
下面是一个示例代码。请注意,我的示例代码可以改进,例如,您可以添加一个 Person class 来存储 Student 和 Professor class 的共同属性,例如出生日期、id 等
class Aluno:
_id = 0
def __init__(self, name: str, age: int):
self.name = name
self.age = age
self.id = Aluno._id
Aluno._id += 1
def __cmp__(self, other):
return self.id == other.id
def __str__(self):
return f"- ID: {self.id}; - Name {self.name}; - Age: {self.age};"
class Professor(Aluno):
def __init__(self, name: str, age: int, wage: float):
super().__init__(name=name, age=age)
self.wage = wage
class Turma:
def __init__(self, year: int, letter: str, director: Professor, alunos: list = None):
if alunos is None:
alunos = []
self.year = year
self.letter = letter
self.director = director
self.alunos = alunos
def add_aluno(self, aluno):
# TODO Check if the aluno alerady exists (or work with a set)
self.alunos.append(aluno)
def remove_aluno(self, aluno):
# TODO Check if the aluno can be removed
self.alunos.remove(aluno)
def __str__(self):
_str = f"{self.year}{self.letter}\n" \
f"* Director:\n" \
f"- {self.director}\n" \
f"* Alunos:\n"
for aluno in self.alunos:
_str += str(aluno)+"\n"
return _str
if __name__ == "__main__":
aluno1 = Aluno(name="Foo Bar", age=18)
aluno2 = Aluno(name="Joe", age=16)
prof = Professor(name="Dr. Phill", age=54, wage=1256)
turma = Turma(year=10, letter="C", director=prof, alunos=[aluno1])
turma.add_aluno(aluno2)
print(f"ALUNO 1: {aluno1}")
print(f"ALUNO 2: {aluno2}")
print(f"PROFESSOR: {prof}")
print(f"TURMA: {turma}")
turma.remove_aluno(aluno2)
print(f"TURMA: {turma}")
输出:
ALUNO 1: - ID: 0; - Name Foo Bar; - Age: 18;
ALUNO 2: - ID: 1; - Name Joe; - Age: 16;
PROFESSOR: - ID: 2; - Name Dr. Phill; - Age: 54;
TURMA: 10C
* Director:
- - ID: 2; - Name Dr. Phill; - Age: 54;
* Alunos:
- ID: 0; - Name Foo Bar; - Age: 18;
- ID: 1; - Name Joe; - Age: 16;
TURMA: 10C
* Director:
- - ID: 2; - Name Dr. Phill; - Age: 54;
* Alunos:
- ID: 0; - Name Foo Bar; - Age: 18;