我目前正在通过以下方式进行方法调用:
InstrumentsInfo instrumentsInfo = new InstrumentsInfo();
String shortInstruName = "EURUSD"
TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo.getInstrumentID(shortInstruName), instrumentsInfo.getInstrumentTickSize(shortInstruName), instrumentsInfo.getInstrumentName(shortInstruName));
在 VBA 中我会做这样的事情
With instrumentsInfo
TrackInstruments(.getInstrumentID(shortInstruName), .getInstrumentTickSize(shortInstruName), .getInstrumentName(shortInstruName));
所以我的问题是,有没有办法避免在Java中的方法调用中重复“instrumentsInfo”?
最佳答案
总而言之,不,尽管您可能想考虑改变
TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo.getInstrumentID(shortInstruName), instrumentsInfo.getInstrumentTickSize(shortInstruName), instrumentsInfo.getInstrumentName(shortInstruName));
至
TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);
然后让构造函数获取它需要的参数。
或者也许使用 builder如果您需要很多参数,请使用模式。
或者确实问问自己,为什么要在 TrackInstruments
之外构建 InstrumentsInfo
,而后者似乎如此严重依赖它。 (没有完全理解你的对象)
关于java - 相当于 Java 中的 VBA With 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753711/