java - 相当于 Java 中的 VBA With 语句

标签 java vba with-statement

我目前正在通过以下方式进行方法调用:

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/

相关文章:

java - 如何在不进行多个开关和 if 的情况下创建基于字符串的类的实例?

VBA从文档中的数据创建另存为文件名

sql - 在 postgresql 9.5 中是否可以从 WITH 查询中插入冲突更新?

mysql - 显示给定用户的交易余额

java - Selenium Webdriver - 元素不可见

java - 是否可以在 Android View 上查询正在运行/待处理的动画?

java - EAGAIN EWOULDBLOCK 安卓

vba - 使用 vba 将多列复制并粘贴到另一个工作表中

vba - 从 Excel-VBA 脚本调用 .reg 文件

python - 带有语句解释的好 Python