在括号内的方法调用

huangapple go评论64阅读模式
英文:

method call within a parenthesis

问题

这可能是一个基础问题,但我找不到解释。在下面的示例中,

restResponse = Optional.ofNullable((K) (((RestConnector) restConnector).connect(healthCheckVO.getRestConnectorRequest())));

我知道这两个括号:((RestConnector) restConnector) 在调用其 connect() 方法之前需要将变量 restConnector 强制转换为 RestConnector 类型。但为什么调用的结果需要再用另一对括号括起来呢?

(   ((RestConnector) restConnector).connect(healthCheckVO.getRestConnectorRequest())   )
英文:

This might be a basic question but I can't find a explanation of it. In the following example,

restResponse = Optional.ofNullable((K) (((RestConnector) restConnector).connect(healthCheckVO.getRestConnectorRequest())));

I know that two parenthesis: ((RestConnector) restConnector) are needed to cast variable restConnector to type RestConnector before calling its connect() method. but why the result of the call needs to be wrapped in another parenthesis?

(   ((RestConnector) restConnector).connect(healthCheckVO.getRestConnectorRequest())   )

答案1

得分: 5

尽管这些特定的括号并非必需,但通常会使用它们:

这里有一个快速的例子,按预期工作 - 其他变量被强制转换为另一个类:

Class temp = (Class)other;

现在,让我们想象我们需要转换对象并调用方法,这样不会编译通过,因为强制转换应用于 MethodOfClass 的结果,而不是变量 other:

Something temp = ((Class)other).MethodOfClass();

这就是为什么你必须使用的原因:

Something temp = ((Class)other).MethodOfClass();

现在,你想要转换方法的结果:

Something temp = (Something)((Class)other).MethodOfClass();

这样可以工作,直到你再次需要在结果上调用方法,因此最好从一开始就使用括号将所有内容包装起来,这样你以后可以添加方法调用。

但是,所有这些括号实际上会创建混乱,所以作为善意的一部分 - 只需对其进行重构:

var request = healthCheckVO.getRestConnectorRequest();
var connector = (RestConnector)restConnector;
var response = connector.connect(request);
restResponse = Optional.ofNullable((K)response);
英文:

while this particular brackets are not required, it is quite common to use them:

here is quick example, which works as expected - other variable is casted to another class:

Class temp = (Class)other;

now, lets imagine we need to cast object and call method, this does not compile, because casting is applied to result of MethodOfClass, not to variable other

Something temp = (Class)other.MethodOfClass();

this is why you have to use:

Something temp = ((Class)other).MethodOfClass();

now, you want to cast result of the method:

Something temp = (Something)((Class)other).MethodOfClass();

and it works, until again you need to call method on result, so it is much easier to wrap everything in brackets from the very beginning, so you can later add method call

but, all these brackets actually create mess, so as part of good will - just refactor it to:

var request = healthCheckVO.getRestConnectorRequest();
var connector = (RestConnector)restConnector;
var response = connector.connect(request);
restResponse = Optional.ofNullable((K)response);

or something along this idea - less mental load per line of code

huangapple
  • 本文由 发表于 2023年4月20日 04:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定