逻辑补码运算符?
是否可以使用逻辑运算符“!”在具有 true 或 false 值的对象上?专门针对这样的物体?
public class Briefcase {
private final double amount;
private final String model;
private boolean removed = false;
private String face;
public Briefcase(double amount, int face, String model) {
this.face = Integer.toString(face);
this.amount = amount;
this.model = model;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return face;
}
public String getModel() {
return model;
}
public void remove() {
removed = true;
face = "X";
}
public boolean isRemoved() {
return removed;
}
}
然后像这样使用它
Briefcase[] cases = new Briefcase[];
if (!cases[5].isRemoved()) { .... block of code}
可以吗?如果是的话,请给我提供该文档的链接以及其他一些示例,我觉得这很奇怪,同时又很有趣
is it possible to use the logical operator "!" on object that holds a value of true or false? specifically for an object like this?
public class Briefcase {
private final double amount;
private final String model;
private boolean removed = false;
private String face;
public Briefcase(double amount, int face, String model) {
this.face = Integer.toString(face);
this.amount = amount;
this.model = model;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return face;
}
public String getModel() {
return model;
}
public void remove() {
removed = true;
face = "X";
}
public boolean isRemoved() {
return removed;
}
}
then use it like this
Briefcase[] cases = new Briefcase[];
if (!cases[5].isRemoved()) { .... block of code}
is that possible? if so provide me links to that document that and some other examples, I find this weird at the same time interesting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
例程
ISREVER
仅在公文>
对象, 不是上。要使用否定操作!您必须尝试以下操作The routine
isRemoved
only exist on theBriefcase
object, not on the array. To use the negating operation ! you have to try something like the following是的,您可以,但不能使用代码中所示的数组,而是使用
!case.isRemoved()
。这里的要点是 !(negate operatior) 适用于布尔操作数。
只要操作数表达式解析为布尔值 ! 就可以了。
Yes you can, but not with array as shown in code but as
!case.isRemoved()
.The point here is that !(negate operatior) works on boolean operands.
As long as the operand-expression resolves to a boolean ! this will work.
这是。
boolean
是一种原始类型,但我认为使用!
没有什么奇怪的。It is.
boolean
is a primitive type, but I see nothing weird with using!
.我不确定您是否错误或设计地将索引留在了数组中。但这肯定会起作用:
其中 i 是索引。
I am not sure whether you left the index in the array by mistake or design.. but this will definitely work:
where i is the index.
在这种情况下 !否定调用 isRemoved() 的值
您可以在这里查看运算符列表:http://www.java-tips.org/java-se-tips/java.lang/what-is-java-operator-precedence.html
另外关于答案说你不能写:
[] 和。 () 运算符具有相同的优先级。
[] 索引运算符将首先执行,因为该行是从左到右计算的,从而检索一个 Briefcase 实例,您可以在该实例上调用 isRemoved() 方法。
In this case ! negates the value of the call isRemoved()
You can have a look here to see the list of operators: http://www.java-tips.org/java-se-tips/java.lang/what-is-java-operator-precedence.html
Also regarding the answer that said you can't write:
[] and . () operators have the same precedence.
The [] index operator will perform first because the line is evaluated from left to right, thus retrieving a Briefcase instance, on which you can call the isRemoved() method.