开发者社区> 问答> 正文

将抽象对象转换为子类对象

我正在尝试编译:

Property flat = new Flat(1, "Street", "City", "BDK 8DN", 2, 1);

属性是一个抽象类,而Flat是一个子类。

public class Flat extends Property

Flat包含Property中的所有抽象方法。

我在Property上有警告,在新Flat()上有错误

无法从公寓转换为物业

对泛型类型Property的引用应参数化

编辑

额外的代码:

属性

public abstract class Property {
    private int number;
    private String street;
    private String city;
    private String postCode;
    private int numberOfRooms;
    protected Map<Room, ITenant> rooms = new HashMap<Room, ITenant>();

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

    public abstract String displayOccupiedProperty();

    public abstract boolean isAvailable();

    public abstract void occupyRoom(Room room, ITenant tenant);
}

平面

public class Flat extends Property {

    private int floor;

    public Flat(int number, String street, String city, String postCode, int numberOfRooms, int floor) {
        super(number, street, city, postCode, numberOfRooms);
        this.floor = floor;
    }

  • Property中的四种覆盖方法

public class House extends Property {

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

  • Property中的四种覆盖方法

测试文件

public class PropertyTest {

    @Test
    public void testFlat() {
        Property flat = new Flat(1, "Street", "City", "BDK 8DN", 2, 1);
    }

展开
收起
垚tutu 2019-12-12 09:44:43 492 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
继承与功能组合 立即下载
建立联系方法之一 立即下载
低代码开发师(初级)实战教程 立即下载