如何 return class 在 Java 中输入方法

how to return a class type in a method in Java

我已经实现了一种特定的方法来搜索与给定 ISBN 编号相对应的书籍和 return 那本书。但是我在该方法(searchBook 方法)中将 return 类型用作 void,因为我不知道如何 return 一本“书”。那么我如何 return 那本书而不是打印它呢?

    public class User {
        int ID;
        String name;
        String email;
        int age;
        String isbn;

        public User(int ID,String name,int age){
            this.ID = ID;
            this.name = name;
            this.age = age;
        }

        public static void searchBook(Book[] b, String isbn) {
            int found = 0;
            for (int i = 0; i < 6; i++) {
                if (b[i].ISBN == isbn) {
                    found = 1;
                    System.out.println(b[i].title);
                    break;
                }
            }
            if (found == 0) {
                System.out.println("ISBN Not Found");
            }
        }
    }

    public class Book {
        String title;
        String author;
        String ISBN;
        float rating;
        int noOfDays;

        public  static void displayBookDetails(Book[] b){   
            System.out.println("Title\t\t\t\tAuthor\t\tISBN\t\tRating");
            for(int i=0;i<b.length;i++)
            {
                System.out.println(b[i].title+"\t\t\t"+b[i].author+"\t"+b[i].ISBN +"\t"+b[i].rating);
            }
        }

        //book constructor
        public Book(String title,String author ,String ISBN,float rating){
            this.title = title;
            this.author = author;
            this.ISBN = ISBN;
            this.rating = rating;
        }
    }
public class Driver {

    

    public static void main(String[] args) {


Book[] arr = new Book[6];
        arr[0] = new Book("Rabbi Zidni Ilma", "Martin", "2194-5357", 6.5f);
        arr[1] = new Book("A story of a soldier","Martin", "2193-4567", 3.2f);
        arr[2] = new Book("Apple Garden", "Rexon", "2104-3080", 1.2f);
       }
}

**System.out.println(Customer.searchItem(item,53965307));**

下面是一个示例,说明如何 return 从方法中获取对象。

要比较包括字符串在内的对象,您需要使用等号而不是 == 运算符。 == 运算符仅适用于原始值。

class Book {
    String title;
    Book(String title){ this.title = title; }
}

class Main{
    
    static Book find(Book[] books, String title){
        for(int i=0; i<books.length; i++)
            if (books[i].title.equals(title)){
                return books[i];
        return null;
    }
    
    public static void main(String[] args) {
        Book[] books = {new Book("A"), new Book("B"), new Book("C")};
        Book b = find(books, "B");
        System.out.println(b.title);
    }
}

只是 return Book 找到书后的对象。并且有一个错误。字符串值应通过 equals 方法进行比较。

    public static Book searchBook(Book[] books, String isbn) {
        for (Book book : books) {
            if (book.ISBN.equals(isbn)) {
                return book;
            }
        }
        // or throw new RuntimeException("Not Found Book matching isbn");
        return null;
    }
覆盖 toString 方法
class Book {

    // ... other code

    @Override
    public String toString() {
        return title + "\t\t\t"+ author+"\t"+ ISBN +"\t"+ rating;
    }
}

声明该方法 searchBook returns Book(而不是 void)并添加两个 return 语句:如果 Book找到了,如果没有找到另一个。

/**
 * Returns the book having the supplied ISBN in array 'b' or null if
 * no book in 'b' has the given ISBN.
 *
 * @param b - array of books to search.
 * @param isbn - ISBN to search for in 'b'.
 *
 * @return  Book with given ISBN or null if nothing found.
 */
public static Book searchBook(Book[] b, String isbn) {
    // Always make sure that the method arguments are valid.
    if (isbn != null && !isbn.isEmpty()) {
        if (b != null && b.length > 0) {
            for (int i = 0; i < b.length; i++) {
                if (b[i].ISBN.equals(isbn)) {
                    return b[i];
                }
            }
            return null;
        }
        else {
            throw new IllegalArgumentException("No books to search.");
        }
    }
    else {
        throw new IllegalArgumentException("Invalid ISBN.");
    }
}

如果您至少使用 Java 8,那么您可以使用流 API

public static java.util.Optional<Book> searchBook(Book[] b, String isbn) {
    if (isbn == null || isbn.isEmpty()) {
        throw new IllegalArgumentException("Invalid ISBN.");
    }
    if (b == null || b.length == 0) {
        throw new IllegalArgumentException("No books to search.");
    }
    return java.util.Arrays.stream(b)
                           .filter(book -> isbn.equals(book.ISBN))
                           .findFirst();
}

参考How do I compare strings in Java判断两个字符串是否相等