【问题标题】:Issue overriding tostring问题覆盖 tostring
【发布时间】:2013-02-19 20:31:14
【问题描述】:

我想在CargoShip 类的toString 方法中覆盖Ship 类的toString 方法,这样控制台就不会打印船的建造年份。我试过这样做,但它仍然打印年份。我不确定我是否对覆盖进行了错误的编码,或者问题是否与在 ShipDemo 类中调用该方法的方式有关。

船级:

public class Ship {
    public String shipName;
    public String yearBuilt;

    public Ship() {
    }

    public Ship(String name, String year) {
        shipName = name;
        yearBuilt = year;
    }

    public void setShipName(String name) {
        shipName = name;
    }

    public void setYearBuilt(String year) {
        yearBuilt = year;
    }

    public String getShipName() {
        return shipName;
    }

    public String getYearBuilt() {
        return yearBuilt;
    }

    public String toString() {
        //return toString() + " Name: " + shipName
        //+ "\n Year Built: " + yearBuilt;
        String str;
        str = " Name: " + shipName + "\n Year Built: " + yearBuilt;

        return str;
    }
}

CargoShip 类:

public class CargoShip extends Ship {
    public int capacity;

    public CargoShip() {
    }

    public CargoShip(int maxCap, String name, String year) {
        super(name, year);
        capacity = maxCap;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int cap) {
        cap = capacity;
    }

    public String toString() {
        return super.toString() + " Name: " + getShipName()
                + " Tonnage Capacity: " + getCapacity();
    }
}

ShipDemo 类:

public class ShipDemo {
    public static void main(String[] args) {
        // Array Reference
        Ship[] shiptest = new Ship[3];

        // Elements in array set to ship type
        shiptest[0] = new Ship();
        shiptest[1] = new CargoShip();
        shiptest[2] = new CruiseShip();

        // Ship 1
        shiptest[0].setShipName("Manitou ");
        shiptest[0].setYearBuilt("1936 ");

        // Ship 2 ; Cargoship
        shiptest[1] = new CargoShip(13632, "SS Edmund Fitzgerald", "1958");

        // Ship 3 ; Cruiseship
        shiptest[2] = new CruiseShip(2620, "RMS Queen Mary 2", "2004");

        // loop to print out all ship info
        for (int i = 0; i < shiptest.length; i++) {
            // Output
            System.out.println("Ship " + i + " " + shiptest[i]);
        }
    }
}

【问题讨论】:

  • 但是,您在 CargoShip 实现中包含了 Ships 版本的 toString 的主体。它的工作方式与您编写的完全一样。
  • 怎么样?不包括年份。
  • 因为你调用了super.toString(),它将调用CargoShip超类上的toString方法,恰好是...Ship

标签: java overriding tostring


【解决方案1】:

CargoShip 你有以下内容:

public String toString()
{       
    return super.toString() + " Name: " + getShipName() + " Tonnage Capacity: "      + 
    getCapacity();    
}

通过调用super.toString(),您实际上是在调用父类toString() 方法,其中包括年度打印。您应该删除该方法调用并将返回的字符串更改为仅包含您要显示的信息。

覆盖父方法意味着提供具有相同名称、参数列表、返回类型和可见性的方法,但实现可能不同(方法体)。您无需调用 super 即可将其视为覆盖。

您可能希望在CargoShip 中有这样的内容:

public String toString()
{       
    return " Name: " + getShipName() + " Tonnage Capacity: " + getCapacity();    
}

【讨论】:

  • 我的指示说我需要一个 toString 方法来覆盖基类中的 toString 方法
  • 您已经通过在扩展ShipCargoShip 中定义自己的toString() 方法来做到这一点。您无需调用父级来覆盖它。只需使用具有相同方法定义的方法即可覆盖。
  • 如果我只是在没有 super 的情况下从 CargoShip 调用 toString,那么我应该得到一个运行错误。
  • 如果我在没有 super 的情况下运行它,我会收到此错误“线程“主”java.lang.StackOverflowError 中的异常”
  • 那是因为你还在递归调用toString(),导致死循环,导致java内存不足。完全取出该方法调用。看看我编辑的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 2018-07-27
相关资源
最近更新 更多