这个JAVA程序错在哪?

来源:百度知道 编辑:UC知道 时间:2024/06/15 03:09:31
public class Do {
abstract class Person{
public Person(String s){ name=s; }
public abstract String getDescription();
public String GetName(){ return name;}
private String name;
}
class Student extends Person{
public Student(String m,String n)
{ super(m);major=n;}
public String getDescription()
{ return "a student major in" + major;}
private String major;
}
public static void main(String[] args) {
Person[ ] per=new Person[2];
per[0]= new Student("King","English");
per[1]= new Student("Queen","Spanish");
for(Person p : per)
System.out.println(p.GetName()+","+p.getDescription());}
}

问题出在这里
per[0] = new Student("King", "English");
per[1] = new Student("Queen", "Spanish");
Student 是Do的内部类,所以只有Do的对象才能创建Student.
看看这样的代码:
package ttt;

public class Do {
abstract class Person {
public Person(String s) {
name = s;
}

public abstract String getDescription();

public String GetName() {
return name;
}

private String name;
}

class Student extends Person {
public Student(String m, String n) {
super(m);
major = n;
}

public String getDescription() {
return "a student major in" + major;
}

private String major;
}

public static void main(String[] args) {
Person[] per = new Person[2];
Do d = new Do();
per[0] = d.new Student("King", "English");
per[1] = d.new Stude