非静态方法错误,如何修复?

时间:2015-11-07 00:39:31

标签: java

我一直收到错误消息“非静态方法getCountAirports()无法从静态上下文引用”如何在Airport.java中修复此错误另外,我的布尔语句是否正确?

我已经看过以下内容: ECMAScript 2015 What is the reason behind "non-static method cannot be referenced from a static context"? 他们两个都无法帮助我理解什么是不正确的。

共有3个节目。 Airport.java,AirportClient.java和Pause.java。

预先写好AirportClient.java和Pause.java。 Airport.java是我填写的。错误位于Airport.java,其他两个文件保持不变

Airport.java

public class Airport
{

  // instance variables
  private String airportCode;
  private int gates;

  // 1. ***** Add a static class variable *****
  //  countAirports is an int
  //  assign an initial value of 0

  private static int countAirports = 0;



  // 2. ***** Modify this method *****
  // Default constructor:
  // method name: Airport
  // return value:  none
  // parameters: none
  // function: sets the airportCode to an empty String
  //    ***** add 1 to countAirports class variable
  public Airport( )
  {
     airportCode = "";
     countAirports++;

  }

  // 3. ***** Modify this method *****
  // Overloaded constructor:
  // method name: Airport
  // return value: none
  // parameters:  a String airport code and an int startGates
  // function: assigns airportCode the value of the
  //      startAirportCode parameter;
  //      calls the setGates method,
  //      passing the startGates parameter
  //    ***** add 1 to countAirports class variable
  public Airport( String startAirportCode, int startGates )
  {
     airportCode = startAirportCode;
     setGates( startGates );
     countAirports++;

  }

  // Accessor method for the airportCode instance variable
  // method name: getAirportCode
  // return value: String
  // parameters: none
  // function: returns airportCode
  public String getAirportCode( )
  {
     return airportCode;
  }

  // Accessor method for the gates instance variable
  // method name: getGates
  // return value: int
  // parameters: none
  // function: returns gates
  public int getGates( )
  {
     return gates;
  }

  // 4. ***** Write this method *****
  // Accessor method for the countAirports class variable
  // method name: getCountAirports
  // return value: int
  // parameters: none
  // function: returns countAirports
  public int getCountAirports()
  {
    return countAirports;
  }




  // Mutator method for the airportCode instance variable
  // method name: setAirportCode
  // return value: void
  // parameters: String newAirportCode
  // function: assigns airportCode the value of the
  //                    newAirportCode parameter
  public void setAirportCode( String newAirportCode )
  {
     airportCode = newAirportCode;
  }

  // Mutator method for the gates instance variable
  // method name: setGates
  // return value:  void
  // parameters: int newGates
  // function: validates the newGates parameter.
  //   if newGates is greater than 0, sets gates to newGates;
  //   otherwise, prints an error message to System.err
  //   and does not change value of gates
  public void setGates( int newGates )
  {
     if ( newGates  >=  0 )
       gates = newGates;
     else
     {
       System.err.println( "Gates must be at least 0" );
       System.err.println( "Value of gates unchanged." );
     }
  }

  // 5. ***** Write this method *****
  // method name:  toString
  // return value: String
  // parameters: none
  // function:  returns a String that contains the airportCode
  //    and gates

  public String toString()
  {
    return "Airport code: " + airportCode + "; gates: " + gates ;
  }


  // 6. ***** Write this method *****
  // method name: equals
  // return value: boolean
  // parameter:  Airport object
  // function:  returns true if airportCode
  //     and gates in this object
  //    are equal to those in the parameter object;
  //    returns false otherwise

  public boolean equals( Object o )
  {
    if ( ! (o instanceof Airport))
      return false;
    else
    {
      Airport objAirport = ( Airport ) o;
      if (airportCode.equals(objAirport.airportCode)
            && gates == objAirport.gates)
        return true;
      else
        return false;
    }







   }
}// end of Airport class definition

