使用多个类实例化2D对象数组

时间:2016-05-24 12:23:44

标签: java arrays oop object

我的作业让我碰壁了。

我创建了一个2D对象数组,我需要使用各种类来实例化。

还有很多其他要求,但我主要担心的是(目前)我无法弄清楚为什么我得到一个'c'而不是'的数组。 “

这是我的代码的一部分,包括Driver类,Item类,Dot类和Array类。

非常感谢任何帮助。

package game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class DirectionPanel extends JPanel
{

    private final int WIDTH = 1300, HEIGHT = 900;
    private final int JUMP = 10; 
    private final int IMAGE_SIZE = 31;
    private ImageIcon spaceShipImage;
    private int x, y;

    public DirectionPanel()
    {
        addKeyListener (new DirectionListener());

        x = WIDTH /2;
        y = HEIGHT /2;

        try
        {
            spaceShipImage = new ImageIcon(getClass().getResource("/2.gif"));
        }
        catch (Exception e)
        {
            System.out.println("sss");
        }
        setBackground(Color.BLACK);
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
        setFocusable(true);
    }

    public void paintComponenet (Graphics g)
    {
        super.paintComponent(g);
        spaceShipImage.paintIcon(this, g, x, y);
    }

    private class DirectionListener extends KeyAdapter
    {

      public void keyPressed (KeyEvent event)
      {

         switch (event.getKeyCode())
         {

            case KeyEvent.VK_UP:
                    System.out.println("Dsfdsf");
                    y -= JUMP;
               break;
            case KeyEvent.VK_DOWN:              
               y += JUMP;
               break;
            case KeyEvent.VK_LEFT:
               x -= JUMP;
               break;
            case KeyEvent.VK_RIGHT:
               x += JUMP;
               break;
         }

         repaint();
      }

    }
}
<p align="right"> 

<table align="right"> 

<form name="login" action="/scripts/login.cgi" method="post"> 
    public class Driver
    {

      public static void main(String[] args) 
      {
      Driver a = new Array();
      ((Array) a).runArray();
      }
   }
    public abstract class Item extends Driver
    {

     private char component;

     public Item (char c)
     {
       component = 'c';
     }


    public char display()
   {
     return component;
   }


  //create a new array of objects.
  Item[][] array = new Item [10][10];

  }

1 个答案:

答案 0 :(得分:1)

因为无论你如何调用Item的构造函数,它都将是char c

public Item (char c)
     {
       component = 'c';
     }

将其更改为

public Item (char c)
     {
       component = c;
     }