逻辑补码运算符?

发布于 2024-12-04 10:41:55 字数 924 浏览 1 评论 0原文

是否可以使用逻辑运算符“!”在具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鱼窥荷 2024-12-11 10:41:56

例程ISREVER 仅在公文>对象, 不是上。要使用否定操作您必须尝试以下操作

for(BriefCase c: cases) {
    if (!c.isRemoved()) {
        // block of code
    }
}

The routine isRemoved only exist on the Briefcase object, not on the array. To use the negating operation ! you have to try something like the following

for(BriefCase c: cases) {
    if (!c.isRemoved()) {
        // block of code
    }
}
独闯女儿国 2024-12-11 10:41:56

是的,您可以,但不能使用代码中所示的数组,而是使用 !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.

静待花开 2024-12-11 10:41:56

这是。 boolean 是一种原始类型,但我认为使用 ! 没有什么奇怪的。

It is. boolean is a primitive type, but I see nothing weird with using !.

今天小雨转甜 2024-12-11 10:41:56

我不确定您是否错误或设计地将索引留在了数组中。但这肯定会起作用:

if(!cases[i].isRemoved()){ .... block of code}

其中 i 是索引。

I am not sure whether you left the index in the array by mistake or design.. but this will definitely work:

if(!cases[i].isRemoved()){ .... block of code}

where i is the index.

徒留西风 2024-12-11 10:41:56

在这种情况下 !否定调用 isRemoved() 的值
您可以在这里查看运算符列表:http://www.java-tips.org/java-se-tips/java.lang/what-is-java-operator-precedence.html

另外关于答案说你不能写:

Briefcase[] cases = new Briefcase[];
if (!cases[5].isRemoved()) { .... block of code}

[] 和。 () 运算符具有相同的优先级。

[] 索引运算符将首先执行,因为该行是从左到右计算的,从而检索一个 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:

Briefcase[] cases = new Briefcase[];
if (!cases[5].isRemoved()) { .... block of code}

[] 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文