python创建person类用printinfo方法_Python类(继承)使用Person、Student、Professor类

我很难创建类继承的python代码。父类为“人类”,子类为“学生类”和“教授类”。在

我花了很多时间,但这太难理解了。我做了1个父类,2个子类和方法。

我认为我的很多代码需要修改,我不知道如何修复它。在class Person:

def __init__(self,name,age,department):

self.name = name

self.age = age

self.department = department

def get_name():

return self.name

class Student(Person):

def __init__(Person,i_d,GPA,advisor):

self.Person = Person

self.i_d = i_d

self.GPA = GPA

self.advisor = advisor

def print_info():

print("Name: %s, Age: %d, Department: %s, Advisor: %s " %(Person.name,Person.age,Person.department,Student.advisor))

def reg_advisor():

return Professor.name

class Professor(Person):

def __init__(self,position,laboratory,student):

self.position = position

self.laboratory = laboratory

self.student = student

def print_info():

print("Name: %s, Age: %d, Department: %s, Student: %s and %s " %(Person.name,Person.age,Person.department,Professor.student))

def reg_student():

return Student.name

Stu1 = Student("David", 30, "Computer", 20001234, 4.5)

Stu2 = Student("Tom", 30, "Computer", 20001234, 4.5)

prof1 = Professor("Andrew", 55, "Computer", "Full", "DT") # The last element is the list of student.

stu1.reg_advisor(prof1)

stu2.reg_advisor(prof1)

prof1.reg_student(stu1)

prof1.reg_student(stu2)

stu1.print_info()

stu2.print_info()

prof1.print_info()

结果应该是:

^{pr2}$

我更希望不添加或删除方法。在

任何帮助都将不胜感激。在

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值