自定义类:自定义类型无法转换为其他自定义类型

时间:2017-10-28 04:02:35

标签: java abstract-class subclass

我被赋予了一个赋值,它有两个给定的类,一个抽象父类Lot.java和一个测试类TestLots.java。我不应该编辑其中任何一个。赋值是创建Lot的两个子类,以便TestLots中的错误不再是错误。

该程序的目的是按顺序显示批次的名称和区域:

    Lot ID L3 has area: 13500.0 
    Lot ID L2 has area: 27000.0 
    Lot ID L1 has area: 35000.0 
    Lot ID L4 has area: 70000.0

但是我得到错误: 不兼容的类型:LotType1无法转换为Lot,和 LotType2无法转换为Lot。我怀疑问题出在我的子类中,以及它应该覆盖或引用父类的方式。

这是TestLots,我收到错误:

    public class TestLots {

        public static void main(String args[]){  
            // an array of lots -- some of type1, some of type2
            Lot[] lots = {new LotType1("L1",350, 200), // error here
                new LotType2("L2",100,270),
                new LotType1("L3",100, 270),
                new LotType2("L4",350,200)
            };

            // sort the lots of mixed types by area (note, you'll have to implement
            // Comparable interface correctly in LotType1 and LotType2 for this to work:
            java.util.Arrays.sort(lots);

            // print out sorted results
            for (Lot lot: lots) {
                System.out.print(lot + " "); 
                System.out.println();
            }
        }
    }

这是Lot,父类

public abstract class Lot {

    public abstract double calculateArea();
    public abstract String getID();

    @Override
    public String toString() {
        return "Lot ID "+ getID() +" has area: "+ calculateArea();
    }
}

子类几乎完全相同:

public class LotType1 extends Lot implements Comparable<LotType1>{
    String name;
    int height;
    int width;
    double area;

    public LotType1(String name, int height, int width) {
        this.name = name;
        this.height = height;
        this.width = width;
    }

    public String getID() {
        return name;
    }

    public double calculateArea() {
        return area = ((width * height)/2);
    }

    @Override
    public int compareTo(LotType1 lot) {
        area = ((width * height)/2);
        if(area==lot.area)
        {
            return 0;
        }
        else if(area>lot.area)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

编辑以添加LotType2:

public class LotType2 extends Lot implements Comparable<LotType2>{
    String name;
    int height;
    int width;
    double area;

    public LotType2(String name, int height, int width) {
        this.name = name;
        this.height = height;
        this.width = width;
    }

    public String getID() {
        return name;
    }

    public double calculateArea() {
        return area = (width * height);
    }

    @Override
    public int compareTo(LotType2 lot) {
        area = (width * height);
        if(area==lot.area)
        {
            return 0;
        }
        else if(area>lot.area)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

对不起,这篇文章太长了。我决定包含所有相关文件以防万一。

2 个答案:

答案 0 :(得分:1)

问题是您无法在同一个集合中对具有不同类似实现的对象进行排序。更改子类以实现Lot的Comparable:

    public class LotType1 extends Lot implements Comparable<Lot> {

并在compareTo方法中使用calculateArea():

    @Override
    public int compareTo(Lot lot) {
        if (calculateArea() == lot.calculateArea()) {
            return 0;
        } else if (calculateArea() > lot.calculateArea()) {
            return 1;
        } else {
            return -1;
        }
    }

答案 1 :(得分:0)

您的完整版代码如下所示。

它会给你预期的结果。

public class TestLots {

public static void main(String args[]) {
    // an array of lots -- some of type1, some of type2
    Lot[] lots = {new LotType1("L1", 350, 200), // error here
            new LotType2("L2", 100, 270),
            new LotType1("L3", 100, 270),
            new LotType2("L4", 350, 200)
    };

    // sort the lots of mixed types by area (note, you'll have to implement
    // Comparable interface correctly in LotType1 and LotType2 for this to work:
    java.util.Arrays.sort(lots);

    // print out sorted results
    for (Lot lot : lots) {
        System.out.print(lot + " ");
        System.out.println();
    }
}
}

abstract class Lot {

public abstract double calculateArea();

public abstract String getID();

@Override
public String toString() {
    return "Lot ID " + getID() + " has area: " + calculateArea();
}
}


class LotType1 extends Lot implements Comparable<Lot> {
String name;
int height;
int width;
double area;

public LotType1(String name, int height, int width) {
    this.name = name;
    this.height = height;
    this.width = width;
}

public String getID() {
    return name;
}

public double calculateArea() {
    return area = ((width * height) / 2);
}

@Override
public int compareTo(Lot lot) {
    if (calculateArea() == lot.calculateArea()) {
        return 0;
    } else if (calculateArea() > lot.calculateArea()) {
        return 1;
    } else {
        return -1;
    }
}
}

class LotType2 extends Lot implements Comparable<Lot> {
String name;
int height;
int width;
double area;

public LotType2(String name, int height, int width) {
    this.name = name;
    this.height = height;
    this.width = width;
}

public String getID() {
    return name;
}

public double calculateArea() {
    return area = ((width * height) / 2);
}

@Override
public int compareTo(Lot lot) {
    if (calculateArea() == lot.calculateArea()) {
        return 0;
    } else if (calculateArea() > lot.calculateArea()) {
        return 1;
    } else {
        return -1;
    }
}
}