如何从存储在ArrayList

时间:2017-05-21 03:28:36

标签: java arraylist tostring

我是java和面向对象编程的新手,但是渴望在java上更好,并且参与stackoverflow社区。

我怀疑这个问题有一个简单的解决方案,但我无法在任何地方找到答案。  我有一堆书籍细节存储在一个arraylist中,每个都有不同的细节级别(即工程书的额外字符串“主题”,而标准书只有3个变量)。  我想知道是否可以从存储在ArrayList中的值调用每个book类的toString方法?  或者,是否可以存储来自ArrayList的每个不同书类方法的toString输出?

这样,如果有工程教科书,它将显示数组列表中的所有细节,而编程教科书将显示较少的细节(如果字符串字段为空,则不会留下'null'。

即。 arraylist将打印:

Textbook1:Java101 / Migoreng / 1331 / Java博士  Textbook2:Python201 / Dr Karl / 112 / Python  教科书3:Fluids101 /布里斯班博士/ 122 / GIS /建模(那里有额外的领域)

等...

如果任何人都可以提供关于可能有用的关键字,那将非常感激。

由于

以下是代码:

构建ArrayList的TestTextBooks类:

import java.util.ArrayList;

public class TestTextbooks {  

        public static void main(String[] args) {
            Textbook java101 = new ProgrammingTextbook("Java101","Dr Migoreng",1331,"Java");
            Textbook python201 = new ProgrammingTextbook("Python201","Dr Karl",112,"Python");
            Textbook fluidMechanics = new EngineeringTextbook("Fluids101","Dr Brisbane",122,"GIS","Fluid Mechanics");
            Textbook modelling101 = new EngineeringTextbook("Modelling101","Dr Suzuki",322,"WaterGems","Water Modelling");

      ArrayList books = new ArrayList();
      books.add(java101); 
      books.add(python201); 
      books.add(fluidMechanics);
      books.add(modelling101);

      System.out.println("" + books.toString());
            }    
    }

编程课本类(添加字段“语言”,如果可用)

public class ProgrammingTextbook extends Textbook {
    private String language;
    private String title;
    private String author;
    private int pages;


    public ProgrammingTextbook (){
    }

        //overloaded constructor
    public ProgrammingTextbook (String title, String author,int pages,String language){
        this.title = title;
        this.author = author;
        this.pages = pages;
        this.language=language;
    }
    //set method for overloaded constructor
    public void setProgrammingTextBook (String title, String author,int pages,String language){
        setLanguage(language);
    }
     //individual set methods
    public void setLanguage(String language){
        this.language = language;
    }
    //individual get methods
    public String getLanguage(){
        return language;
    }  
    public String toString(String title, String author,int pages,String language){
        return title + "/" + author + "/" + pages + "/" + language;
    }

}

工程教科书课程(添加字段语言和主题)

public class EngineeringTextbook extends Textbook {
    private String language;
    private String subject;
        //default constructor (probs shouldn't actually have values)
        public EngineeringTextbook (){
    }
    //overloaded constructor
    public EngineeringTextbook (String title, String author,int pages,String language,String subject){
        this.title = title;
        this.author = author;
        this.pages = pages;
        this.language=language;
        this.subject=subject;
    }
    //set method for overloaded constructor
    public void setEngineeringTextBook (String title, String author,int pages){
        setLanguage(language);
        setSubject(subject);
    }
        //individual set methods
    public void setLanguage(String language){
        this.language = language;
    }
    public void setSubject(String subject){
        this.subject = subject;
    }

    //inbdividual get methods
    public String getLanguage(){
        return language;
    }
    public String getSubject(){
        return subject;
    }

    //toString method
    public String toString(String title, String author,int pages,String language,String subject){
        return title + "/" + author + "/" + pages + "/" + language + "/" + subject;
    }
}

抽象教科书类(定义作者,标​​题和页面)(由所有书籍共享)

public abstract class Textbook {

    protected String title;
    protected String author;
    protected int pages;

    //default constructor (Don't really need to add null for all values but put in to show it is a default constructor
    public Textbook (){
        this.title = null;
        this.author = null;
        this.pages = 0;
    }
    //overloaded constructor
    public Textbook (String title, String author,int pages){
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    //individual set methods
    public void setTitle(String title){
        this.title = title;
    }
    public void setAuthor(String author){
        this.author = author;
    }
    public void setPages(int pages){
        this.pages = pages;
    }
    //individual get methods
    public String getTitle(){
        return title;
    }
    public String getAuthor(){
        return author;
    }
    public int getPages(){
        return pages;
    }
    //abstract toString method
    public String toString(String title, String author,int pages){
       return title + "/" + author + "/" + pages;
    }   
}

1 个答案:

答案 0 :(得分:0)

Object.toString() 采取任何参数。如果未能正确覆盖方法,请使用@Override注释来捕获它。

@Override
public String toString() {
    return title + "/" + author + "/" + pages + "/" + language;
}

@Override
public String toString() {
   return title + "/" + author + "/" + pages;
}