Поскольку у вас есть несколько классов BooksChild, вам потребуется класс BookParents, который расширяет классы BooksChild. Вы можете использовать по-прежнему использовать массив BookParents:
store = new BookParents[100];
Внутри каждого BooksChild вы будете использовать конструктор, который выглядит примерно так:
public class BooksChild1 extends BookParents{
String myISBN, myTitle, myAuthor;
public BooksChild1(String isbn, String title, String author){
super(1, isbn, title, author); //1 denotes the class type
}
}
Внутри класса BookParents у вас будет другой конструктор:
public class BookParents{
int myType;
String myISBN, myTitle, myAuthor;
public BookParents(int type, String isbn, String title, String author){
//when a new BooksChild1 is created, it will set the myType equal to 1
myType = type;
myISBN = isbn;
myTitle = title;
myAuthor = author;
}
//this accessor will allow you to find the type of BooksChild
public int getMyType(){
return myType;
}
}
`
Теперь для части магазина, добавив BooksChild в массив BooksParent:
public class Store{
//these declarations must be public if you want to use them in a public methods
public BookParents store[];
public int ind, max;
public Store() {
ind=0; //Begin 0 and change with the method AddBooks;
max=100;
store = new BookParents[100];
}
public String addBook(BookParnets bp){
/*must be max-1 or otherwise it will try
to push a final ind of 100 which is out of bounds*/
if(ind<max-1){
store[ind++]=bp;
return "TheBooksChild added correctly";
}
return "Full store";
}
}
Последняя часть будет существовать в вашем классе драйверов, когда вы объявляете объекты типа BooksChild для вставки в свое хранилище, оператор объявления для BooksChild1 будет выглядеть следующим образом:
BookParents b = new BooksChild1("isbn", "title", "author");
Если вам когда-либо понадобится найти тип BooksChild в массиве вашего магазина, вы можете использовать следующий тип для возврата типа (пусть store [0] содержит BooksChild неизвестного типа):
store[0].getMyType();