简体   繁体   English

静态和非静态方法之间的区别

[英]difference between static and non-static methods

I'm doing a fairly simple program in order to learn the difference between static and non-static methods and variables, and I thought I understood it, but I can't get the following code to run: 我正在做一个相当简单的程序,以了解静态和非静态方法与变量之间的区别,我以为我理解了,但是我无法运行以下代码:

public class Zombie {
String name;
int serial = 0;
static int count = 0;

public Zombie(String name) {
this.name = name;
count++;
}

static String printStatus() {
String status;
if(count == 1) {
  status = (count + "zombie created so far");
}
else {
  status = (count + "zombies created so far");
}
return status;
}
String printZombie() {
String ident = (name + " is zombie " + serial);
return ident;
}

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

Zombie z1 = new Zombie("Deb");

printStatus();

Zombie z2 = new Zombie("Andy");

printStatus();

Zombie z3 = new Zombie("Carl");

printStatus();

z1.printZombie();
z2.printZombie();
z3.printZombie();
}
}

It should have an output of: 它的输出应为:

0 zombies created so far
1 zombie created so far
2 zombies created so far
3 zombies created so far
Deb is zombie 0
Andy is zombie 1
Carl is zombie 2

But I can't get it to run. 但是我无法运行它。 I assume the problem (at least one of them) is with the first method, but I can't figure it out. 我认为问题(至少其中一个)是第一种方法,但我无法弄清楚。 count is supposed to be static and the other two variables aren't, and printStatus is supposed to be static but printZombie is not. count应该是静态的,而其他两个变量不是,而printStatus应该是静态的,而printZombie不是。 Can someone please explain this to me? 有人可以向我解释一下吗?

You're exactly right: 您完全正确:

1) The difference between "static" and "non-static" is basically the difference between "class-wide" and "per object instance". 1)“静态”和“非静态”之间的区别基本上是“全类”和“每个对象实例”之间的区别。

2) Your "count" goes from 0 (before creating the 1st zombie) to 3 (after creating the last). 2)您的“计数”从0(在创建第一个僵尸之前)到3(在创建最后一个僵尸之后)。

3) All three objects reference the same static "count". 3)所有三个对象都引用相同的静态“计数”。

What I don't get is how "serial" is incrementing :( Did you leave anything out of your code sample? 没有得到的是“串行”如何递增:(您是否在代码示例中遗漏了任何东西?

Here's what I get from your code: 这是我从您的代码中得到的:

// Modified code
public class Zombie {
  String name;
  int serial = 0;
  static int count = 0;

  public Zombie(String name) {
System.out.println ("Zombie(" + name + "): serial=" + serial + ", count=" + count + "...");
    this.name = name;
    count++;
  }

  static String printStatus() {
System.out.println ("printStatus: count=" + count + "...");
    String status;
    if(count == 1) {
      status = (count + " zombie created so far");
    }
    else {
      status = (count + " zombies created so far");
    }
System.out.println ("  " + status);
    return status;
  }

  String printZombie() {
System.out.println ("printZombie: serial=" + serial + ", count=" + count + "...");
    String ident = (name + " is zombie " + serial);
System.out.println ("  " + ident);
    return ident;
  }

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

    Zombie z1 = new Zombie("Deb");
    printStatus();

    Zombie z2 = new Zombie("Andy");
    printStatus();

    Zombie z3 = new Zombie("Carl");
    printStatus();

    z1.printZombie();
    z2.printZombie();
    z3.printZombie();
  }
}

Corresponding output: 对应的输出:

printStatus: count=0...
  0 zombies created so far
Zombie(Deb): serial=0, count=0...
printStatus: count=1...
  1 zombie created so far
Zombie(Andy): serial=0, count=1...
printStatus: count=2...
  2 zombies created so far
Zombie(Carl): serial=0, count=2...
printStatus: count=3...
  3 zombies created so far
printZombie: serial=0, count=3...
  Deb is zombie 0
printZombie: serial=0, count=3...
  Andy is zombie 0
printZombie: serial=0, count=3...
  Carl is zombie 0

static is a context that belongs to the Class, non-static methods are executed under the object's context static是属于Class的上下文,非静态方法在对象的上下文下执行

printStatus(); is exactly the same for all the zombies while printZombie will depend on the object. 对于所有僵尸都完全相同,而printZombie将取决于对象。

Usually static methods should be stand-alone, like math functions. 通常,静态方法应该是独立的,例如数学函数。

Math.divide(NumberA, NumberB)

Static attributes/methods can be called outside the class using the class itself as reference, like this: 可以使用类本身作为引用在类外部调用静态属性/方法,如下所示:

Zombie.count , if you are inside the class you can use it as in your main method, but it may look confusing Zombie.count ,如果您在类内部,则可以像在main方法中一样使用它,但看起来可能会令人困惑

Static methods can be accessed without instantiating the class ie Zombie.printStatus(); 无需实例化类即Zombie.printStatus();即可访问静态方法。 as opposed to the printZombie method which can only be accessed when you have created a new Zombie object. 与printZombie方法相反,后者只能在创建新的Zombie对象时访问。

When you say you cant get it to run, what do you mean? 当您说无法运行它时,您是什么意思? what's going wrong? 怎么了 I think it's running but you;re not directing the output to the console: 我认为它正在运行,但是您没有将输出定向到控制台:

You should be doing this: 您应该这样做:

System.out.println(printStatus());

This is because printStatus() merely returns a String. 这是因为printStatus()仅返回String。

Very simply, static variables only have one copy no matter how many times they're instantiated. 简单地说,静态变量无论实例化多少次,都只有一个副本。 Non-static variables are the opposite ie there are as many copies as the number of times they instantiated. 非静态变量则相反,即副本的数量与其实例化的次数一样多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM