内部类错误

来源:百度知道 编辑:UC知道 时间:2024/06/07 05:19:34
public class shiyan1{
class df{
int a,b;
df(int a1,int b1){
a=a1;
b=b1;
}
}
class gg extends df{
float c,d;
gg(float c1,float d1){
super(0,0);
c=c1;
d=d1;
}
}
public static void main(String args[]){
gg h=new gg(2,2);
System.out.println(h.a+" "+h.b+" "+h.c+" "+h.d);
}
}

系统提示 gg h=new gg(2,2);出错,请问为什么?如何解决?

public class Yyy{
class df{
int a,b;
df(int a1,int b1){
a=a1;
b=b1;
}
}
class gg extends df{
float c,d;
gg(float c1,float d1){
super(0,0);
c=c1;
d=d1;
}
}
public static void main(String args[]){
Yyy hh=new Yyy();//内部类在static里面定义对象时,需要指明它所在的基类
Yyy.gg h=hh.new gg(2,2); //内部类对象的创建方法,仔细看
System.out.println(h.a+" "+h.b+" "+h.c+" "+h.d);
}
}

可能你多打了两个}吧,试试这个:
public class shiyan1{
class df{
int a,b;
df(int a1,int b1){
a=a1;
b=b1;
}
class gg extends df{
float c,d;
gg(float c1,float d1){
super(0,0);
c=c1;
d=d1;
}
public static void main(String args[]){
gg h=new gg(2,2);
System.out.println(h.a+" "+h.b+" "+h.c+" "+h.d);
}
}