【问题标题】:"Implicit super constructor Block() is undefined"“隐式超级构造函数 Block() 未定义”
【发布时间】:2013-09-20 14:17:17
【问题描述】:

我正在创建一个游戏。当我扩展我的 Block 类时,它显示一个错误。

错误: 隐式超级构造函数 Block() 未定义。必须显式调用另一个构造函数

代码:

Game.java:

package lt.projecturanium;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.util.HashMap;

import javax.swing.JFrame;

import lt.projecturanium.blocks.Block;
import lt.projecturanium.blocks.BlockRectangle;
import lt.projecturanium.entity.Player;
@SuppressWarnings("unused")

public class Game extends Canvas implements Runnable{

    private static final long serialVersionUID = 1L;

    private static JFrame _frame;
    public static Game _instance;

    private static final String TITLE = "Project Uranium";
    private static final int WIDTH = 650;
    private static final int HEIGHT = WIDTH * 3 / 4;

    private static final int UPDATE_RATE = 50;
    private static final int RENDER_RATE = 100;

    public static HashMap<Block, Coordinates> blocks = new HashMap<Block, Coordinates>();

    public int rectx = 0;
    public int recty = 0;
    public int rectID = 0;

    public boolean hitted = false;

    public float interpolation;

    public static final Dimension SIZE = new Dimension(WIDTH, HEIGHT);

    private Thread _thread;

    private boolean _running;

    private int _totalTicks = 0;
    private int _tps = 0;
    private int _fps = 0;

