首页 > 解决方案 > 测试继承的构造函数时出现类型不匹配错误

问题描述

我正在使用日食。当我尝试使用 Junit 测试一个类时,在尝试创建对象时出现错误。

    public abstract class Property {

    private int number = 0;
    private String street = null;
    private String city = null;
    private String postCode = null;
    private int numberOfRooms = 0;

    public Property(int number, String street, String city, String postCode, int numberOfRooms) {...}
    ...
}

    public class House extends Property {

    public House(int number, String street, String city, String postCode, int numberOfRooms) {
        super(number, street, city, postCode, numberOfRooms);
    }
   ...
}

这是junit测试文件:

public class PropertyTest {

@Test
    public void testHouse() {
        Property house = new House(1, "Percy Road", "London", "WC2N 5DU", 2);
        assertEquals("1 Percy Road, London WC2N 5DU (2 bedroom house)", house.toString());
    }
}

我在 PropertyTest: Type mismatch: cannot convert from House to Property中得到这个错误。据我所知,这段代码应该可以工作

标签: javaoopabstract-classsubclass

解决方案


推荐阅读