好吧,所以我不知道如何让这个教授班叫这个讲师班

2024-09-29 06:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个代码,我想先解决这个问题

class Lecturer (Person):
    def lecture (self, stuff):
        self.say(stuff + "- you should be taking notes")

然后我试着做一个名为professor的课,在这个讲座上,我做了这样的代码

^{pr2}$

然而,问题来了,我需要把讲义的定义带进来,这样def profess就会调用它,这意味着它在结尾添加了profess的第一部分。在

ex.X声称“凭直觉很明显……无论你想说什么,你都应该记笔记。”

我只是不知道怎么让它叫它,你需要把讲师说(self,stuff)或者诸如此类的东西,这是我自己学习python的一部分,但是如果我继续自己的工作,任何帮助都会很感激的。在


Tags: 代码selfyoudefbeclasspersonnotes
2条回答

那么教授要么“当”讲师,要么“有”讲师。在

class Person(object):
    def say(self, stuff):
        return stuff

class Lecturer(Person):
    def lecture(self, stuff):
        return self.say(str(stuff) + " - you should be taking notes")

class Professor1(Lecturer):  # 'is a' Lecturer
    def profess(self, stuff):
        return self.lecture("Its intuitively obvious that "+str(stuff))

class Professor2(Person):  # 'has a' lecturer
    def __init__(self):
        super(Professor2,self).__init__()
        self.lecturer = Lecturer()

    def profess(self, stuff):
        return self.lecturer.lecture("Its intuitively obvious that "+str(stuff))

p1 = Professor1()
print(p1.profess('Earth is flat'))

p2 = Professor2()
print(p2.profess('Earth is flat'))

结果

^{pr2}$

看起来你想要的是创建一个子类。在

class Lecturer(Person):
    def lecture(self, stuff):
        self.say(stuff + "- you should be taking notes")

class Professor(Lecturer):
    pass

既然这是家庭作业,我就让你来决定用什么来代替pass。在

相关问题 更多 >