如何从 Java 中的另一个类调用方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36389546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:22:37  来源:igfitidea点击:

How to Call a Method From Another Class in Java?

javaclassmethods

提问by Jainam Patel

I have a Quick Quesiton and I need Help. My Question is that I have created 2 classes where I want to call a method "IsWin" to my Panel Class.

我有一个快速问题,我需要帮助。我的问题是我创建了 2 个类,我想在其中向我的面板类调用方法“IsWin”。

This is the method That I have:

这是我拥有的方法:

Class Name: IsWin

班级名称:IsWin

    public class IsWin { 

 private JButton[][] slots;
 private Color playerColor = Color.red;   

 public boolean Winner(int column, int row) {

        boolean result = false;

        // horizontal      
        boolean found = false;
        int counter = 0;

        for ( int i = 0; i < slots.length; i++ ) {        
          if ( slots[i][row].getBackground().equals(playerColor)) { 

            counter++;

            // win 
            if ( found == true ) {            
              if ( counter == 4 ) {               
                result = true;
                break;
              }
            }
            else {            
              found = true;
            }
          }

          // reset counter
          else {          
            if ( found == true ) {            
              counter = 0;
            }          
            found = false;
          }
        }

This is the Class where I want to Call the Method

这是我要调用方法的类

This is my Panel Class: connectFourPanel

这是我的面板类:connectFourPanel

    private class clikMeButtonListener implements ActionListener{
 public void actionPerformed(ActionEvent event) {      

   if (event.getSource() == clickMeOne 
         || event.getSource() == clickMeTwo
         || event.getSource() == clickMeThree
         || event.getSource() == clickMeFour
         || event.getSource() == clickMeFive
         || event.getSource() == clickMeSix
         || event.getSource() == clickMeSeven
      ) {

     int lastEmptyIdx = -1;

     for ( int i = 0; i < slots[column].length; i++ ) {          
       if ( slots[column][i].getBackground() != Color.white ) {

         break;
       }
       else {            
         lastEmptyIdx = i;
       }
     }

     if ( lastEmptyIdx != -1 ) {          
       slots[column][lastEmptyIdx].setBackground(playerColor);

       if ( IsWin.contains(column, lastEmptyIdx) ) {

         String message = playerColor == Color.red ? " Player One Won!" : " Player Two Won!";
         JOptionPane.showMessageDialog(null, message, " Results ", JOptionPane.INFORMATION_MESSAGE);

         System.exit(0);            
       }

       else {     
         playerColor = playerColor == Color.red ? Color.yellow : Color.red; 

         // assuming that Color.yellow is for computer player
         if ( isHumanVsComputer && playerColor == Color.yellow ) {               
           doComputerMove();
         }
       }      
     }
   } 
 }

NOTE THIS IS NOT MY FULL CODE

注意这不是我的完整代码

UDPATE: This is what I have in my Panel class..

UDPATE:这就是我在我的面板类中所拥有的..

IsWin myIsWin =  new IsWin();
myIsWin.Winner(column, row);

回答by user2004685

You can make the method which you want to call from another class staticand then call it using <ClassName>.<MethodName>.

您可以创建要从另一个类调用的方法,static然后使用<ClassName>.<MethodName>.

For Example,

例如,

public class Foo {
    public static void foobar() {
        /* Your Code Here */
    }
}

Now, you can access this method using Foo.foobar();as follows:

现在,您可以使用以下方法访问此方法Foo.foobar();

public class Panel {
    public void someMethod() {
        Foo.foobar();
        /* Your Code Here */
    }
}

回答by ΦXoc? ? Пepeúpa ツ

you will need to declare an object of the class IsWinin the Panelclass and to that instance call the method Winner()

您需要在Panel类中声明类IsWin的对象,并对该实例调用该方法Winner()

Example:

例子:

IsWin myIsWin =  new IsWin....

myIsWin.Winner(0, 0);

回答by Etienne GT

You have to create the IsWin Object and access the Winner method. For Example:

您必须创建 IsWin 对象并访问 Winner 方法。例如:

    IsWin iw = new IsWin();
    if ( iw.winner(column, lastEmptyIdx) ) {
        String message = playerColor == Color.red ? " Player One Won!" : " Player Two Won!";
        JOptionPane.showMessageDialog(null, message, " Results ", JOptionPane.INFORMATION_MESSAGE);

        System.exit(0);            
    }