为什么 Derived 构造函数参数出现“[=10=] 预期”错误?
Why there is an error as "<identifier> expected" with the Derived constructor arguments?
我已经定义了一个超classBase
的构造函数,但是在子classDerived
中声明构造函数的参数时,为什么显示错误 "identifier expected"?
class Base
{
int x,y;
Base(int x1,int y1)
{
x=x1;
y=y1;
}
void viewxy()
{
System.out.println("x = "+x+" y= "+y);
}
void viewsum()
{
System.out.println("x+y: "+(x+y));
}
}
class Derived extends Base
{
int z;
Derived(x1,y1,z1)
{
super(x1,y1);
z=z1;
}
void viewz()
{
System.out.println("z = "+z);
}
void viewderivedsum()
{
System.out.println("x+y+z= "+(x+y+z));
}
}
这里:
Derived(x1,y1,z1)
你把 types 放在参数前面 class,是什么让你认为你现在可以省略它们?
令人惊讶的是,语法规则总是相同的,因为你需要这样的东西:
Derived(int x1, int y1, int z1)
相反。
我已经定义了一个超classBase
的构造函数,但是在子classDerived
中声明构造函数的参数时,为什么显示错误 "identifier expected"?
class Base
{
int x,y;
Base(int x1,int y1)
{
x=x1;
y=y1;
}
void viewxy()
{
System.out.println("x = "+x+" y= "+y);
}
void viewsum()
{
System.out.println("x+y: "+(x+y));
}
}
class Derived extends Base
{
int z;
Derived(x1,y1,z1)
{
super(x1,y1);
z=z1;
}
void viewz()
{
System.out.println("z = "+z);
}
void viewderivedsum()
{
System.out.println("x+y+z= "+(x+y+z));
}
}
这里:
Derived(x1,y1,z1)
你把 types 放在参数前面 class,是什么让你认为你现在可以省略它们?
令人惊讶的是,语法规则总是相同的,因为你需要这样的东西:
Derived(int x1, int y1, int z1)
相反。