关于java继承的No enclosing instance of。。。。错误
我刚学java的继承,接口,今天参照教材写了一段代码但出现了错误,我百思不得其解··
package text1;
import text1.Person.GraduteStudent;
import text1.Person.Student;
class Person
{
private String name;
private int age;
protected static int count=0;
public Person(String name,int age)
{
this.name = name;
this.age = age;
count++;
}
public void print()
{
System.out.print("姓名:"+name+"年龄:"+age);
}
class Student extends Person{
private String speciality;
public Student(String name,int age,String spec)
{
super(name, age);
this.speciality = spec;
}
public void print(){
System.out.println();
super.print();
System.out.print("专业:"+speciality);
}
}
class GraduteStudent extends Student
{
private String tutor;
public GraduteStudent(String name,int age, String spec,String tutor){
super(name, age, spec); /*错误出现在这里,错误提示是:No enclosing instance of type Person is available due to some intermediate constructor invocation*/
this.tutor = tutor;
}
public void print(){
System.out.println();
super.print();System.out.println("导师是:"+tutor);
}
}
}
public class text {
public static void main(String[] args) {
Person pl = new Person("远见", 21);
pl.print();
Student sl = new Student("卓识", 19, "计算机"); /*这里也有个错误,我觉得//是因为上面的错误的关系,提示是:No enclosing instance of type Person is accessible. Must qualify the allocation with an enclosing instance of type Person (e.g. x.new A() where x is an instance of Person).*/
sl.print();
GraduteStudent gra = new GraduteStudent("乘势", 22, "空间物理", "蓝天野");
gra.print();
System.out.println("总人数:"+Person.count);
}
}
感觉快要崩溃··求大神们看看到底哪里出错了····
非常感谢 java 继承 eclipse
[解决办法]
Person p = new Person();
Student s = p.new Student();
要这样才行
[解决办法]
先跟你解释第二个错误,第一个错误一楼和二楼解释了
你定义的内部类是成员内部类,生成对象的方式是
OuterClass.InnerClass inner = new OuterClass().new InnerClass();
Person.GraduteStudent gra = new Person("zhangsan",19).new GraduteStudent("乘势", 22, "空间物理", "蓝天野");