继承和类

时间:2015-05-31 22:00:14

标签: python

这是我的班级档案。

@RunWith(MockitoJUnitRunner.class)
public class TestClass {

    @Mock
    private QueueItem item1;

    private List<QueueItem> items;

    @Before
    public void setUp() {
        items = new ArrayList<QueueItem>();
        // Your mocked QueueItem is the first entry in the list
        items.add(item1);
    }

    @Test
    public void simpleTest() {
        invokeSomeMethodWithList(items);
        Mockito.verify(item1).someMethod();
    }
}

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

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")

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

2 个答案:

答案 0 :(得分:1)

你想使用类似的东西:

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,语法略有不同,您还应该从object继承以支持新的样式类:

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()

答案 1 :(得分:1)

你非常接近。这里最重要的是记住在父类上调用show_info,然后在覆盖show_info声明的同时添加您的信息。

我认为你正在使用Python 3,所以下面是Python 3语法。这也适用于Python 2;请记住,除非您导入print_function,否则父类将打印元组而不是字符串,如from __future__ import print_function中所示。

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

    def show_info(self):
        Edifice.show_info(self)
        print('For human habitation: {} bedrooms'.format(self.__bedrooms))