如何实例化游戏(How to Instantiate Game)
我几乎在Java的每个游戏示例中都看到了这段代码。 创建
Game
类的实例,然后从该实例执行循环。public static void main(String[] args) { Game g = new Game(); g.gameLoop(); } public Game () { }
但是,因为除了执行
gameLoop()
之外我什么都不做,为什么不这样写呢? 我真的不需要在任何地方存储Game
实例吗? 喜欢这个。public static void main(String[] args) { new Game (); } public Game () { gameLoop(); }
如果是这样的话?
这两种情况都在起作用,并且已经过我的测试。
I see this piece of code in almost every game example for Java. Create an instance of the
Game
class then execute the loop from that instance.public static void main(String[] args) { Game g = new Game(); g.gameLoop(); } public Game () { }
However, as I don't do anything apart from the execution of the
gameLoop()
why not write it like this? I don't really need to store the instance ofGame
anywhere do I? Like this.public static void main(String[] args) { new Game (); } public Game () { gameLoop(); }
If so for what?
Both cases are working and have been tested by me.
原文:https://stackoverflow.com/questions/27388484
最满意答案
使用
default
情况下!switch (e.which) { case 66: // "B" button on the keyboard alert("it is bold"); break; case 73: // "I" button on the keyboard alert("it is italic"); break; default: // everything else return; // without preventing the default action } e.preventDefault(); return false;
Use the
default
case!switch (e.which) { case 66: // "B" button on the keyboard alert("it is bold"); break; case 73: // "I" button on the keyboard alert("it is italic"); break; default: // everything else return; // without preventing the default action } e.preventDefault(); return false;
相关问答
更多-
获取所有开关案例[关闭](Gets all switch cases [closed])[2022-02-14]
我会使用字典来提取,例如: var dict = new Dictionary{ { "help", doHelp }, { "command1", doCommand1 } }; // then you can do: var action = dict["help"]; action(); // if you want to add another command later, use: dict.Add("command2", () => doComman ... -
处理开关案例(Processing switch cases)[2022-08-26]
你可以这样做,但这不是switch命令的逻辑用法: String.prototype.startsWith = function( str ){ return ( this.indexOf( str ) === 0 ); }; var myVar = 'product 42'; switch (true) { case myVar.startsWith( 'product' ): alert(1); // do something break; } Yo ... -
多个案例在开关:(Multiple cases in switch statement)[2023-08-04]
对于您提到的第二种方法,C ++和C#中没有语法。 你的第一种方法没有错。 然而,如果你有很大的范围,只需使用一系列if语句。 There is no syntax in C++ nor C# for the second method you mentioned. There's nothing wrong with your first method. If however you have very big ranges, just use a series of if statements. -
java-超过18000个案例,我应该使用switch还是if语句?(java-More than 18000 cases, should i use switch or if statement?)[2022-12-06]
您可以按需加载报价文件并缓存其报价以便在同一会话中使用,例如: public class Quotes { Hashtable cache; public Quotes() { cache = new Hashtable(); } public String getQuote(int person, int quoteNumber) { if (!cache.containsKey(new Integer(person))) { ... -
C ++ Switch Cases(C++ Switch Cases)[2023-08-23]
以下是此代码的行为方式。 x等于零 所以cout<<"Zero"; 被执行。 因为没有break; 在它之后, 执行第二种情况: cout<<"Hello World"; 而自从cout<<"something"; 打印后不添加换行符,它们打印为单个单词。 Here's how this code behaves. x is equal to zero so cout<<"Zero"; is executed. Since there's no break; after it, the second cas ... -
如果一个开关匹配多个案例,所有匹配的案例都会运行(If a switch matches multiple cases will all matching cases run?)[2021-10-05]
不,不是这样的。 在您的示例中只会运行其中一个开关案例。 一旦遇到break ,交换机处理就结束。 只有一个分支匹配。 在PHP中,我认为(但您可能需要仔细检查)第一个匹配的案例将运行。 有多个可能的匹配,你需要使用一组if语句(注意不要使用if ... elseif) 我会转换为 if($test & 1) { // do stuff } if(test & 2) { // do stuff } No this is not the case. Only one of the swit ... -
成功匹配后继续匹配下一个案例的Scala开关(Scala switch which continue matching next cases after successful match)[2021-08-13]
如果你不能使用0 | 1 | 2 0 | 1 | 2 0 | 1 | 2你可以使用一系列操作来解决这个问题: def switch[T](i: T)(actions: (T, () => Unit)*)(default: => Unit) = { val acts = actions.dropWhile(_._1 != i).map{_._2} if (acts.isEmpty) default else acts.foreach{_()} } def myMethod(i: Int): Un ... -
列出所有可能的开关案例?(List all possible switch cases?)[2023-12-06]
我通过https://commandline.codeplex.com/上的CommandLineParser实现了这一目标。 例如: class Options { [Option("account-name", Required = true, HelpText = "Name of the account to use")] public string AccountName { get; set; } [Option("single- ... -
只要将这些案例放在一起,即可使用相同的代码。 正如Grant在评论中指出的那样,它在文档中: http : //msdn.microsoft.com/en-us/library/06tc147t.aspx enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } private string Do(Day day) { switch (day) { case Day.Mon ...
-
使用default情况下! switch (e.which) { case 66: // "B" button on the keyboard alert("it is bold"); break; case 73: // "I" button on the keyboard alert("it is italic"); break; default: // everything else retu ...