简体   繁体   English

构造函数-找不到符号

[英]Constructor - cannot find symbol

I am trying to compile a program, which has a subclass DramaticGame , of Game. 我正在尝试编译一个具有Game子类DramaticGame的程序。 The subclass contains the following: 子类包含以下内容:

public class DramaticGame extends Game
{
  public DramaticGame(String machineName, int machineSize, String rackName, int rackSize)
  {
    super.makeMachine(machineName, machineSize);
    super.makeRack(rackName, rackSize);
  }
}

DramaticGame.java:5: cannot find symbol DramaticGame.java:5:找不到符号

symbol : constructor Game() 符号:构造函数Game()

location: class Game 位置:类游戏

{ {

^ ^

It says it cannot find the "{" symbol in Game, but i am not sure how to fix this. 它说它在游戏中找不到“ {”符号,但是我不确定如何解决。

public Game(String paramString1, int paramInt1, String paramString2, int paramInt2)
{
  this.machine = makeMachine(paramString1, paramInt1);
  this.rack = makeRack(paramString2, paramInt2);
}

Your superclass is missing default constructor which is invoked implicitly if you do not issue 您的超类缺少默认构造函数,如果您不发出,则将隐式调用该构造函数

super(eventual params);

as first statement in your constructor. 作为构造函数中的第一条语句。

No, what it can't find is a default constructor. 不,找不到默认构造函数。

The compiler will create a no-argument default constructor if you don't supply any constructors. 如果您不提供任何构造函数,则编译器将创建一个无参数的默认构造函数。

But the moment you write one you're on your own. 但是,一旦您写了一个,就可以靠自己了。

I'd advise that you write one that calls the one you've already written with default arguments: 我建议您编写一个使用默认参数调用已编写的代码:

public class DramaticGame extends Game
{
  public DramaticGame() {
      this("default-machine-name", 1024, "default-rack-name", 10); 
  }

  public DramaticGame(String machineName, int machineSize, String rackName, int rackSize)
  {
    super.makeMachine(machineName, machineSize);
    super.makeRack(rackName, rackSize);
  }
}

I'd also wonder if your Game constructor should take rack name and size. 我也想知道您的Game构造函数是否应使用机架名称和大小。 The fact that those are members of the Game class suggests to me that they should be set in the Game constructor. 这些是Game类的成员这一事实向我表明,应该在Game构造函数中设置它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM