静态和非静态方法在java中 [英] static and non static methods in java

查看:116
本文介绍了静态和非静态方法在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复:

什么时候一个方法应该是静态的?

i我试图做管/地铁票机的接口类。好不是真的,但在计算机科学模块的课程。我不明白什么时候使用静态方法。我不太了解计算机科学,但主要方法似乎使用静态。

i am trying to make an interface class for tube/subway ticket machine. well not for real, but for a coursework in a computer science module. i dont understand when to use static methods. i dont know much about computer sciences, but main methods seem to use static.

class UNInterfaceTest
{
   public static final int NOTTING_HILL = 1;
   public static final int HIGH_KEN = 2;
   public static final int GLOUS = 3;
   public static final int SOUTH_KEN = 4;
   public static final int SLOANE = 5;
   public static final int VICTORIA = 6;
   public static final int ST_JAMES = 7;
   public static final int WESTMINSTER = 8;
   public static final int QUIT = 10;
   private Input in = new Input();

private static void displayMenu()
{
   System.out.println("CIRCLE LINE: Please Select the Number of Your Current Station.");
   System.out.println(NOTTING_HILL + ": Nottinghill_Gate");
   System.out.println(HIGH_KEN + ": High_Street_Kensignton");
   System.out.println(GLOUS + ": Gloucester_Road");
   System.out.println(SOUTH_KEN + ": South_Kensignton");
   System.out.println(SLOANE + ": Sloane_Square");
   System.out.println(VICTORIA + ": Victoria"); 
   System.out.println(ST_JAMES + ": St_James_Park");
   System.out.println(WESTMINSTER + ": Westminster");
   System.out.println();
   System.out.println(QUIT + ". Quit");
}

public static void run()
{
    while(true)
    {
        displayMenu();
        int option = getMenuInput();
        if (option == QUIT)
    {
        break;
    }
    doOption(option);
    }
 }

private static void doOption(int option)
{ 
    switch(option){
 case NOTTING_HILL:
     //findNottinghill_Gate();
       break;
      case HIGH_KEN:
     //findHighStreetKensignton();
       break;
      case GLOUS:
     //findGloucesterRoad();
       break;
      case SOUTH_KEN:
     //findSouthKensignton();
       break;
      case SLOANE:
     // findSloaneSquare();
       break;
      case VICTORIA:
     //findVictoria();
       break;
      case ST_JAMES:
     //findStJamesPark();
       break;
      case WESTMINSTER:
      //findWestminster();
       break;
      default:
        System.out.println("Invalid option - try again");
    }
} 
private int getMenuInput()
{
    //KeyboardInput val = new KeyboardInput();
    System.out.print("Enter menu selection: ");
    int option = in.nextInt();
    in.nextLine();
    return option;
}

 public static void main(String[] args)
 {
    run();
 }
}


推荐答案

作为cookie切割器的类,以及作为cookie(类的实例)的对象。静态方法是类本身(cookie切割器)的一部分,如 MyClass.myMethod(); ,而非静态方法是类实例的一部分cookie),如 new MyClass()。myMethod();

Think of a class as a cookie cutter, and an object as the cookie (an instance of the class). Static methods are methods part of the class itself (the cookie cutter), as in MyClass.myMethod();, whereas non-static methods are part of the class instances (the cookies), as in new MyClass().myMethod();.

是一般的机票,例如计算应该扣除多少钱。

In your case, static methods would be things specific to ticket machines in general, like calculating how much money should be deducted. Non-static methods would be things specific to a single ticket counter, such as keeping track of how many tickets it has processed.

这里是一些更多的信息:

http://cscie160-distance.com/nonstatic.html

Here is some more information:
http://cscie160-distance.com/nonstatic.html

这篇关于静态和非静态方法在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