我刚学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
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
解决方案 »
- jquery实现分页技术
- 我在文本框中输入"0.4,0.18,0.1,0.1,0.07,0.06,0.05,0.04",想让 double weight[]={0.4,0.18,0.1,0.1,0.07,0.06,0.05,0.04};
- 今天下班前最后一个问题 ,也是一个英文的,请高手帮忙翻译一下。
- 现在有《JAVA编程思想》(第四版)的全中文版下载吗?
- 如何向zip添加文件
- 我写的一个测试数据库连接的函数出现missing返回值问题
- 求解数组相关算法!
- 求高手解答java内部类问题
- 初学,请指教
- 如何在窗口的标题栏显示数据库的账号(账号已好的,怎样添加)
- 阅读thinking in java遇到了一个内部类的小问题。
- string池,常规区,代码区,堆栈是什么?
Student s = p.new 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)
{
new Person("zhangsan", 20).new Student("zhangsan", 23, "hello").super(name, age, spec);
this.tutor = tutor;
} public void print()
{
System.out.println();
super.print();
System.out.println("导师是:" + tutor);
}
}
}public class Test
{ public static void main(String[] args)
{
Person pl = new Person("远见", 21);
pl.print();
Person.Student sl = new Person("zhangsan", 19).new Student("卓识", 19, "计算机");
sl.print();
Person.GraduteStudent gra = new Person("zhangsan",19).new GraduteStudent("乘势", 22, "空间物理", "蓝天野");
gra.print();
System.out.println("总人数:" + Person.count);
}}
你定义的内部类是成员内部类,生成对象的方式是
OuterClass.InnerClass inner = new OuterClass().new InnerClass();Person.GraduteStudent gra = new Person("zhangsan",19).new GraduteStudent("乘势", 22, "空间物理", "蓝天野");你生成Student对象的时候不是对了么,怎么生成GraduateStudent对象的时候就不记得了呢?
第一个错误就是你要调用内部类的构造方法得先有Student实例