船,麻烦代码

时间:2017-01-25 23:08:39

标签: java oop

我的代码有点麻烦。我想要的是使用if语句来比较船只的桅杆数量。把它放在代码中我想要这个。

if (minimumAantalMasten > aantalMasten ) {
    System.out.println(boot.toString());}
}

但我无法访问此变量。我尝试使用getter和setter,但它不起作用,因为我无法访问这些......

这是我的完整代码:

ArrayList<Boot> boten = new ArrayList<>();

boten.add(new Boot("Berenboot", new Schipper("Kapitein Brom", "GVB III",37501), 2,4));
boten.add(new Boot("Kon-Tiki", null, 1, 6));
boten.add(new Boot("Titanic", new Schipper("Edward John Smith", "GVB III", 13), 423, 1912));
boten.add(new ZeilBoot(3, 297, "Oosterschelde", new Schipper("Roy Heiner", "GVB II", 9617), 9, 24));
boten.add(new ZeilBoot(3, 197, "De Brederode", new Schipper("Maarten Harpertszoon Tromp", null, 2341598), 4,270));
boten.add(new ZeilBoot(1, 27, "Cato", new Schipper("Niels Trommel", "VB II", 158), 2, 4));
boten.add(new ZeilBoot(1, 16, "Golden Star", new Schipper("Marit Bouwmeester", null, 5), 0, 4));
boten.add(new MotorBoot(35, "Strider", new Schipper("Rene de Vogel", "VB II",853), 4, 8));
boten.add(new MotorBoot(21, "Supprise VII", new Schipper("Pete Black", "PNB IV", 13), 4, 8));
boten.add(new MotorBoot(286, "Spirit of Australia", null, 0, 0));

public static void laatBotenMetMinimumAantalMastenZien(ArrayList<Boot> boten, int minimumAantalMasten) {
    System.out.println("\n=== Boten met minimum aantal aan masten ===");
    for (Boot boot : boten) {
        if (minimumAantalMasten > aantalMasten) {
            System.out.println(boot.toString());
        }
    }
}

这是包含我尝试访问的变量的类:

public class ZeilBoot extends Boot {

private int aantalMasten;
private int zeilOppervlakPerMast;

public ZeilBoot(int aantalMasten, int zeilOppervlakPerMast, String naam, Schipper schipper, int aantalHutten, int aantalSlaapplaatsen) {
    super(naam, schipper, aantalHutten, aantalSlaapplaatsen);
    this.aantalMasten = aantalMasten;
    this.zeilOppervlakPerMast = zeilOppervlakPerMast;
}

@Override
public boolean isVaarbewijsNodig() {
    return ((super.isVaarbewijsNodig()) || (zeilOppervlakPerMast > 150));
}

/**
 * @return the aantalMasten
 */
public int getAantalMasten() {
    return aantalMasten;
}

/**
 * @param aantalMasten the aantalMasten to set
 */
public void setAantalMasten(int aantalMasten) {
    this.aantalMasten = aantalMasten;
}

@Override
public String toString() {
    return super.toString() + "Aantal Masten: " + aantalMasten + "\n" + "Zeiloppervlak per mast: "
            + zeilOppervlakPerMast + "\n";
}

}

2 个答案:

答案 0 :(得分:1)

你的方法应该是:

public static void laatBotenMetMinimumAantalMastenZien(ArrayList<Boot> boten, int minimumAantalMasten) {
    System.out.println("\n=== Boten met minimum aantal aan masten ===");
    for (Boot boot : boten) {
        if (minimumAantalMasten > boot.getAantalMasten()) {
            System.out.println(boot.toString());
        }
    }
}

如果aantalMasten只是ZeilBoot中的字段,那么您必须检查实例类型并强制转换:

for (Boot boot : boten) {
    if (boot instanceof ZeilBoot) {
        if (minimumAantalMasten > ((ZeilBoot) boot).getAantalMasten()) {
            System.out.println(boot.toString());
        }
    }
}

答案 1 :(得分:0)

Lisa卓越解决方案的替代品。

Boot课程中,添加此方法。

public int getAantalMasten() {
    return 0;
}

这将在ZeilBoot类中重写。

@Override
public int getAantalMasten() {
    return aantalMasten;
}

然后,您可以依赖多态来确保调用正确的方法版本。

for (Boot boot : boten) {
    if (minimumAantalMasten <= boot.getAantalMasten()) {
        System.out.println(boot);
    }
}

请注意,toString之后无需在括号内明确调用println,因为println方法会为您调用它。我还将您的>更改为<=,因为您在评论中说明您要打印的船只数量大于指定的最小值。