1

I'm trying to write a toString() method for a class that has a inner class inside it, and I'm having trouble with that. I was thinking about looping through each array, but I didn't do it correctly. Any suggestions would be great.

public class MarketDataListLevel2 {

    public static class BidAskList {
        protected  MarketData Bid;
        protected MarketData Ask;
        private BidAskList(@JsonProperty("Bid") MarketData Bid, @JsonProperty("Ask") MarketData Ask) {
            this.Bid = Bid;
            this.Ask = Ask;
        }
    }

    protected BidAskList[] BidAskList;
    public MarketDataListLevel2(@JsonProperty("Top10BidAsks") BidAskList[] BidAskList) {
        this.BidAskList = BidAskList;
    }       

    public String toString() {
        // Will make an edit to this later
    }
}

MarketData is essentially an object with 4 fields.

EDIT:

This is the sort of data that I want outputted:

{"Top10BidAsks":[ {"Bid":{"Price":10.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0}, "Ask":{"Price":12.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0}}, {"Bid":{"Price":0.0,"Size":0.0,"ExchangeID":"SMART","timeStamp":0}, "Ask":{"Price":13.0,"Size":12.0,"ExchangeID":"SMART","timeStamp":0}}] }

As you can see a {"Price":10.0,"Size":2.0,"ExchangeID":"SMART","timeStamp":0} is essentially 4 fields constructed from my MarketData class.

The bid and ask is essentially one object (inner class created from being made up of two Marketdata objects). And finally, the entire object is the MarketDataListLevel2 class.

Basically I want my toString method to be able to output data, that involves all 3 of these classes.

4

3 回答 3

2

这个你可能想多了。

根据定义,内部类是您的类单独访问的东西,因此在大多数情况下,即使知道您正在使用内部类,您也不希望任何外部类。因此,正如上面提到的其他人,您不需要您的内部类实现 toString 方法。

但是,如果您真的想传递该信息,则可以将其设置为类似于反向扩展类...您将在内部类中实现 toString,然后在外部类的 toString 中引用该 innerClass.toString()。

于 2013-11-06T19:29:30.830 回答
1

在您的代码中:

public String toString() {
        // Not finished
        return "\nMarketDataDepth:"
        + Arrays.toString(BidAskList.Ask)
        + "\n";
    }

这甚至不应该编译。BidAskList's Ask场是non-static。再次声明:

protected BidAskList[] BidAskList;

混淆声明BidAskList.Ask。编译器将读取BidAskList为数组,因此会给出错误:can not find symbol : symbol variable Ask

于 2013-11-06T19:37:39.377 回答
1

我建议您将内部类 BidAskList 重命名为 BidAsk。它并不是真正的列表,而只是包含 Bid 和 Ask 两个字段。(重命名可能会帮助您解决一些困惑。)

我还建议您将 MarketData 类型更改为双精度(或浮点数,具体取决于您的数据)。

您还应该将您的字段(如 Bid 和 Ask)命名为以小写开头。

下面有两个 toString() 方法。内部类中的一个将 BidAsk 打印为[45.45 x 45.47]. 另一个在外部类中,使用 foreach 循环打印出每个 BidAsk。

public class MarketDataListLevel2 {

    public class BidAsk {
        protected double bid;
        protected double ask;
        private BidAsk(double bid, double ask) {
            this.bid = bid;
            this.ask = ask;
        }
        public String toString() { return "[" + bid + " x " + ask + "]"; }
    }

    protected BidAsk[] bidAskList;
    public MarketDataListLevel2(BidAsk[] bidAskList) {
        this.bidAskList = bidAskList;
    }       

    public String toString() {
        StringBuilder ans = new StringBuilder();
        for (BidAsk bidAsk : bidAskList) {
            ans.append(bidAsk.toString());
        }
        return ans.toString();
    }
}
于 2013-11-06T19:51:35.833 回答