日期:2014-05-20  浏览次数:20622 次

//照书上的示例抄下来,编译错误,但我又找不出误,急急急........
//照书上的示例抄下来,编译错误,但我又找不出误,急急急........
我反复检查了好几遍也没有发现什么错误>
难道是我的JAVA解释程序有误,请各位帮我运行一下,如果在你们的机器上运行正常,
有可能是我的机器毛病了.谢谢各位!!!!!!!!!!!!!!!!!!
public   class   Person4
{
static   int   count=0;
protected   String   name;
protected   int   age;
public   Person4(String   n1,int   a1)
{
this.name=n1;
this.age=a1;
this.count++;
}
public   int   olderthen(Person4   b)
{
Person4   a=this;
return   a.age-b.age;
}
public   void   print()
{
System.out.print(this.getClass().getName()+   "   ");
System.out.print( "count= "+this.count+ "   ");
System.out.println( "   "+this.name+ ", "+this.age);  
}
class   Student4   extends   Person4
        {
protected   String   dept;
Student4(String   n1,int   a1,String   d1)
{
super(n1,a1);
dept=d1;
}
}
public   static   void   main(String   args[])
{
Person4   p1=new   Person4( "李大广 ",21);
p1.print();
Student4   s1=new   Student4( "陈小瑞 ",19, "计算机系 ");//这一行提示出错
s1.print();
System.out.println( "年龄差=   "   +   p1.olderthen(s1));
        }
}

------解决方案--------------------
public class Person4
{
static int count=0;
protected String name;
protected int age;
public Person4(String n1,int a1)
{
this.name=n1;
this.age=a1;
this.count++;
}
public int olderthen(Person4 b)
{
Person4 a=this;
return a.age-b.age;
}
public void print()
{
System.out.print(this.getClass().getName()+ " ");
System.out.print( "count= "+this.count+ " ");
System.out.println( " "+this.name+ ", "+this.age);
}

}


class Student4 extends Person4
{
protected String dept;
Student4(String n1,int a1,String d1)
{
super(n1,a1);
dept=d1;
}
}
public static void main(String args[])
{
Person4 p1=new Person4( "李大广 ",21);
p1.print();
Student4 s1=new Student4( "陈小瑞 ",19, "计算机系 ");//这一行提示出错
s1.print();
System.out.println( "年龄差= " + p1.olderthen(s1));
}
}


少了一个大括号,再试试