试图从另一个类调用方法

时间:2014-10-29 03:19:36

标签: java

大家好,我是新来的,我是Java的新手。我正在编写一个玩战争游戏的程序,并在图形面板上显示游戏。我'有问题从War类中的Card类调用draw方法。例如,当尝试使用drawSpade时,我得到错误"无法对类型Card"中的非静态方法drawSpade(Graphics,int,int)进行静态引用。我知道有很多关于这个错误的帖子,但是我很难理解为什么它在我的程序中发生以及如何解决它。任何帮助或建议都会很棒。谢谢!

    public class War extends JFrame
    {

    private int card;
    private int drawNum;
    public int drawSuit;
    private final int xA = 225;
    private final int yA = 50;


    public War()
    {
        Color tableGreen = new Color(50, 150, 50);
        setBackground(tableGreen);      // Sets background color
        setSize(800,500);               // Sets window size
        setTitle("War");                // Sets window title
        setLocation(50,50);             // Sets where the window appears
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);               // Makes the window appear
    }

    int myCard()
    {
        Random myCard = new Random();
        return myCard.nextInt(13) + 1;
    }
    int mySuit()
    {
        Random mySuit = new Random();
        return mySuit.nextInt(4) + 1;
    }

    int compCard()
    {
        Random compCard = new Random();
        return compCard.nextInt(13) + 1;
    }
    int compSuit()
    {
        Random compSuit = new Random();
        return compSuit.nextInt(4) + 1;
    }


    public void cardNum()
    {
        if (myCard() == 1)
        {
            drawNum = 1;
        }
    }
    public void cardSuit()
    {
        if (mySuit() == 1)
        {
            drawSuit = 1;
        }
    }

    public void paint (Graphics pane)
    {
        drawSuit = 1;
        drawNum = 1;

        if (drawNum == 1)
            Card.drawBlank(pane, xA, yA);
            Card.drawTwo(pane, xA, yA);

        if (drawSuit == 1)
            Card.drawSpade(pane, 75, 100);

    }

     public int getDrawSuit() 
     {
         return drawSuit;
     }

}


    public class Card 
    {
    private static final int HEIGHT = 200;
    private static final int WIDTH = 150; 
    private final int xA = 225;
    private final int yA = 50;


    public void drawBlank (Graphics pane, int xA, int yA)
    {
        pane.setColor(Color.black); 
        pane.drawRoundRect(xA, yA, WIDTH, HEIGHT, 20, 20);
        pane.setColor(Color.white);
        pane.fillRoundRect(xA + 1, yA + 1, WIDTH - 1, HEIGHT - 1, 20, 20);
    }
    public void drawSpade (Graphics pane, int xA, int yA)
    {
        pane.setColor(Color.black);
        Polygon triangle;
        triangle = new Polygon();
        triangle.addPoint(xA + WIDTH + WIDTH/2, yA + HEIGHT/5);
        triangle.addPoint(xA + WIDTH + WIDTH/2 + 5, yA + HEIGHT/3);
        triangle.addPoint(xA + WIDTH + WIDTH/2 - 5, yA + HEIGHT/3);
        pane.fillPolygon(triangle);
        pane.drawOval(xA + WIDTH + WIDTH/2, yA + HEIGHT/5, 20, 20);
        pane.fillOval(xA + WIDTH + WIDTH/2, yA + HEIGHT/5, 20, 20);
        pane.drawOval(xA + WIDTH + WIDTH/2 - 20, yA + HEIGHT/5, 20, 20);
        pane.fillOval(xA + WIDTH + WIDTH/2 - 20, yA + HEIGHT/5, 20, 20);
        pane.drawOval(xA + WIDTH + WIDTH/2 - 10, yA + HEIGHT/5 - 15, 20, 20);
        pane.fillOval(xA + WIDTH + WIDTH/2 - 10, yA + HEIGHT/5 - 15, 20, 20);
    }  
    /**
     * Numbers
     */
    public void drawTwo (Graphics pane, int xA, int yA)
    {
        if (getDrawSuit() == 1 || getDrawSuit() == 2)
            pane.setColor(Color.black);
        else
            pane.setColor(Color.red);

            pane.drawString("2", xA, yA);
    }

     public int getDrawSuit() 
     {
         return getDrawSuit();
     }
}

3 个答案:

答案 0 :(得分:0)

您正在调用Card.drawSpade这是类Card的静态方法,但它不存在。

drawSpade被声明为实例方法:public void drawSpade (Graphics pane, int xA, int yA)。如果您希望它是一个类方法,您需要声明为静态。实际上,您需要Card的实例,例如

Card c = new Card(); ... c.drawSpade(pane, 75, 100);

答案 1 :(得分:0)

在您的示例中,drawSpade被定义为非静态,这意味着您需要一个卡的INSTANCE才能调用它:

 private Card c = new Card();
 c.drawSpade(<arguments>);

如果要将其称为静态方法,请使用static关键字声明它:

public static void drawSpade (Graphics pane, int xA, int yA)
{
  ...
}

然后您可以这样称呼它:

Card.drawSpade(<arguments>);

您是否需要其中一个取决于您使用该方法的内容。静态方法不保持卡的状态,其中一个实例将具有一个状态,并且可用于保持游戏中每张卡的状态(通过为游戏中的每张卡创建一个实例)。

答案 2 :(得分:0)

对于这个例子,我们需要知道静态方法和非静态方法(aka实例方法)之间的区别。当我们创建一个静态方法时,基本上说这个方法现在属于该类,并且该方法不需要访问对象状态。

所以我们可以只使用类名,然后调用方法。

 Card.drawSpade(pane, 75, 100);

但是,由于drawSpade是和实例方法,这意味着我们需要创建该类的实例,以便能够调用此方法。我们通过创建从该类生成的对象来完成此操作。

Card myCard = new Card();
myCard.drawSpade(pane,75,100);