这是AirportClient.java

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class AirportClient extends JFrame
{
  String action1, action2;
  boolean firstTime = true;

  double animationPause = 6.0;     // 6 seconds between animations
  Airport airport1, airport2;  // declare Airport object references

  public void workWithAirports( )
  {
    animate( "Two airport object references declared:",
                   "Airport airport1, airport2;" );

    /* Instantiate airport1 using the overloaded constructor  */
    airport1 = new Airport( "IAD", 30 );
    animate( "Instantiated airport1 using overloaded constructor:",
                   "airport1 = new Airport( \"IAD\", 30 );" );

    /* Call toString() */
    animate( "Calling toString:",
                    "JOptionPane.showMessageDialog( null, airport1.toString( ) );" );
    JOptionPane.showMessageDialog( null, airport1.toString( ) );

    /*  Instantiate a second airport object using overloaded constructor*/
    airport2 = new Airport( "IAD", 30 );
    animate( "Instantiated airport2 using overloaded constructor:",
                   "airport2 = new Airport( \"IAD\", 30 );" );

    /* Get the value of countAirports */
    animate( "Getting the value of countAirports:",
                    "JOptionPane.showMessageDialog( null, \"countAirports is \" +"
                            + "  Airport.getCountAirports( ) );" );
    JOptionPane.showMessageDialog( null, "countAirports is " +
                     Airport.getCountAirports( ) );

     /* Compare the two airport objects */
     animate( "Comparing airport1 and airport2 using the equality operator ",
                    " if ( airport1 ==  airport2 )..." );
     if ( airport1 ==  airport2 )
        JOptionPane.showMessageDialog( null, "airport1 and airport2 are equal" );
     else
        JOptionPane.showMessageDialog( null, "airport1 and airport2 are not equal" );

     /* Compare the two Airport objects */
     animate( "Comparing airport1 and airport2 using equals:",
                       " if ( airport1.equals( airport2 ) )..." );
     if ( airport1.equals( airport2 ) )
        JOptionPane.showMessageDialog( null, "airport1 and airport2 are equal" );
     else
        JOptionPane.showMessageDialog( null, "airport1 and airport2 are not equal" );

     /* Finished */
     animate( "Actions are complete, exiting", "" );
     System.exit( 1 );
   }

  public AirportClient( )
  {
    super( "Using the Airport Class" );
    setSize( 520, 400 );
    setVisible( true );
  }

  public void paint( Graphics g )
  {
    super.paint( g );
    if ( firstTime )
      firstTime = false;
    else
    {
      int boxL = 75, boxH = 20;
      int sX = 50, sY = 50;

      // countAirports
      g.setColor( Color.BLACK );
      g.drawRect( sX, sY, boxL, boxH );
      g.drawString( "countAirports", sX, sY - 10 );
      g.setColor( Color.BLUE );
      g.drawString( Integer.toString( Airport.getCountAirports( ) ),
                            sX + 15, sY + 15 );

      // airport1
      sY = 125;
      if ( airport1 != null )
      {
        // object reference box
        g.setColor( Color.BLACK );
        g.drawRect( sX, sY, boxL, boxH );
        g.drawString( "airport1", sX, sY - 10 );
        draw( g, sX, sY, airport1 ); // draw airport object
      }
      else
      {
        // indicate null reference
        g.setColor( Color.BLACK );
        g.drawRect( sX, sY, boxL, boxH );
        g.drawString( "airport1", sX, sY - 10 );
        g.setColor( Color.BLUE );
        g.drawString( "null", sX + 15, sY + 15 );
      }

      sY = 250;
      if ( airport2 != null )
      {
        // object reference box
        g.setColor( Color.BLACK );
        g.drawRect( sX, sY, boxL, boxH );
        g.drawString( "airport2", sX, sY - 10 );
        draw( g, sX, sY, airport2 ); // draw airport object
       }
       else
       {
         // indicate null reference
   g.setColor( Color.BLACK );
         g.drawRect( sX, sY, boxL, boxH );
         g.drawString( "airport2", sX, sY - 10 );
         g.setColor( Color.BLUE );
         g.drawString( "null", sX + 15, sY + 15 );
       }

       // display action at bottom of screen
       g.setColor( Color.BLUE );
       g.drawString( action1, 15, 370 );
       g.drawString( action2, 20, 385 );
    }
  }

  private void draw( Graphics g, int sX, int sY, Airport a )
  {
    int boxL = 75, boxH = 20;

    // arrow
    g.setColor( Color.BLACK );
    g.drawLine( sX + boxL, sY + boxH / 2,
                 sX + boxL + 25, sY + boxH / 2 );
    g.drawLine( sX + boxL + 25, sY + boxH / 2,
                 sX + boxL + 25, sY + boxH * 2 );
    g.drawLine( sX + boxL + 25 - 5, sY + boxH * 2 - 5,
                 sX + boxL + 25, sY + boxH * 2 );
    g.drawLine( sX + boxL + 25 + 5, sY + boxH * 2 - 5,
                 sX + boxL + 25, sY + boxH * 2 );

    // airportCode
    g.setColor( Color.BLACK );
    g.drawString( "airport code", sX + boxL - 75, sY + 2 * boxH + 15 );
    g.drawRect( sX + boxL, sY + 2 * boxH, boxL, boxH );
    g.setColor( Color.BLUE );
    g.drawString( a.getAirportCode( ),
                         sX + boxL + 5, sY + 2 * boxH + 15 );

    // gates
    g.setColor( Color.BLACK );
    g.drawString( "gates", sX + boxL - 75, sY + 3 * boxH + 15 );
    g.drawRect( sX + boxL, sY + 3 * boxH, boxL, boxH );
    g.setColor( Color.BLUE );
    g.drawString( Integer.toString( a.getGates( ) ),
                  sX + boxL + 5, sY + 3 * boxH + 15 );
  }

  private void animate( String a1, String a2 )
  {
    action1 = a1;
    action2 = a2;
    repaint( );
    Pause.wait( animationPause );
  }

  public static void main( String[] args )
  {
    AirportClient app = new AirportClient( );
    app.workWithAirports( );
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  }
}

和Pause.java

public class Pause
{
  /** wait method
  *  @param seconds  number of seconds to pause
  */
  public static void wait( double seconds )
  {
     try
     {
       Thread.sleep( (int)( seconds * 1000 ) );
     }
     catch ( InterruptedException e )
     {
       e.printStackTrace( );
     }
  }
}

1 个答案:

答案 0 :(得分:0)

static方法意味着它是类的方法,而不是该类的特定实例(Object)。这就是您的方法应该是什么(只需将static关键字添加到方法声明中。)

但是,当声明表明它是非静态的时,您将以静态方式引用它。这导致错误。

非静态方法意味着在类的实例上调用它 - 它不适用于本身。

例如,对于static

// if you're only manipulating something for the entire `Airport` class,
// independent of its objects, use a `static` method (or field, for that matter)
// in this case, you are not doing `getCountAirports()` ON a certain object
public static getCountAirports() { ... }
public static int countAirports = 0;

// and these can be referenced on the CLASS:
Airport.getCountAirports();
Airport.countAirports

非 - static

// in this case, this is for a particular `Airport` instance:
public String getAirportCode() { ... }
public String airportCode;

// and this can be called on the OBJECT
Airport a = new Airport("JFK", 39);
a.getAirportCode();
a.airportCode;