Java中VBA与语句的等价性

Java中VBA与语句的等价性,java,vba,with-statement,Java,Vba,With Statement,我目前正在以以下方式进行方法调用: InstrumentsInfo instrumentsInfo = new InstrumentsInfo(); String shortInstruName = "EURUSD" TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo.getInstrumentID(shortInstruName), instrumentsInfo.getInstrumentTick

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

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”

不,Java中没有语法为的
。但是,为了避免重复“instrumentsInfo”,您可以创建一个采用以下类型的构造函数:

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);
但是,这种设计使
TrackInstruments
了解
仪器信息,它不会促进对象之间的松散耦合,因此您可以使用:

Integer instrumentID = instrumentsInfo.getInstrumentID(shortInstruName);
Integer instrumentTickSize = instrumentsInfo.getInstrumentTickSize(shortInstruName);
String instrumentName = instrumentsInfo.getInstrumentName(shortInstruName);

TrackInstruments trackInstruments = new TrackInstruments(instrumentID, instrumentTickSize, instrumentName);

不,Java中没有语法为
。但是,为了避免重复“instrumentsInfo”,您可以创建一个采用以下类型的构造函数:

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);
但是,这种设计使
TrackInstruments
了解
仪器信息,它不会促进对象之间的松散耦合,因此您可以使用:

Integer instrumentID = instrumentsInfo.getInstrumentID(shortInstruName);
Integer instrumentTickSize = instrumentsInfo.getInstrumentTickSize(shortInstruName);
String instrumentName = instrumentsInfo.getInstrumentName(shortInstruName);

TrackInstruments trackInstruments = new TrackInstruments(instrumentID, instrumentTickSize, instrumentName);

一个单词“否”,尽管你可能想考虑改变< /P>
TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo.getInstrumentID(shortInstruName), instrumentsInfo.getInstrumentTickSize(shortInstruName), instrumentsInfo.getInstrumentName(shortInstruName));

然后让构造函数获取它需要的参数

如果需要很多参数,也可以使用该模式


或者问问自己,为什么要在
轨迹仪器
之外构建
仪器信息
,而后者似乎如此依赖它。(虽然没有完全理解你的对象)

一个单词“否”,尽管你可能想考虑改变

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo.getInstrumentID(shortInstruName), instrumentsInfo.getInstrumentTickSize(shortInstruName), instrumentsInfo.getInstrumentName(shortInstruName));

然后让构造函数获取它需要的参数

如果需要很多参数,也可以使用该模式


或者问问自己,为什么要在
轨迹仪器
之外构建
仪器信息
,而后者似乎如此依赖它。(在不完全理解对象的情况下)是的,您可以在TrackInstruments中创建接受对象类型InstrumentsInfo的构造函数

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);

可以,您可以在TrackInstruments中创建接受对象类型InstrumentsInfo的构造函数

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);

看这个问题:看这个问题:谢谢,我想做你的建议,但传递instrumentInfo我会失去我在方法调用中使用的参数的可见性,即名称、ID等。谢谢,我想做你的建议,但传递instrumentInfo我会失去我在方法调用中使用的参数的可见性,即名称,ID等。生成器听起来是个好主意,因为我会在方法中传递的参数上保持可见性。生成器听起来是个好主意,因为我会在方法中传递的参数上保持可见性。