pythonoop子类先打印父类方法,然后打印自己的方法

2024-05-13 18:04:36 发布

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

我有两个python文件,第一个是父类。我用子类创建了一个新文件。当我运行子类文件时,父类文件中的方法首先运行子类

父类如下所示

class human():
def __init__ (self, gender="", age=0, height=0, howHigh=""):
    #setting attributes
    self.gender = ""
    self.age = 0
    self.height = 0
    self.howHigh = ""


def setHeight(self):
    self.height = int(input("What is your height in cm? "))

def setGender(self):
    self.gender = input("What is your gender? ")

def setAge(self):
    self.age = int(input("What is your age? "))

def changeHeight(self):
    if self.height < 80:
        self.howHigh = "Small"
        print("Your hieght is small!")

    elif self.height >= 80 and self.height < 180:
        self.howHigh = "Medium"
        print("Your hieght is medium!")

    elif self.height >= 180:
        self.howHigh = "Large"
        print("Your hieght is tall")

human1 = human()
human1.setHeight()
human1.setGender()
human1.setAge()
print("human gender is ", human1.gender)
print("Human age is", human1.age)
print("Human height is", human1.height)
human1.changeHeight()
print(human1.howHigh)
human1 = human()
human1.setHeight()
human1.setGender()
human1.setAge()
print("human gender is ", human1.gender)
print("Human age is", human1.age)
print("Human height is", human1.height)
human1.changeHeight()
print(human1.howHigh)

子类文件如下所示

from human_class import *
class child(human):
def __init__(self):
    super().__init__()

def setHeight(self):
    self.height = int(input("What is your height in cm? "))


def changeHeight(self):
    if self.height < 30:
        self.howHigh = "Small"
        print("Your hieght is small for a child!")

    elif self.height >= 30 and self.height < 120:
        self.howHigh = "Medium"
        print("Your hieght is medium for a child!")

    elif self.height >= 120:
        self.howHigh = "Large"
        print("Your hieght is tall for a child!")


child1 = child()
child1.setHeight()
child1.changeHeight()
print(child1.howHigh)

下面是代码,当我运行父类文件时,会运行human runs。 当我运行第二个文件时,先运行子类,然后运行类human()的方法,然后运行类child(human)的方法。 我想要的是运行子类文件并只运行那些方法。 有没有可能,我大多数时候都是这样做的

谢谢你的帮助


Tags: 文件selfchildageyourisdefgender
1条回答
网友
1楼 · 发布于 2024-05-13 18:04:36

是的,这是可能的,这就是inheritance in Python所做的。按照@deceze的建议,更新如下文件:

human_class.py

class human():
    def __init__ (self, gender="", age=0, height=0, howHigh=""):
        #setting attributes
        self.gender = ""
        self.age = 0
        self.height = 0
        self.howHigh = ""


    def setHeight(self):
        self.height = int(input("What is your height in cm? "))

    def setGender(self):
        self.gender = input("What is your gender? ")

    def setAge(self):
        self.age = int(input("What is your age? "))

    def changeHeight(self):
        if self.height < 80:
            self.howHigh = "Small"
            print("Your hieght is small!")

        elif self.height >= 80 and self.height < 180:
            self.howHigh = "Medium"
            print("Your hieght is medium!")

        elif self.height >= 180:
            self.howHigh = "Large"
            print("Your hieght is tall")

if __name__ == '__main__':
    human1 = human()
    human1.setHeight()
    human1.setGender()
    human1.setAge()
    print("human gender is ", human1.gender)
    print("Human age is", human1.age)
    print("Human height is", human1.height)
    human1.changeHeight()
    print(human1.howHigh)
    human1 = human()
    human1.setHeight()
    human1.setGender()
    human1.setAge()
    print("human gender is ", human1.gender)
    print("Human age is", human1.age)
    print("Human height is", human1.height)
    human1.changeHeight()
    print(human1.howHigh)

chlid_class.py

from human_class import *
class child(human):
    def __init__(self):
        super().__init__()

    def setHeight(self):
        self.height = int(input("What is your height in cm? "))


    def changeHeight(self):
        if self.height < 30:
            self.howHigh = "Small"
            print("Your hieght is small for a child!")

        elif self.height >= 30 and self.height < 120:
            self.howHigh = "Medium"
            print("Your hieght is medium for a child!")

        elif self.height >= 120:
            self.howHigh = "Large"
            print("Your hieght is tall for a child!")


child1 = child()
child1.setHeight()
child1.changeHeight()
print(child1.howHigh)

什么是if __name__ == '__main__':做的?official documentation

'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

if __name__ == "__main__":
    # execute only if run as a script

相关问题 更多 >