Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:访问不同类中的不同变量以在条件语句中使用_Java_Getter Setter_Bluej - Fatal编程技术网

Java:访问不同类中的不同变量以在条件语句中使用

Java:访问不同类中的不同变量以在条件语句中使用,java,getter-setter,bluej,Java,Getter Setter,Bluej,我在论坛上做了一些研究,发现这是最适合我的问题,但解决方案不起作用: 因此,我尝试访问“LibraryCard”类中的两个变量: 我发现如果我想在我的第二个类“学生”中访问它们,我必须在我的“LibraryCard”类中添加一个get方法: 在访问这两个变量后,我需要在“学生”类的if语句中使用它们: 我是这样实现的 public boolean finishedStudies() { if ( (this.booksBorrowed = 0) && (t

我在论坛上做了一些研究,发现这是最适合我的问题,但解决方案不起作用:

因此,我尝试访问“LibraryCard”类中的两个变量:

我发现如果我想在我的第二个类“学生”中访问它们,我必须在我的“LibraryCard”类中添加一个get方法:

在访问这两个变量后,我需要在“学生”类的if语句中使用它们: 我是这样实现的

    public boolean finishedStudies()
    {
    if ( (this.booksBorrowed = 0) && (this.booksBorrowed >= this.limit)) {
        return true;
    }

    else
       return false;
    }
当我试图编译它时,BlueJ说它找不到变量和限制


一般来说,我对Java和编程非常陌生,非常感谢您的帮助。

您可以在学生类中创建LibraryCard类的实例,然后通过调用该实例上的getter来访问这两个变量:

LibraryCard card = new LibraryCard();
int limit = card.getlimit();
int booksBorrowed = card.getbooksBorrowed();

你认为
this
是什么意思?`if((this.booksBorrowed=0)和&(this.booksBorrowed>=this.limit)){`除了使用“this”代替LibraryCard实例的方法的问题之外,这里还有多个错误。逻辑(使用
=
&
)我希望你们不介意回答以下问题:1.这段代码是在哪一部分中编写的?在BlueJ中,它分为变量、构造函数和方法2.什么是“卡片”在这个特定的环境中,我不能回答任何问题,因为我不知道你想做什么。你应该看一些关于Java面向对象编程的教程
    public boolean finishedStudies()
    {
    if ( (this.booksBorrowed = 0) && (this.booksBorrowed >= this.limit)) {
        return true;
    }

    else
       return false;
    }
LibraryCard card = new LibraryCard();
int limit = card.getlimit();
int booksBorrowed = card.getbooksBorrowed();