为什么这个编译不正确?我添加了main方法行来尝试对其进行测试,但是我遇到了很多错误,说私有构造函数以及公共表达式都是非法的表达式,除了说它们不是语句之外。它还要求我在我认为不必要的地方添加分号,因为它们是方法的开始。我不希望有人为我重新输入代码,但有人可以至少将我指向正确的方向并告诉我我要去哪里了吗?
import java.util.Arrays
public class Book{
public static void main (String[] args) {
private String title;
private String authors[];
public Book() {
title = "Test";
authors = null;
}
public Book(String title, String[] authors) {
this.title = title;
this.authors = authors;
}
public String getterTitle() {
return title;
}
public void setterTitle(String title) {
this.title = title;
}
public String[] getterAuthors() {
return authors;
}
public void setterAuthors(String[] authors) {
this.authors = authors;
}
public String bookToString() {
return "" + getterTitle() + " by " + getterAuthors() + "";
}
}
答案 0 :(得分:2)
在java.util.Arrays
之后您缺少分号,并且您的main
没有右括号}
:
import java.util.Arrays;
public class Book {
private String title;
private String authors[];
public Book(String title, String[] authors) {
this.title = title;
this.authors = authors;
}
public String getterTitle() {
return title;
}
public void setterTitle(String title) {
this.title = title;
}
public String[] getterAuthors() {
return authors;
}
public void setterAuthors(String[] authors) {
this.authors = authors;
}
public String bookToString() {
return "" + getterTitle() + " by " + getterAuthors() + "";
}
public static void main (String[] args) {
// Do something here
}
}
答案 1 :(得分:1)
我想我明白你的意思。
因此,您想参加2节课。
这是主要的。
TestBook.java
package testbook;
public class TestBook {
public static void main(String[] args) {
// TODO code application logic here
Book bookClass = new Book();
}
}
这是一堂课。
Book.java
package testbook;
public class Book {
private String title;
private String authors[];
public Book() {
title = "Test";
authors = null;
}
public Book(String title, String[] authors) {
this.title = title;
this.authors = authors;
}
public String getterTitle() {
return title;
}
public void setterTitle(String title) {
this.title = title;
}
public String[] getterAuthors() {
return authors;
}
public void setterAuthors(String[] authors) {
this.authors = authors;
}
public String bookToString() {
return "" + getterTitle() + " by " + getterAuthors() + "";
}
}
答案 2 :(得分:0)
import java.util.Arrays; // the semi - colon is missing on this line
public class Book{
private String title;
private String authors[]; // to make these variables global you
// need to declare them outside of a method
public static void main (String[] args) {
} // you need to close the method before beginning with others
更正以上内容即可为您修复代码。