gpt4 book ai didi

python - 继承和类

转载 作者:太空宇宙 更新时间:2023-11-04 03:32:28 25 4
gpt4 key购买 nike

这是在我的类文件中。

class Edifice:
def __init__(self,storeys,area):
self.__storeys = storeys
self.__area = area

def show_info(self):
print('Storeys:',self.__storeys,', floor area:',self.__area)

class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.__bedrooms = bedrooms

def show_info(self):
super(Home, self).show_info()
print("For human habitation: ", self.__bedrooms, "bedrooms")

这是在我的可执行文件中:

from Classes import Edifice, Home

def main():
print("Home")
h = Home(2,3000,3)
h.show_info()

main()

我需要从可执行文件创建另一个具有不同文本输出的 Home show_info() 方法实例。

最佳答案

你想使用类似的东西:

class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.__bedrooms = bedrooms
super(Home, self).show_info()
print("For human habitation: ", bedrooms, "bedrooms")

h = Home(2,3000,3)
Storeys: 2 , floor area: 3000
For human habitation: 3 bedrooms

要真正重写该方法,再次使用 super 非常简单。调用父方法:

class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.__bedrooms = bedrooms

def show_info(self):
super(Home, self).show_info()
print("For human habitation: ", self.__bedrooms, "bedrooms")

h = Home(2,3000,3)
h.show_info()
Storeys: 2 , floor area: 3000
For human habitation: 3 bedrooms

如果您使用的是 python2,语法略有不同,您还应该继承对象以支持新的样式类:

class Edifice(object):
def __init__(self,storeys,area):
self.__storeys = storeys
self.__area = area

def show_info(self):
print('Storeys:{}, floor area: {}'.format(self.__storeys,self.__area))

class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super(Home, self).__init__(storeys, area)
self.__bedrooms = bedrooms

def show_info(self):
super(Home, self).show_info()
print("For human habitation: {} bedrooms.".format(self.__bedrooms)

h = Home(2,3000,3)
h.show_info()

不确定可执行文件是什么意思,但您可以在文件中使用 if __name__==__main__ 并且只有在运行文件时才会执行代码:

if __name__=="__main__":
h = Home(3,3000,2)
h.show_info()

如果您只想导入并运行另一个 .py 文件中的代码:

from whatever import Home
h = Home(3,3000,2)
h.show_info()

无论什么模块都必须位于同一目录或您的 PYTHONPATH 中,在本 question. 中讨论了几种不同的添加到您的路径的方法。

对于您的编辑,我会将消息设为属性并针对第二个实例进行更改:

class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.__bedrooms = bedrooms
self.message = "For human habitation: {} bedrooms"

def show_info(self):
super(Home, self).show_info()
print("{}".format(self.message.format(self.__bedrooms)))

然后:

from module import Home


h = Home(3, 3000, 2)
h.show_info()

h2 = Home(2, 4000, 5)
h2.message = "Second Human habitation: {} bedrooms"

h2.show_info()

输出:

Storeys: 3 , floor area: 3000
For human habitation: 2 bedrooms
Storeys: 2 , floor area: 4000
Second Human habitation: 5 bedrooms

没有修改就简单多了:

class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.bedrooms = bedrooms
self.message = "For human habitation: {} bedrooms".format(self.bedrooms)

def show_info(self):
super(Home, self).show_info()
print(self.message)

我们只是设置来自实例的整个消息:

from module import Home


h = Home(3, 3000, 2)
h.show_info()

h2 = Home(2, 4000, 5)
h2.message = "Second Human habitation: {} bedrooms".format(h2.bedrooms)

h2.show_info()

关于python - 继承和类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30563167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com