有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从tester类访问/更改ArrayList?

在过去的几个小时里,这个问题一直困扰着我,我试图弄清楚如何从我的tester类访问“Library”类中存储的图书对象的arraylist,我确实有getter和setter,但我无法弄清楚如何从tester类访问它们。这是我的密码

书。爪哇

public class Book {
private int id;
private String bookTitle;
private String authorName;
private double bookReleaseYear;
private int numOnLoan;
private int numInStock;

//constructor
public Book(int id, String bookTitle, String authorName, double bookReleaseYear, int numOnLoan, int numInStock) {
    this.id = id;
    this.bookTitle = bookTitle;
    this.authorName = authorName;
    this.bookReleaseYear = bookReleaseYear;
    this.numOnLoan = numOnLoan;
    this.numInStock = numInStock;
}
//getters
//setters

图书馆。爪哇

import java.util.ArrayList;
import java.util.Scanner;

public class Library {

public ArrayList<Book> books = new ArrayList<Book>();

//Constructor
public Library(ArrayList<Book> books) {
    this.books = books;
}
//Getters/setters

public ArrayList<Book> getBooks() {
    return books;
}

public void setBooks(ArrayList<Book> books) {
    this.books = books;
}

图书馆测试员。爪哇

import java.util.ArrayList;
import java.util.Scanner;
public class LibraryTester {

public static void main(String[] args) {

    Library<Book> book1 = new Library();
}

我已经在谷歌上搜索了好几个小时,除非我碰巧错过了,提前谢谢你的帮助


共 (0) 个答案