2个不同的继承类覆盖

时间:2015-05-07 19:07:42

标签: java multidimensional-array

我必须处理ANSI项目,它需要一些2D数组。我有一个基类的派生类(基类是2D-Object数组)。当我尝试将较小(形状)数组添加到较大(地图)数组中时,它会覆盖较大数组的维度。

代码:

基类

try-except

较小网格

public class pixelGrid
{
    private static pixelCell[][] bitMap;
    private static pixelCell defaultCell;

    public pixelGrid()
    {
        defaultCell = new pixelCell();
    }

    public pixelGrid(int width, int height)
    {
        bitMap = new pixelCell[width][height];
        defaultCell = new pixelCell();
    }

    public static void setDimension(int width, int height)
    {
        bitMap = new pixelCell[width][height];
    }

    //...
}

更大的网格

import java.util.Scanner;
import java.io.*;

public class shape extends pixelGrid
{
public String name;
private static boolean hasDimension;
private static boolean hasDefaultCell;
public shape(String n)
{
    name = n;
    hasDimension = false;
    hasDefaultCell = false;
    draw(); 
}

public void draw()
{
    try(Scanner parser = new Scanner(new File (name +".cmd")))
    {
        String commandToken = parser.nextLine();
        System.out.println("Line(shape): " + commandToken);
        while(commandToken.charAt(0) != 'q')
        {
            Scanner sc = new Scanner(commandToken);
            String commandLine = sc.next();
            System.out.println("command(shape):" + commandLine);
            if(commandLine.equals("d") && !hasDimension)
            {
                setDimension(commandToken);
                hasDimension = true;
            }
            else if(commandLine.equals("b") && !hasDefaultCell)
            {
                setDefaultCell(commandToken);
                hasDefaultCell = true;
            }
            else if(hasDimension && hasDefaultCell)
            {
                char command = commandToken.charAt(0);
                switch(command)
                {
                    case 'l':
                    drawLine(commandToken);
                    break;
                    case 'c':
                    drawCircle(commandToken);
                    break;
                    case 'p':
                    drawPoint(commandToken);
                    break;
                    case 'e':
                    erase(commandToken);
                    break;
                    case 'x':
                    ANSI.cls();
                    break;
                    default:
                    commandToken = parser.nextLine();
                    break;
                }
            }
            commandToken = parser.nextLine();
        }
    }
    catch(IOException e)
    {
        System.out.println( "\nError reading input file(from shape): " + name);
        e.printStackTrace();
    }
}

    private static void setDefaultCell(String command)
    {
        Scanner parser = new Scanner(command);
        parser.next();
        String token = parser.next();
        char c;
        if(token.equals("space"))
        {
            c = ' ';
        }
        else
        {
            c = token.charAt(0);
        }
        int fg = ANSI.color2Int(parser.next());
        int bg = ANSI.color2Int(parser.next());
        parser.close();
        pixelCell cell = new pixelCell(c,fg,bg);
        setDefaultCell(cell);
        clearMap(getDefaultCell());
        System.out.println("Default Cell: " + getDefaultCell());
    }

    private static void setDimension(String command)
    {
        Scanner parser = new Scanner(command);
        System.out.println("command(shape): " + command);
        parser.next();
        int w = parser.nextInt();
        int h = parser.nextInt();
        setDimension(w,h);
        System.out.println("Dimensions: " + getWidth() + " x " + getHeight());
    }

//...

}

调试结果:

  

尺寸(地图> pre-shape-proc):200 x 100

     

线(形状):d 10 10

     

命令(形状):d

     

命令(形状):d 10 10

     

尺寸:10 x 10

     

尺寸(地图>形状创建后):10 x 10

修改

这是我的主要方法:

主要

public class bitMap extends pixelGrid
{

    public bitMap(int width, int height)
    {
        super(width, height);
    }

    public void addShape(int x, int y, shape s)
    {
        int shapeX = 0;
        int shapeY = 0;
        System.out.println("Dimensions(map): " + getWidth() + " x " + getHeight());
        System.out.println("Dim(shape): " + x + " , " + y + " " + s.getWidth() + " x " + s.getHeight());
        for(int w = x; w < (x + s.getWidth()); w++)
        {
            for(int h = y; h < (y + s.getHeight()); h++)
            {
                setCell(w,h,s.getCell(shapeX,shapeY));
                shapeY++;
            }
            shapeX++;
            shapeY = 0;
        }
    }
    //...
}

1 个答案:

答案 0 :(得分:0)

问题是你的pixelGrid构造函数重新初始化静态属性defaultCell,因此类的第二个实例化(或其派生类之一)&#34;重置&#34;所有价值观。

public class pixelGrid
{
  private static pixelCell defaultCell;

  public pixelGrid()
  {
    // resets defaultCell
    defaultCell = new pixelCell();
  }
}

相反,您应该直接初始化static属性:

private static pixelCell defaultCell = new pixelCell();

或者使用静态初始化块(主要是因为除了new之外还有更多要做的事情)

private static pixelCell defaultCell;

static {
  defaultCell = new pixelCell();
}

(如果每个派生类需要static属性一次,则必须单独在派生类中声明它。)

请参阅https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html