gpt4 book ai didi

java - 将 boolean 方法调用到另一个类中时遇到问题

转载 作者:行者123 更新时间:2023-11-30 02:19:07 24 4
gpt4 key购买 nike

我在将 boolean 方法调用到另一个类中时遇到问题。无论我尝试什么,程序要么找不到该变量,要么警告我尝试在静态上下文中使用非静态变量。

我尝试访问的 boolean 代码

public boolean isSame(String title, String author)
{
return this.title.equals(title) &&
this.author.equals(author);
}

我用来访问该方法的(当前)代码(我知道这是错误的)

public void copyCount(String title, String author)
{
int numberOfCopy = 0;
boolean Book.isSame = false;
if(isSame == true) {
numberOfCopy++;
}
System.out.println(copyCount);
}

本质上,copyCount 方法(来自“Library”类)应该采用 2 个 String 参数,并使用 isSame 方法(来自“Book”类)测试它们。如果书籍详细信息与给定的详细信息匹配,则 isSame 返回 true,如果不匹配,则返回 false。

如何正确引用另一个类的 isSame 方法?谢谢。

最佳答案

How do I properly reference the isSame method from another class?

看起来您需要一个 Book 类的实例...

Book book = new Book("myTitle", "myAuthor");  // I'm just guessing there's a constructor like this
boolean same = book.isSame("anotherTitle", "anotherAuthor");
// 'same' will be false

关于java - 将 boolean 方法调用到另一个类中时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com