问题描述
在这本书中,有关于如何使用静态变量和方法本实施例。我不明白是怎么回事。解释为什么必须有中的方法和变量前面静态的。有两个单独的类调用病毒和VirusLab。该VirusLab.java发生在一个命令行参数,使病毒的对象的数量,然后吐出病毒的对象的数量。谢谢
病毒:
公共类病毒{静态INT virusCount = 0;公共病毒(){
virusCount ++;
}公共静态INT getVirusCount(){
返回virusCount;
}}
VirusLab:
公共类VirusLab {
公共静态无效的主要(字串[] args){
INT numViruses =的Integer.parseInt(参数[0]);
如果(numViruses大于0){
病毒[] = virii新病毒[numViruses]
的for(int i = 0; I< numViruses;我++){
virii [I] =新病毒();
}
的System.out.println(有+ Virus.getVirusCount()
+病毒。);
}
}
}
一个网络搜索会给你几百个链接来解释Java中的静态的关键字。
请参阅以下文件:
另外,请查阅一本教科书为您进一步的学习,这将有很大的帮助。
In this book, there is this example of how to use static variables and methods. I dont understand what is going on. explain why there has to be static in front of the methods and variables. There are two seperate classes called Virus and VirusLab. The VirusLab.java takes in a command line argument and makes the amount of Virus objects, then spits out the number of Virus objects. Thanks
Virus:
public class Virus {
static int virusCount = 0;
public Virus() {
virusCount++;
}
public static int getVirusCount() {
return virusCount;
}
}
VirusLab:
public class VirusLab {
public static void main(String[] args) {
int numViruses = Integer.parseInt(args[0]);
if (numViruses > 0) {
Virus[] virii = new Virus[numViruses];
for (int i = 0; i < numViruses; i++) {
virii[i] = new Virus();
}
System.out.println("There are " + Virus.getVirusCount()
+ " viruses.");
}
}
}
A web search would have given you hundreds of links to explain 'static' keyword in Java.
Please refer to the following documentation: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Also, please refer a text book for your further learning, that will help a lot.
这篇关于什么是static修饰符在Java中做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!