1 回答
TA贡献1883条经验 获得超3个赞
公共类型 Book 必须在其自己的文件 - java 中定义,项目中应该为每个公共类型都有一个单独的文件。所以你应该再做一个
您应该将类声明为静态类,或者应该是一个单独的文件
我已经包含了您在 App.class 中添加的 Book.class 作为静态的内容,它将对您有所帮助
//The public type Book must be defined in its own file
//public class Book{
// public String title;
// public String author;
// public int numPages;
//}
public class App
{
public static void main(String [] args)
{
Book book1 = new Book();
book1.title = "Harry Potter";
book1.author = "JK Rowling";
book1.numPages = 400;
System.out.println(book1.title);
Book book2 = new Book();
book2.title = "Lord of the Rings";
book2.author = "JRR Tolkien";
book2.numPages = 300;
System.out.println(book2.title);
}
public static class Book{
public String title;
public String author;
public int numPages;
}
}
添加回答
举报