一种可以从父数组返回子对象的方法?

时间:2018-11-06 15:07:35

标签: java arrays class parent

我正在寻找一种可以从父数组返回子对象的方法-这意味着我如何使方法返回子对象?

问题是我有此方法从数组返回父对象 但在程序中,我需要一个具有其自身属性的子对象,并且该数组具有3个不同的 子对象的类型

这是我到目前为止所拥有的:

class Store {
    private BookParents store[];
    private int ind, max;

    public Store() {
         ind=0; //Begin 0 and change with the method AddBooks;
         max=100;
         store = new BookParents[100];
    } 
public String AddBooks(BooksChild1 a){
    if(ind<max){
        store[ind++]=a;
        return "TheBooksChild added correctly";
    }
      return "Full store";
    }
public String AddBooks(BooksChild2 b){
    if(ind<max){
        store[ind++]=b;
        return "TheBooksChild added correctly";
    }
      return "Full store";
    }
public String AddBooks(BooksChild3 c){
    if(ind<max){
        store[ind++]=c;
        return "TheBooksChild added correctly";
    }
      return "Full store";
    }

    public BooksParents SearchBook(String c) {
        AnyBookChild null1 = new Book1("Unknown", 0,"Unknown","Unknown","Unknown","Unknown","Unknown");

        if(ind!=0){
            for(int i=0 ;i<ind;i++){                    
                if(c.compareTo(store[i].getName(store[i]) )==0)
                    System.out.println(store[i].PrintlnBook());                            
                    return store[i]; 
            }
            System.out.println("Book didn't find, try another name.");
            return null;
         } else {
             System.out.println("There is not books in the store");
             return null;
         }
    }
}

1 个答案:

答案 0 :(得分:0)

由于您有多个BooksChild类,因此需要一个BookChild类扩展的BookParents类。您仍然可以使用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();