【发布时间】:2016-09-13 23:50:05
【问题描述】:
我正在尝试用 java 中的数组创建一个示例,我想创建一个图书馆,其中包含一些按类别排序的书籍,我想使用 addBook 方法将书籍添加到类别数组中,但我没有如果我有很多数组,我不知道该怎么做,目标是将书添加到相同的类别外壳中,我将 3 个类称为 Estante,Libros,libreria。
package com.manzacatesSAS;
/**
* Created by deborah on 7/09/16.
*/
public class Libreria
{
Estante[] miedo = new Estante[6];
Estante[] comedia = new Estante[6];
Estante[] novelas = new Estante[6];
Estante[] romance = new Estante[6];
Estante[] comics = new Estante[6];
}
package com.manzacatesSAS;
/**
* Created by deborah on 7/09/16.
*/
public class Estante {
//attributes of Estante
private int maxNumLibros = 6;
//I guess this isn't necesay because if i use array,this only has 6 spaces
private int maxPages = 3000;
private String categoria = "";
private int currentLibros = 0;
private int currentPages = 0;
public int getNumLibros() {
return maxNumLibros;
}
public void setNumLibros(int maxNumLibros) {
this.maxNumLibros = maxNumLibros;
}
public int getMaxPages() {
return maxPages;
}
public void setMaxPages(int maxPages) {
this.maxPages = maxPages;
}
public String getCategoria() {
return categoria;
}
public void setCategoria(String categoria) {
this.categoria = categoria;
}
/**
* Constructor de Estantes
*
* @param categoria categoria de los libros
*/
//No añadi los parametros restantes dado que al crear uno nuevo sus atributos deben estar vacios, o definidos por los atributos comunes.
public Estante(String categoria) {
this.categoria = categoria;
}
public void addBook(Libros x){
if (currentPages + x.getNumPages() < maxPages && currentLibros < maxNumLibros) {
currentLibros++;
//I guess the array of the categorie to add the book would be here, and i think that use a for to add the book in the correct array.
}
}
}
【问题讨论】:
-
抱歉,不知道您要的是什么。
-
所以你的图书馆有 5 个书架阵列,每个书架有 6 个书架的空间,总共 30 个书架。我期待在某处看到一系列书籍(
Libros),但没看到?此外,一个数组留给您跟踪实际使用了多少空间,使用ArrayList更容易,但当然,如果这是为了获得数组经验,它应该可以工作。 -
不错的面向对象思维,顺便说一句。