2014-11-11 3 views
1

그래서 왼쪽과 화살표를 가리키는 화살표를 만들기 위해 다형성과 상속을 사용하는 프로그램을 만들어야합니다. 그러나, 내 주 클래스를 만들고 메서드를 호출하려고 할 때, 예를 들어 "LeftArrow.drawHere"화살표를 그리기 위해 정적 필드가 아닌 정적 참조를 만들 수 없다는 오류가 발생합니다 "drawHere() 클래스 LeftArrow ". 그때 내가 어떻게 할 수 없는지 궁금해서 내 주된 방법을 디자인해서 어떻게 화살을 그릴 수 있을까? 여기 내 코드입니다. 몇 가지 클래스가 있지만 여기에 모든 클래스를 게시합니다.자바 오류를 고치는 방법?

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 
} 
+0

도움을 주셔서 감사합니다. 귀하의 의견을 upvote 하겠지만 충분한 평판 포인트가 없습니다. – Rocketsm46

답변

0

메인은 정적입니다. LeftArrow를 사용하기 전에 LeftArrow의 인스턴스를 만들어야합니다. 이 같은 것 :

LeftArrow myArrow = new LeftArrow(); 
myArrow.drawHere(); 
+0

도움을 주셔서 감사합니다. – Rocketsm46

1

나는 당신의 주된 방법을 보길 원하지만 나는 명성이 없다. 짐작할 수 있겠지만, 당신이 실제로 당신의 메인 메소드에서이 클래스의 객체를 실제로 인스턴스화하지는 않지만, 그것들로부터 메소드를 호출하려고하고 있다고 말할 수 있습니다. 예를 들어, 다음과 같이하면 :

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

그럴 수 있습니다.

+0

도움에 감사드립니다. – Rocketsm46

1

메소드 drawHere는 비 정적으로 선언됩니다. LeftArrow.drawHere() 대신

LeftArrow left = new LefArrow(a, b, c); 
    left.drawHere(); 
+0

감사합니다. – Rocketsm46