1

这是我的简单java代码。当我编译/运行程序时,Eclipse IDE 显示语法错误。语法错误对我没有任何意义

class A {
    int x;
    int z;
    int s;

    A(int a,int b) {
        x=a;
        z=b;
    }

    void display() {
        System.out.println("x+y :"+(x+z));
    }
}

class B extends A
{
    B(int a, int b, int c) {
        x=a;
        z=b;
        s=c;
    }

    void display() {
        System.out.print("In B class...");
        System.out.println("x+y+s :"+(x+z+s));
    }
}

public class Simple {
    public static void main(String[] args) {
        A ob=new A(10, 20);
        B ob2=new B(20, 30, 40);
        ob.display();
        ob2.display();
    }
}
4

8 回答 8

3

在您的类 A 中,您提供了一个接受两个参数的构造函数,并且您没有为 A 定义无参数构造函数。因此,当您尝试实例化扩展 A 的B 时,它会失败,因为它无法调用A()

有两种方法可以解决这个问题:

  • 为 A 提供无参数构造函数

就像是:

class A{
    int x;
    int z;
    int s;

    public A(){
    }

    public A(int a,int b){
        x=a;
        z=b;
    }

    void display(){
        System.out.println("x+y :"+(x+z));
    }
}
  • 调用 super(a, b) 作为 B 的构造函数中的第一条语句。

例如:

class B extends A
{
    B(int a, int b, int c){
        super(a,b);
        x=a;
        z=b;
        s=c;
    }

    void display(){
        System.out.print("In B class...");
        System.out.println("x+y+s :"+(x+z+s));
    }
}

如果您是 Java 新手,您可能想阅读Java 中的继承创建对象

于 2012-09-22T06:21:14.863 回答
1

这里有几点需要考虑:-

  • 当您声明一个没有任何构造函数的类时,编译器会通过插入一个默认构造函数来为您执行此操作,即一个空的零参数构造函数。
  • 如果您明确声明了单参数构造函数(或任何其他构造函数),编译器不会添加任何默认构造函数。
  • 每次创建类的实例时,都会在继承层次结构中自上而下调用构造函数。所以,如果你不使用继承,那么实例化一个类首先会调用 Object 类的构造函数(这是任何继承或非继承层次结构的顶级类)
  • 我们使用 super() 调用超类构造函数,但如果我们没有显式完成它,编译器会为我们添加这个。如果你自己添加它,请确保它应该是构造函数的第一条语句。您可以将参数传递给 super,以调用超类的非零参数构造函数,但编译器仅添加零参数 super()。

现在,说了这么多,让我们转到您的代码。在您的类 A 中,您已经声明了一个三参数构造函数,因此编译器不会添加任何内容。所以,恰恰是您没有任何零参数构造函数那里。现在,您的类 B 扩展了 A,因此实例化该类将调用超类构造函数,此处为 A。现在,由于您没有在 B 类中添加任何 super() 调用,编译器将自动添加。但是,编译器添加的是:super(),它将调用 A 的零参数构造函数,我们看到你没有。

那么,你怎么能解决这个问题?

将默认构造函数添加到您的 A 类:-

class A {
    public A() {
    }
}

或者在 B 的构造函数中添加显式 super() 调用作为您的第一条语句,以调用您的 3-arg A 的构造函数:-

class B extends A {
    public B(int a, int b) {
          super(a, b, 19);
          /* More Code */
    }
}

与 super() 类似,您也可以在构造函数中使用 this() 来调用同一个类的构造函数。同样的规则适用于 this().. 它应该是构造函数中的第一条语句..

因此,我们可以看到我们可以在构造函数中使用 super() 或 this()。

我希望这么多信息至少可以解决您当前的问题。

于 2012-09-22T15:13:47.613 回答
0

如果要将值设置为超类中定义的属性,请在子类构造函数中调用 super() 方法。

因此,您的子类构造函数应如下所示

public B(int a, int b, int c){
    // The following statement will automatically sets the values to the base class             properties (which are already derived into derived class).
    super(a, b);
    // No Need of setting the values to the super class properties... The above statement will automatically sets the values...
    s=c;
}
于 2012-09-22T07:41:34.903 回答
0

您的超类有两个 args 构造函数,当您在 B 类的构造函数中扩展类 A 时,您已经调用了超类的两个 args 构造函数。将您的子类构造函数代码更改为此。

B(int a, int b, intc) {
   super(a,b); //this has to be in the first line inside the constructor.
    //do your things
  }

这个链接解释了它

于 2012-09-22T06:40:29.363 回答
0

类a定义“A类”在哪里?数据字段定义在哪里(B 类的 x、z、s 和 A 类的 x、z)?请编辑您的帖子

帖子编辑后,我尝试编译它,错误应该如下:

Simple.java:17:找不到符号符号:构造函数 A() 位置:类 A B(int a, int b, int c){ ^ 1 错误

这意味着您应该在创建类 B 时调用类 A 的某个构造函数,因为 B 扩展了 A。您没有默认构造函数,因为您创建了自己的构造函数。

像这样重写 B 的构造函数,你会没事的:

B(int a, int b, int c){
    super(a,b);
    s=c;
}

祝你好运!

于 2012-09-22T06:16:32.770 回答
0

(缺乏)格式使其难以阅读。类 A 定义的开始在代码块之外。

看起来您没有在 B 类中定义 int 字段。应该是:

 class B extends A
 {
     int s;

  ...
  }

编辑:看起来你在我发布答案时重新格式化并更改了它......

于 2012-09-22T06:20:28.740 回答
0

您已经明确声明了 A 类的构造函数,因此 B 类应该明确调用其父级的 - 类 A - 构造函数。

将以下代码添加到 B 类的构造函数中:

super(a, b);

然后您的 B 类构造函数将如下所示:

B(int a, int b, int c) {
    super(a, b);
    x = a;
    z = b;
    s = c;
}

PS,请注意您粘贴的代码的格式。很难阅读。

于 2012-09-22T06:25:35.367 回答
-1

首先,发布 eclipse 给出的错误会很有帮助。

但无论如何,您的语法错误源于您没有定义变量的数据类型,例如 x、z、s 等。

例如,

int x = a;
int z = b;

等等

于 2012-09-22T06:18:11.647 回答