简体   繁体   中英

how can I return a String?

package book1;
import java.util.ArrayList;


public abstract class Book  {
    public String Book (String name, String ref_num, int owned_copies, int     loaned_copies ){
    return;

    }
}

class Fiction extends Book{
    public Fiction(String name, String ref_num, int owned_copies, String author) {

            }

        }

at the moment when i input values into the variable arguments and call them with this :

public static class BookTest {
    public static void main(String[] args) {
        ArrayList<Book> library = new ArrayList<Book>();
        library.add(new Fiction("The Saga of An Aga","F001",3,"A.Stove"));
        library.add(new Fiction("Dangerous Cliffs","F002",4,"Eileen Dover"));


        for (Book b: library) System.out.println(b);
        System.out.println();


    }
}

i get a return value of this:

book1.Fiction@15db9742
book1.Fiction@6d06d69c
book1.NonFiction@7852e922
book1.ReferenceBook@4e25154f

how can i convert the classes to return a string value instead of the object value? I need to do this without changing BookTest class. I know i need to use to string to convert the values. but i don't know how to catch the return value with it. could someone please point me in the right direction on how to convert this output into a string value?

You need to override the toString() method in your Book or Fiction class. The method is actually declared in the Object class, which all classes inherit from.

@Override
public String toString(){
    return ""; // Replace this String with the variables or String literals that you want to return and print.
}

This method is called by System.out.println() and System.out.print() when they receive an object in the parameter (as opposed to a primitive, such as int and float ).

To reference the variables in the method, you'll need to declare them in the class and store them via the class's constructor.

For example:

public abstract class Book {
    private String name;
    private String reference;
    private int ownedCopies;
    private int loanedCopies;

    public Book (String name, String reference, int ownedCopies, int loanedCopies) {
        this.name = name;
        this.reference = reference;
        this.ownedCopies = ownedCopies;
        this.loanedCopies = loanedCopies;
    }

    @Override
    public String toString(){
        return name + ", Ref:" + reference + ", OwnedCopies: " + ownedCopies + ", LoanedCopies: " + loanedCopies; // Replace this String with the variables or String literals that you want to return and print.
    }
}

You need to overwrite the toString() Method of your Book class. In this class you can generate a String however you like. Example:

@Override
public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append(this.author).append(": ").append(this.title);
  return sb.toString();
}

The classes you have defined, don't store any values. It is in other words useful to construct a new book. You need to provide fields:

public abstract class Book {

    private String name;
    private String ref_num;
    private int owned_copies;
    private int loaned_copies;

    public String Book (String name, String ref_num, int owned_copies, int loaned_copies) {
        this.name = name;
        this.ref_num = ref_num;
        this.owned_copies = owned_copies;
        this.loaned_copies = loaned_copies;
    }

    public String getName () {
        return name;
    }

    //other getters

}

Now an object is basically a set of fields. If you want to print something, you can access and print one of these fields, for instance:

for (Book b: library) System.out.println(b.getName());

In Java, you can also provide a default way to print an object by overriding the toString method:

@Override
public String toString () {
    return ref_num+" "+name;
}

in the Book class.

Need to give your object Book a ToString() override.

http://www.javapractices.com/topic/TopicAction.do?Id=55

Example:

@Override public String toString()
{
    return name;
}

Where name, is a string in the Class.

I am hoping that you have assigned the passed arguments to certain attributes of the classes. Now, once you are done with that, you can override the toString() method in Book to return your customized string for printing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM