简体   繁体   English

有关构建对象括号的问题

[英]Issue about casting object brackets

I have noticed that there are two ways to cast objects (the difference is the placement of the outer parenthesis): 我注意到有两种方法可以转换对象(区别在于外括号的位置):

 1. SimpleType simpleType = ((SimpleType) (property.getType()));
 2. SimpleType simpleType = ((SimpleType) property).getType();

Are they doing the same thing ? 他们做同样的事吗?

Are they doing the same thing ? 他们做同样的事吗?

No they are not. 不,他们不是。

  • The first one is casting your value returned from property.getType() to SimpleType . 第一个是将从property.getType()返回的值转换为SimpleType ( Invocation is done before Casting ) 调用Casting之前完成)
  • The second one is first casting your property to SimpleType and then invoking the getType() method on it. 第二个是首先将propertySimpleType ,然后在其上调用getType()方法。 ( Casting is done before Invocation ). 铸件调用之前完成)。

You can also understand it from the precedence of parenthesis . 您也可以从括号的优先级中理解它。 Since it has the highest precedence, it will be evaluated first. 由于它具有最高优先级,因此将首先进行评估。

First Case: - 第一个案例: -

So, in ((SimpleType) (property.getType())); 所以,在((SimpleType) (property.getType())); : - : -

(property.getType())

is evaluated first, then the casting is performed. 首先评估,然后执行铸造。 In fact you don't really need a parenthesis around that. 事实上,你并不需要围绕它的括号。 ( property binds tighter to the dot (.) operator than the cast operator). property结合更紧的dot (.)除了操作者cast操作者)。 So, invocation will always be done before casting. 因此,调用将始终在转换之前完成。 Unless you force it to reverse as in the below case: - 除非你强制它反转,如下例所示: -

Second Case : - 第二种情况: -

In ((SimpleType) property).getType() : - ((SimpleType) property).getType() : -

((SimpleType) property)

is evaluated first, then the invocation is done. 首先进行评估,然后完成调用。 As, now you have enclosed property inside the brackets, due to which it binds tighter to the cast operator, due to higher precedence enforced by parenthesis . 至于,现在你已经封闭property的支架,因为它结合紧,里面cast运营商,由于较高的优先级用括号执行。

They are doing two totally unrelated things: first is dowcasting the result of getType() and the second is downcasting the property variable. 他们正在做两件完全不相关的事情:第一件是对getType()的结果进行评估,第二件是向下转换property变量。 The first one looks like the one you need, given the type of the left-hand side. 考虑到左侧的类型,第一个看起来像您需要的那个。 Note that in the first example you have extra parentheses, this would be enough, and is how this is idiomatically written: 请注意,在第一个示例中,您有额外的括号,这就足够了,这是如何以惯用法编写的:

SimpleType simpleType = (SimpleType) property.getType();

They are not the same. 他们不一样。

  • The first casts the result of getType to SimpleType . 第一个将getType的结果转换为SimpleType
  • The second casts property to SimpleType and calls getType on it, yet the result of getType is not casted to SimpleType . 第二个将property转换为SimpleType并在其上调用getType ,但getType的结果不会转换为SimpleType

They are entirely doing diffrent things. 他们完全在做不同的事情。 they are not same at all. 它们根本不相同。

 1. SimpleType simpleTypee = ((SimpleType) (property.getType()));

This first invokes getType of property and then casts the returned Object to SimpleType 这首先调用属性的getType,然后将返回的Object强制转换为SimpleType

 2. SimpleType simpletype = ((SimpleType) property).getType();

This first casts the property to SimpleType and then invokes getType on the SimpleType 这首先将属性转换为SimpleType,然后在SimpleType上调用getType

No they are not doing the same thing. 不,他们没有做同样的事情。

SimpleType simpleTypee = ((SimpleType) (property.getType()));

First the property.getType() will be called and the object returned by property.getType() will be casted to SimpleType 首先, property.getType()将被调用和返回的对象property.getType()将被强制转换为SimpleType

and in the second case 在第二种情况下

SimpleType simpletype = ((SimpleType) property).getType();

first the object property will be casted to SimpleType and then getType() will be invoked on that newly casted object 首先将object property转换为SimpleType ,然后在该新转换的对象上调用getType()

They are doing different things as detailed in the other answers. 他们正在做其他答案中详细说明的不同事情。 You can also cast by doing this 你也可以这样做

SimpleType.class.cast(property.getType());
SimpleType.class.cast(property).getType();

Depending on what you actaully want to cast. 取决于你实际想要施展的内容。 Either property or the result of getType(). 属性或getType()的结果。 I prefer this syntax as it is more explicit and easier on the eye... 我更喜欢这种语法,因为它在眼睛上更明确,更容易......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM