我有两个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()的方法,然后运行类child(human)的方法。 我想要的是运行子类文件并只运行这些方法。 这是可能的,大部分时间我会这样做吗?
感谢您的帮助
答案 0 :(得分:0)
是的,这是可能的,这是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__'
是顶级代码执行的范围的名称。从标准输入,脚本或交互式提示中读取时,模块的__name__
设置为'__main__'
。
if __name__ == "__main__":
# execute only if run as a script