尝试解决psvm中的错误,老实说,我不知道什么是错的或google与此有关。 它不会让我发布这个没有更多的话,但我没有知道什么是错误的程序,以提供更多的细节。
public class backToSchoolDalton{
public class Person{
//Person Code
public class Teacher extends Person{
//TeacherCode
public class Student extends Person{
//StudentCode
public class CollegeStudent extends Student {
//CollegeStudentCode
public static void main(String[] args) {
ERROR HEAVEN RIGHT HERE
backToSchoolDalton a = new backToSchoolDalton();
backToSchoolDalton b = new backToSchoolDalton();
backToSchoolDalton c = new backToSchoolDalton();
backToSchoolDalton d = new backToSchoolDalton();
a.Person("Coach Bob", 27, "M");
b.Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
c.Teacher("Duke Java", 34, "M", "Computer Science", 50000);
d.CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
Person bob = new Person("Coach Bob", 27, "M");
System.out.println(bob);
Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
System.out.println(lynne);
Teacher mrJava = new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
System.out.println(mrJava);
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
System.out.println(ima);
}
}
请帮忙!
答案 0 :(得分:0)
public static void main(String[] args) {
backToSchoolDalton s = new backToSchoolDalton();
Person bob = s.new Person("Coach Bob", 27, "M");
System.out.println(bob);
Student lynne = s.new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
System.out.println(lynne);
Teacher mrJava = s.new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
System.out.println(mrJava);
CollegeStudent ima = s.new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
System.out.println(ima);
}
答案 1 :(得分:0)
有两种(常见)类型的嵌套类。 Inner classes and static nested classes.内部类与其周围类的实例耦合,这意味着您需要在使用它之前构造周围类的实例。另一方面,静态嵌套类不是。它就像任何其他类一样,除非出于组织原因在另一个类中定义。
您发布的用于构建backToSchoolDalton
实例的解决方案之所以有效,是因为您现在拥有了周围类的实例。但这实际上并不是你想要的。相反,只需将内部类设置为静态嵌套类,就可以直接构造这些类。
public static class Person {
//Person Code
}
public static class Teacher extends Person {
//TeacherCode
}
...
Teacher c = new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
答案 2 :(得分:-2)
编辑:我的下面的回答是错误的。只要该类在构造函数中没有这样做,就可以在同一个类中创建一个类的实例。不删除我的答案,以便其他人可以从我的错误中吸取教训
据我所知,你不能在同一个类中声明一个类的实例。你需要将main()移到backtoSchoolDalton()之外:
backToSchoolDalton() {
//dalton code
}
public static void main(String[] args) {
backToSchoolDalton a = new backToSchoolDalton();
backToSchoolDalton b = new backToSchoolDalton();
backToSchoolDalton c = new backToSchoolDalton();
backToSchoolDalton d = new backToSchoolDalton();
a.Person("Coach Bob", 27, "M");
b.Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
c.Teacher("Duke Java", 34, "M", "Computer Science", 50000);
d.CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
Person bob = new Person("Coach Bob", 27, "M");
System.out.println(bob);
Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
System.out.println(lynne);
Teacher mrJava = new Teacher("Duke Java", 34, "M", "Computer Science",
50000);
System.out.println(mrJava);
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
System.out.println(ima);
}