1

所以我必须创建一个程序,在其中我使用多态性和继承来创建指向左的箭头和箭头,我做到了。但是,当我创建我的主类并尝试调用方法时,例如“LeftArrow.drawHere”来绘制箭头时,我收到错误说我无法从 LeftArrow 类中对非静态字段“drawHere() 进行静态引用”。我想知道如果我不能这样做,我该如何设计我的主要方法来绘制箭头?这是我的代码,请注意有几个类,但我会将它们全部发布在这里。

import java.util.Scanner;

public class LeftArrow extends ShapeBasic
{
    private int tail, width;
    private static int inside;
    public LeftArrow()
    {
        super();
        tail = 0;
        width = 0;
    }
    public LeftArrow(int noff, int ntail, int nwidth)
    {
        super(noff);
        tail = ntail; 
        setWidth(nwidth);
    }

    public void setTail(int ntail)
    {
        tail = ntail;
    }

    public int getTail()
    {
        return tail;
    }
    public void setWidth(int nwidth)
    {
        if (nwidth % 2 == 1)
        {
            width = nwidth;
        }
        else
        {
            System.out.println(" Width must be odd");
            System.out.println("Enter a new width");
            Scanner key = new Scanner(System.in);
            int wid = key.nextInt();
            setWidth(wid);
        }
    }
    public int getWidth()
    {
        return width;
    }
    public void drawHere() 
    {
        drawTriangle();
        drawTail();
        drawBTriangle();
        //System.out.println();
    }
    public void drawTriangle()
    {
        inside = 1;
        int split = (width/2);
        skipSpaces(getOffset());
        System.out.println('*');
        for(int count = 0; count < split; count ++)
        {
            skipSpaces(getOffset() - (inside + 1));
            System.out.print('*');
            skipSpaces(inside);
            System.out.print('*');
            inside = inside + 2;
            System.out.println();
        }
    }
    public void drawTail()
    {
        skipSpaces(getOffset() - getInside() - 1);
        System.out.print('*');
        skipSpaces(getInside());
        for (int count = 0; count < tail ; count++)
        { 
            System.out.print('*');
        }
    }
    public void drawBTriangle()
    {
        int inside = getInside();
        int split = (width/2);
        System.out.println();
        for(int count = 0; count < split; count ++)
        {
            skipSpaces(getOffset() - (inside - 1));
            System.out.print('*');
            inside = inside - 2;
            skipSpaces(inside);
            System.out.print('*');
            System.out.println();
        }
        skipSpaces(getOffset());
        System.out.print('*');
    }
    public int getInside()
    {
        return inside;
    }
    private static void skipSpaces(int number)
    {
        for (int count = 0; count < number; count++)
            System.out.print(' ');
    }
    @Override
    public void setOffset(int noff) {
        // TODO Auto-generated method stub

    }
    @Override
    public int getOffset() {
        // TODO Auto-generated method stub
        return 0;
    }
}

import java.util.Scanner;

public class RightArrow extends ShapeBasic
{
    private int tail, width;
    private static int inside;
    public RightArrow()
    {
        super();
        tail = 0;
        width = 0;  
    }
    public RightArrow(int noff, int ntail, int nwidth)
    {
        super(noff);
        tail = ntail;
        setWidth(nwidth); // must be odd
    }
    public void setTail(int ntail)
    {
        tail = ntail;
    }
    public int getTail()
    {
        return tail;
    }
    public void setWidth(int nwidth)
    {
        if (nwidth % 2 == 1)
        {
            width = nwidth;
        }
        else
        {
            System.out.println(" Width must be odd");
            System.out.println("Enter a new width");
            Scanner key = new Scanner(System.in);
            int wid = key.nextInt();
            setWidth(wid);
        }
    }
    public int getWidth()
    {
        return width;
    }
    public void drawHere()
    {
        drawTriangle();
        drawTail();
        drawBTriangle();
        System.out.println();
    }
    public void drawTail()
    {
        skipSpaces(getOffset() + 1);
        for (int count = 0; count < tail ; count++)
        { 
            System.out.print('*');
        }
        skipSpaces(getInside());
        System.out.print('*');  // fix
    }
    public void drawTriangle()
    {
        inside = 1;
        int split = (width/2);
        skipSpaces(getOffset() + tail);
        System.out.println('*');
        for(int count = 0; count < split; count ++)
        {
            skipSpaces(getOffset() + tail);
            System.out.print('*');
            skipSpaces(inside);
            System.out.print('*');
            inside = inside + 2;
            System.out.println();
        }
    }
    public void drawBTriangle()
    {
        int inside = getInside();
        int split = (width/2);
        System.out.println();
        for(int count = 0; count < split; count ++)
        {
            skipSpaces(getOffset() + tail);
            System.out.print('*');
            inside = inside - 2;
            skipSpaces(inside);
            System.out.print('*');
            System.out.println();
        }
        skipSpaces(getOffset() + tail);
        System.out.print('*');
    }
    public int getInside()
    {
        return inside;
    }
    private static void skipSpaces(int number)
    {
        for (int count = 0; count < number; count++)
            System.out.print(' ');
    }
}

public abstract class ShapeBase implements ShapeInterface {
    private int offset;
    public abstract void drawHere();


    public void drawAt(int lineNumber) {
        for (int count = 0; count < lineNumber; count++)
            System.out.println();
        drawHere();
    }
}

public class ShapeBasic implements ShapeInterface
{

    private int offset;

    public ShapeBasic()
    {
        offset = 0;
    }
    public ShapeBasic( int noff)
    {
        offset = noff;
    }
    public void setOffset(int noff) 
    {
        offset = noff;
    }

    public int getOffset() 
    {
        return offset;
    }

    public void drawAt(int linnumber) 
    {
        for (int count = 0; count < linnumber; count++)
            System.out.println();
        drawHere();
    }

    public void drawHere() 
    {
        for (int count = 0; count < offset; count++)
            System.out.print(' ');
        System.out.println('*');
    }

}

public interface ShapeInterface 
{
    public void setOffset(int noff);    // set how many space from left

    public int getOffset(); // returns offset
    public void drawAt(int linnumber);  // moves down equal to line number Ex. 5 = 5 down
    public void drawHere(); // draws shape after moving left equal to offset
}
4

3 回答 3

1

我想看看你的主要方法,但我没有声誉。猜测一下,我会说也许你实际上并没有在你的 main 方法中实例化这些类的对象,而是试图从它们中调用方法。例如,如果您这样做:

LeftArrow lArrow = new LeftArrow();
lArrow.drawHere();

它应该工作。

于 2014-11-11T16:43:51.440 回答
1

方法 drawHere 被声明为非静态的。而不是 LeftArrow.drawHere() 你应该打电话

  LeftArrow left = new LefArrow(a, b, c);
  left.drawHere();
于 2014-11-11T16:44:29.497 回答
0

主要是静态的。它需要先创建 LeftArrow 的实例,然后才能使用它们。像这样的东西:

LeftArrow myArrow = new LeftArrow();
myArrow.drawHere();
于 2014-11-11T16:43:13.577 回答