2015-01-01 3 views
2

저는 기초 클래스 작성 자습서를 작성하고 아래 지침이 제공되는 초보자입니다. 지시에 따라 형식의 메서드가 인수에 적용되지 않습니다.

1) Create a new class called Book. 
2) Create fields for title, author, numPages, and isbn. 
3) Create a method called toString that appropriately describes the book. 
4) In the file, BookRunner.java: 
    a) Declare two book variables 
    b) Instantiate two book objects and set their fields. 
    c) Print descriptions of the book objects using their toString methods. 

그래서 나는 두 개의 클래스를 사용하여 작업 있었고, 내가 방법 toString를 호출 마지막 두 줄 때까지 내 코드가 오류를 보여줍니다

public class Book { 
    public String title; 
    public String author; 
    public String numPages; 
    public String isbn; 

    public void toString(String bookName) { 
     String description = "Title:" + title + "Author"+ author + "Num. of pages" + numPages + "ISBN" + isbn; 
     System.out.println(description); 
    } 

public class Ex1_BookRunner { 

    public static void main(String[] args) { 
     Book firstBook; 
     Book secondBook; 

     firstBook = new Book(); 
     secondBook = new Book(); 

     firstBook.title = "One Piece"; 
     firstBook.author = "Oda-Sensei"; 
     firstBook.numPages = "100"; 
     firstBook.isbn = "123456"; 

     secondBook.title = "Life of Megan Fox"; 
     secondBook.author = "Micheal Bay"; 
     secondBook.numPages = "200"; 
     secondBook.isbn = "098765"; 

     toString(firstBook); 
     toString(secondBook); 
    } 
} 

아래의 코드를 볼 수 있습니다.

나는

The method toString() in the type Object is not applicable for the arguments (Book) 

아래의 오류가 내 메소드 선언 어딘가에서 몇 가지 근본적인 오류를 만드는 건가요거야? 나는 다른 질문을 똑같은 질문으로 보았지만, 아직 배운 적이없는 코드 구문을 다루기 때문에 설명을 이해하는 데 어려움이 있습니다.

어떤 도움에 감사드립니다 :)

+0

는 같은 날 것으로 보인다이'numPages'없이,'int'로 더 적합 할 것입니다 시도가 있습니까? – Floegipoky

답변

0

public String toString() { 
    String discription= "Title:" + title + "Author"+ author + "Num. of pages" + numPages + "ISBN" + isbn; 
    return discription; 
} 

우리의 toString 메소드를 시도하고 당신의 주요 방법 인쇄 문이

과 같아야합니다
System.out.println(firstBook.toString()); 
    System.out.println(secondBook.toString()); 

도움이 되었으면 좋겠다.

1

firstBook.toString(); 시도 - 이것은 당신이 그것을보고 싶다면, 당신은 System.out.println(firstBook.toString());과 같은 작업을 수행 할 수 있습니다, 문자열을 생성합니다. 이 오류는 book을 매개 변수로 사용하는 toString 메서드가 없다는 것을 알려줍니다. 대신 원하는 것은 toString() 메서드 에 작성한 Book 인스턴스를 호출하는 것입니다. 당신은 당신이 만든 firstBook 및 secondBook() 메소드 디폴트로 .toString를 호출 할 수 있습니다

firstBook.toString(); 
secondBook.toString(); 

:

0
가로 변경

. 그러나 Book 객체를 매개 변수로 사용하는 toString 메서드를 정의하지 않았으므로 Book 객체를 toString()에 전달하면 앞에서 설명한 오류가 발생합니다.

사실

, 당신의 오류 라인은 매우 명확하게 설명 :

The method toString() in the type Object is not applicable for the arguments (Book) 
0

같은 클래스

 public class Book { 
//your codes or variables 
} 
    public class Ex1_BookRunner { 
//your code or variables 
} 

두 public 클래스는

+0

올바르지 않습니다. 또한이 질문에는 이미 대답이 있습니다. – avojak

관련 문제