【问题标题】:Inheritance answer overlap继承答案重叠
【发布时间】:2017-03-06 20:25:45
【问题描述】:

我必须制作一个将字符串转换为拉丁语的程序,并包含一个构造函数+该程序的示例。然后,我必须使用覆盖的方法基于它创建一个子类,以添加一些新规则。

在原始类文件中我有构造函数:

pig = pigLatin_Class("First test")
print(pig)
pig.sentence = "Changing it up"
print(pig)

这可以正常工作,但是在我的新子类中:

gib = gibberish_class("test")
print(gib)

这也可以正常工作,但也会打印出超类的答案,即使它们不在类定义之外。

我正在使用:

import assignment_a3_1 as a3_1

class gibberish_class(a3_1.pigLatin_Class):

作为我的遗产。

有什么解决办法吗?

编辑:

关于命名约定,我只是使用我们被告知使用的东西,而且我最多只编码一两个月,上了一门大学课程,要温柔! :D

超级:

class pigLatin_Class(object):

    vowels = ["a","e","i","o","u","A","E","I","O","U"]

    def __init__(self, initialSentence):

        self.__sentence = initialSentence
        self.sentencePig = []
        self.pLatin_converter(self.__sentence)

    def pLatin_converter(self, sentence):

        wordList = sentence.split(" ")

        for word in wordList:

            isVowel = False
            wList = list(word)

            for character in word: #vowel checks
                if(character in pigLatin_Class.vowels):
                    isVowel = True
                    break

            if(isVowel == False): #finishing if no vowels
                wList.append(wList.pop(0))
                word = ''.join(wList) + "way "
                self.sentencePig.append(word)

            if(''.join(wList) == word): #Stops it from iterating through again if it contains no vowels

                if(wList[0] in pigLatin_Class.vowels):
                    word = ''.join(wList) + "hay "
                    self.sentencePig.append(word)

                elif(wList[0] not in pigLatin_Class.vowels): #contains vowels but not first

                    for i in range(len(wList)):
                        if(wList[i] in pigLatin_Class.vowels):

                            wList = wList[i:] + wList[:i]
                            word = ''.join(wList) + "ay "
                            self.sentencePig.append(word)
                            break


    def __str__(self):

        string = ("\n The translation of '" + self.__sentence + "' is: " + ''.join(self.sentencePig))
        return string

    @property
    def sentence(self):
        return self.__sentence

    @sentence.setter
    def sentence(self, value):
        self.__sentence = value
        self.sentencePig = []
        self.pLatin_converter(self.__sentence)


pig = pigLatin_Class("First test")
print(pig)
pig.sentence = "Changing it up"
print(pig)

子类:

import assignment_a3_1 as a3_1

class gibberish_class(a3_1.pigLatin_Class):


    symb = ["0","1","2","3","4","5","6","7","8","9",",",".","/",";","#","[","]","=","-","(",")","*","&","^","%","$","£","_","+","{","}","~","<","@",":","?",">","<","|"]
    vowels = a3_1.pigLatin_Class.vowels

    def pLatin_converter(self, sentence):

        wordList = sentence.split(" ")

        for word in wordList:

            suffix = 0
            isVowel = False
            wList = list(word)

            for character in word: #vowel checks
                if(character in gibberish_class.vowels):
                    isVowel = True
                    break

            if(isVowel == False): #finishing if no vowels
                wList.append(wList.pop(0))
                suffix = 1

            if(''.join(wList) == word): #Stops it from iterating through again if it contains no vowels

                if(wList[0] in gibberish_class.vowels):
                    suffix = 2

                elif(wList[0] not in gibberish_class.vowels): #contains vowels but not first

                    for i in range(len(wList)):
                        if(wList[i] in gibberish_class.vowels):

                            wList = wList[i:] + wList[:i]
                            suffix = 3
                            break


            for character in wList:
                #print(character)
                if(character in gibberish_class.symb):

                    wList.append(character)

            if(suffix == 1):
                word = ''.join(wList) + "way "
            elif(suffix == 2):
                word = ''.join(wList) + "hay "
            elif(suffix == 3):
                word = ''.join(wList) + "ay "

            self.sentencePig.append(word)





gib = gibberish_class("test")
gib.pLatin_converter
print(gib)

编辑二:

另一个问题对停止main方法很有帮助,但是在第一个问题中仍然调用了str方法,只是打印了一个空字符串,说if语句只会导致程序抛出错误如果在 str 方法中使用。我该如何解决?

【问题讨论】:

标签: python inheritance


【解决方案1】:

这是一个像您这样的典型程序的外观框架。

class PigLatin(object):

  def __init__(self, text):
    ...

  # Add helper methods to taste.

  def splitIntoWords(self):
    return ...  # re.split would help here.

  def mangle(self):
    # Do your conversion, put it into result
    ...
    return result


class Gibberish(PigLatin):
  # No __init__ since you inherit it.

  def mangle(self):
    # This method replaces, aka overrides, mangle() of PigLatin.
    # You can use other methods, e.g. self.splitIntoWords().
    ...
    return result


if __name__ == '__main__':
   text = 'Hello world!'
   print 'Do you know what "%s" would be in Pig Latin?' % text
   print 'It is %s' % PigLatin(text).mangle()
   print 'And now some Gibberish: %s' Gibberish(text).mangle

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多