【问题标题】:Why there is an error as "<identifier> expected" with the Derived constructor arguments?为什么派生构造函数参数出现“<identifier> expected”错误?
【发布时间】:2018-02-07 09:28:45
【问题描述】:

我已经定义了超类Base的构造函数,但是在子类Derived中声明构造函数的参数时,为什么会显示错误“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));
                }
            }

【问题讨论】:

  • 不相关:请阅读有关 java 命名约定 - 您使用 camelCase 作为方法名称,例如 viewDerivedSum()
  • 感谢复活节接受 ????

标签: java subclass superclass identifier


【解决方案1】:

这里:

Derived(x1,y1,z1)

您将 types 放在基类的参数前面,是什么让您认为现在可以省略它们?

令人惊讶的是,语法规则总是相同的,因为你需要这样的东西:

Derived(int x1, int y1, int z1)

改为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2011-02-24
    • 2020-11-15
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多