    public Game()
    {
        _instance = this;
        setPreferredSize(SIZE);
        setMinimumSize(SIZE);
        setMaximumSize(SIZE);

        _frame = new JFrame(TITLE);

        _frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        _frame.setLayout(new BorderLayout());
        _frame.add(_instance, BorderLayout.CENTER);
        _frame.pack();

        _frame.setResizable(false);
        _frame.setLocationRelativeTo(null);
        _frame.setVisible(true);
        createBufferStrategy(2);
        blocks.put(new Block(new BlockRectangle(200)), new Coordinates(30, 50));
    }
    public synchronized void start()
    {
        _running = true;
        _thread = new Thread(this, TITLE+"_main");
        _thread.start();
    }
    public synchronized void stop()
    {
        _running = false;
        if (_thread != null)
        {
            try {
                _thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void paint(Graphics g) {
        super.paint(g);  // fixes the immediate problem.
        Graphics2D g2 = (Graphics2D) g;
        g2.drawString("FPS: " + _fps + "\n TPS: " + _tps, 10, 10);
        if (hitted)
        {
            recty = 0;
            rectx += 21;
            rectID++;
            blocks.put(new Block(new BlockRectangle(rectID)), new Coordinates(rectx, recty));
            hitted = false;
        }
        recty++;
        g2.drawImage(Player.getTexture(), 60, 60, null);
        g2.drawRect(rectx, recty, 20, 20);
        g2.setColor(new Color(101, 67, 33));
        g2.fillRect(0, 430, getWidth(), getHeight());
        g2.setColor(new Color(0, 100, 0));
        g2.fillRect(0, 420, getWidth(), 10);
        g2.setColor(Color.BLACK);
        if (recty == (419 - 20))
        {   
            hitted = true;
        }
    }
    public void run() {
        double lastUpdateTime = System.nanoTime();
        double lastRenderTime = lastUpdateTime;
        final int ns = 1000000000;
        final double nsPerUpdate = (double) ns / UPDATE_RATE;
        final double nsPerRender = (double) ns / RENDER_RATE;
        final int maxUpdatesBeforeRender = 5;

        int lastSecond = (int) (lastUpdateTime / ns);
        int tickCount = 0;
        int renderCount = 0;
        while (_running) {

          long currTime = System.nanoTime();
          int tps = 0;

          while ((currTime - lastUpdateTime) > nsPerUpdate && tps < maxUpdatesBeforeRender) {
            update();
            tickCount++;
            _totalTicks++;
            tps++;
            lastUpdateTime += nsPerUpdate;
            interpolation = Math.min(1.0F, (float) ((currTime - lastUpdateTime) / nsPerUpdate));
            render(interpolation, getGraphics());
          }

          if (currTime - lastUpdateTime > nsPerUpdate) {
            lastUpdateTime = currTime - nsPerUpdate;
          }
          if (currTime - lastRenderTime == maxUpdatesBeforeRender + 1)
          {
              render(interpolation, getGraphics());
          }
          renderCount++;
          lastRenderTime = currTime;

          int currSecond = (int) (lastUpdateTime / ns);
          if (currSecond > lastSecond) {
            _tps = tickCount;
            _fps = renderCount;
            tickCount = 0;
            renderCount = 0;
            lastSecond = currSecond;
            _frame.setTitle(TITLE + " | TPS: " + _tps + " | FPS: "+ _fps);

          }

          while (currTime - lastRenderTime < nsPerRender && currTime - lastUpdateTime < nsPerUpdate) {
            Thread.yield();
            try {
              Thread.sleep(1);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            currTime = System.nanoTime();
          }
        }   
      }
    public void update()
    {
        _frame.pack();
    }
    public void render(float interp, Graphics g)
    {
        BufferStrategy myStrategy = getBufferStrategy(); 
        Graphics gra = myStrategy.getDrawGraphics();
        paint(gra);
        g.dispose();
        myStrategy.show();
        //System.out.println("Grass x: " + blocks.get("grass").getX() + " y: " + blocks.get("grass").getY());
        System.out.println("Stone x: " + blocks.get(new Block(new BlockRectangle(rectID))).getX() + " y: " + blocks.get(new Block(new BlockRectangle(rectID))).getY());
    }
}

Block.java:

package lt.projecturanium.blocks;

public class Block {
    private Block block;
    public Block (Block block){
        this.block = this;
    }
    public Block getBlock() {
        return block;
    }
    public Block getBlockById(int id)
    {
        return block;
    }
}

BlockRectangle.java:

package lt.projecturanium.blocks;

import java.awt.Image;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import lt.projecturanium.Game;

public class BlockRectangle extends Block{
    private int id;
    private static HashMap<Integer, BlockRectangle> rects = new HashMap<Integer, BlockRectangle>();
    public BlockRectangle(int id)
    {
        this.id = id;
    }
    public int getID()
    {
        return this.id;
    }
    public static BlockRectangle getByID(int id)
    {
        return rects.get(id);
    }
    public static Image getTexture()
    {
        try{        
            return ImageIO.read(Game._instance.getClass().getClassLoader().getResource("../res/player.png"));   
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

坐标.java:

package lt.projecturanium;

public class Coordinates {
    private int x;
    private int y;
    public Coordinates(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public int getX()
    {
        return this.x;
    }
    public int getY()
    {
        return this.y;
    }
}

【问题讨论】:

  • 好吧,有点跑题了。现在在 Eclipse 中调试时出现错误。文件:Game.java 行:186

标签: java super


【解决方案1】:
public class BlockRectangle extends Block

BlockRectangle 扩展了 Block。所以在BlockRectangle的构造函数中,首先要通过super()调用Block的构造函数。

public BlockRectangle(int id){
    //This line is optional if Block has an empty constructor.
    super([...]);
    this.id = id;
}

如果 Block 有一个空的构造函数,它就会被隐式调用。事实并非如此。因此,您必须自己显式调用 super()(或定义一个空的构造函数)。

注意:如果 Block 根本没有构造函数,则会隐式创建一个空的构造函数。

【讨论】:

    【解决方案2】:

    BlockRectangle(...) 需要调用super(null) 因为隐式(=未写在代码中) super() (Block()) 不存在。

    【讨论】:

      【解决方案3】:

      默认构造函数仅在您没有为该类定义任何其他构造函数时才存在。如果没有手动调用其他构造函数,则子类的构造函数会静默调用 super(),这意味着它们会尝试调用默认构造函数。

      你可以通过自己在Block类中创建一个无参构造函数来解决这个问题,或者在每个子类中通过super调用已有的构造函数。

      【讨论】:

        【解决方案4】:

        由于您定义了构造函数,Java 不再提供默认构造函数。因此,您必须自己定义一个空构造函数:

        public Block()
        {
        }
        

        另外,我不确定您为什么要保留对自己的 Block 的引用。鉴于您在 Block 类上拥有的方法,看起来您可能想了解有关继承的更多信息。我认为你在滥用它。

        【讨论】:

        • @ZygimantasAnon,你可以看看我的回答,看看它是否能解决你的疑问。
        • 我今年 11 岁,所以我可能错了。我 4 年前开始编程。
        • 没关系,我只是想帮忙。很高兴看到那里有年轻的程序员:)
        【解决方案5】:

        你得到这个错误是因为构造函数

        public BlockRectangle(int id)
            {
                this.id = id;
            }
        

        隐式调用应该是Block(){}super无参数构造函数

        由于您为类明确定义了构造函数,编译器不会提供默认的无参数构造函数。所以你应该手动创建它。

        将无参数构造函数 Block(){} 添加到您的 Block 类中,它将解决您的问题。

        【讨论】:

          【解决方案6】:

          只要您没有显式定义另一个 Java 类,就有一个隐式的默认构造函数。这就是您在 Block 课程中所做的。

          现在,如果您有一个带有构造函数的子类,则隐式调用超类的默认构造函数。

          public BlockRectangle(int id)
          {
              // Tries to call super() implictly which doesn't exist anymore
              this.id = id;
          }
          

          您可以通过在超类中显式定义一个额外的默认构造函数来解决此问题

          public Block()
          {
          }
          

          或通过显式调用非默认超类构造函数

          public BlockRectangle(int id)
          {
              super(new Block());
              this.id = id;
          }
          

          关于另一个话题:Block 中的构造函数真的没有任何意义:

          public Block (Block block){
              this.block = this;
          }
          

          您忽略了参数并将您的字段设置为this。这是没有意义的,因为 this 引用在类中始终可用。

          【讨论】:

          • 好吧,有点跑题了。现在在 Eclipse 中调试时出现错误。文件:Game.java 行:186
          • 什么错误?以及具体哪条线路?它在这里不显示行号 ;-)
          • 它可以 :) System.out.println("Stone x: " + blocks.get(new Block(new BlockRectangle(rectID))).getX() + " y: " + blocks。 get(new Block(new BlockRectangle(rectID))).getY());
          • 是的,我认为这样更好。可能是真实的,但可能是一个全新的错误。
          猜你喜欢
          • 2014-06-26
          • 1970-01-01
          • 1970-01-01
          • 2010-11-14
          • 1970-01-01
          • 2020-06-05
          • 2013-09-28
          • 1970-01-01
          • 2013-02-24
          相关资源
          最近更新 更多