[英]Java Error: The constructor is undefined (2)
问题:我的重载构造函数是未定义的,即使我在做正确的事情(我认为是这样?)。
源代码:
class Book {
String section;
float subject2;
char section2, author;
int subject, author2, year;
Book(String section, int subject, char author, int author2, int year) {
this.section = section;
this.subject = subject;
this.author = author;
this.author2 = author2;
this.year = year;
}
Book(char section2, float subject2, char author, int author2, int year) {
this.section2 = section2;
this.subject2 = subject2;
this.author = author;
this.author2 = author2;
this.year = year;
}
void displayData() {
System.out.println(section + subject + " ." + author + author2 + " " + year);
}
void displayData2() {
System.out.println(section2 + subject2 + " ." + author + author2 + " " + year);
}}
public class TestBook {
public static void main(String[] args) {
Book book1 = new Book("LB", 2395, "C", 65, 1991);
Book book2 = new Book("E", 185.86, "P", 277, 2010);
book1.displayData();
book2.displayData2();
s.close();
}}
如果打扰,请为长度道歉。 感谢您的帮助!
你打电话时 :
Book book2 = new Book("E", 185.86, "P", 277, 2010);
如果要使用 char 调用第二个构造函数,请将“E”更改为“E”,否则“E”是 String 对象。 领域author
(将“P”更改为“P”)
顺便说一句,您可以覆盖toString()
方法,而不是创建displayData()
toString()
方法:
@Override
public String toString() {
return "your string with information";
}
所以你可以调用这个方法:
System.out.println(book.toString());
System.out.println(book); // The same thing ! It will call the toString() method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.