java java中的静态和非静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4581482/
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-10-30 07:03:27  来源:igfitidea点击:

static and non static methods in java

javaclassmethodsstatic-methods

提问by user560084

Possible Duplicate:
When should a method be static?

可能的重复:
方法什么时候应该是静态的?

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();
 }
}

回答by AniDev

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();.

将类视为饼干切割机,将对象视为饼干(类的实例)。静态方法是类本身(饼干切割器)的方法的一部分,如MyClass.myMethod();,而非静态方法是类实例(饼干)的一部分,如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.

在您的情况下,静态方法通常是特定于售票机的东西,例如计算应扣除多少钱。非静态方法将是特定于单个票务柜台的事情,例如跟踪它已处理的票数。

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

以下是更多信息:http:
//cscie160-distance.com/nonstatic.html

回答by SLaks

A staticmethod is not associated with an instance of a class.
Any method which doesn't use or modify the instance of the class it's defined on should be static.

static方法不与类的实例相关联。
任何不使用或修改它定义的类的实例的方法都应该是static.

回答by Matthew Flaschen

Instance methods are per-ticket machine, while static methods do something that affects all ticket machines simultaneously.

实例方法是针对每个售票机的,而静态方法会同时影响所有售票机。

Now, in your case, you may realistically only use on e at a time. But conceptually, each machine would have its own keyboard, screen, and CPU, so all the methods should be instance.

现在,在您的情况下,您实际上一次只能使用 e。但从概念上讲,每台机器都有自己的键盘、屏幕和 CPU,所以所有的方法都应该是实例。

That means you need to create a machine in main:

这意味着您需要在以下位置创建机器main

UNInterfaceTest machine = new UNInterfaceTest();
machine.run();

Of course, you're currently writing directly to standard out (not sure what Inputreads from). A better design might be to pass in a Writerto the UNInterfaceTest constructor, then write to that (instead of System.out). That lets each machine have its own screen.

当然,您目前正在直接写入标准输出(不确定Input读取的是什么)。更好的设计可能是将 a 传递Writer给 UNInterfaceTest 构造函数,然后写入该构造函数(而不是 System.out)。这让每台机器都有自己的屏幕。

Of course, some of this information may be beyond the scope of the assignment. It should still be useful to know.

当然,其中一些信息可能超出了分配的范围。了解一下应该还是有用的。

回答by crnlx

Short answer: That is because you are not making use of your objects. You can instantiate your class as an object and run it using

简短的回答:那是因为你没有使用你的对象。您可以将您的类实例化为一个对象并使用

 
UNInterfaceTest unit = new UNInterfaceTest();
unit.run();

using non-static methods.

使用非静态方法。