首页 > 代码库 > HeadFirstJava学习心得——main方法
HeadFirstJava学习心得——main方法
main()的2种用途:
- 测试真正的类
- 启动java application
真正的java application只会让对象和对象进行交互(相互调用)。
下面的一个猜数字的游戏充分体现了以上观点:
玩家角色类:持有自己的数字number和一个guess()方法:
public class Player { int number = 0; // 被猜测的数字 public void guess() { number = (int) (Math.random() * 10); System.out.println("I'm guessing " + number); } }
猜数字游戏类:游戏的内部逻辑
public class GuessGame { /* 用3个实例变量分别表示3个Player对象 */ Player p1; Player p2; Player p3; public void startGame() { /* 创建3个Player对象 */ p1 = new Player(); p2 = new Player(); p3 = new Player(); /* 声明3个变量来保存猜测的数字 */ int guessp1 = 0; int guessp2 = 0; int guessp3 = 0; /* 声明3个变量来保存是否猜中 */ boolean p1isRight = false; boolean p2isRight = false; boolean p3isRight = false; /* 产生谜底数字 */ int targetNumber = (int) (Math.random() * 10); System.out.println("I'm thinking of a number between 0 and 9..."); while (true) { System.out.println("Number to guess is " + targetNumber); /* 调用Player的guess()方法 */ p1.guess(); p2.guess(); p3.guess(); /* 取得每个Player所猜测的数字并将其列出 */ guessp1 = p1.number; System.out.println("Player one guessed " + guessp1); guessp2 = p2.number; System.out.println("Player two guessed " + guessp2); guessp3 = p3.number; System.out.println("Player three guessed " + guessp3); /* 检查是否猜中,若猜中则设定是否猜中的变量 */ if (guessp1 == targetNumber) { p1isRight = true; } if (guessp2 == targetNumber) { p2isRight = true; } if (guessp3 == targetNumber) { p3isRight = true; } /* 如果有人猜中,就打印信息,结束游戏 */ if (p1isRight || p2isRight || p3isRight) { System.out.println("We have a winner!"); System.out.println("Player one got it right?" + p1isRight); System.out.println("Player two got it right?" + p2isRight); System.out.println("Player three got it right?" + p3isRight); System.out.println("Game over."); break; } else { // 都没有猜到,所以要继续下去 System.out.println("Player will have to try it agin."); } } } }
主方法:启动游戏
public class GameLauncher { public static void main(String[] args) { GuessGame game = new GuessGame(); game.startGame(); } }
游戏的运行结果:
I‘m thinking of a number between 0 and 9...
Number to guess is 6
I‘m guessing 9
I‘m guessing 9
I‘m guessing 2
Player one guessed 9
Player two guessed 9
Player three guessed 2
Player will have to try it agin.
Number to guess is 6
I‘m guessing 8
I‘m guessing 5
I‘m guessing 8
Player one guessed 8
Player two guessed 5
Player three guessed 8
Player will have to try it agin.
Number to guess is 6
I‘m guessing 3
I‘m guessing 8
I‘m guessing 8
Player one guessed 3
Player two guessed 8
Player three guessed 8
Player will have to try it agin.
Number to guess is 6
I‘m guessing 0
I‘m guessing 4
I‘m guessing 7
Player one guessed 0
Player two guessed 4
Player three guessed 7
Player will have to try it agin.
Number to guess is 6
I‘m guessing 5
I‘m guessing 3
I‘m guessing 5
Player one guessed 5
Player two guessed 3
Player three guessed 5
Player will have to try it agin.
Number to guess is 6
I‘m guessing 6
I‘m guessing 9
I‘m guessing 7
Player one guessed 6
Player two guessed 9
Player three guessed 7
We have a winner!
Player one got it right?true
Player two got it right?false
Player three got it right?false
Game over.
Number to guess is 6
I‘m guessing 9
I‘m guessing 9
I‘m guessing 2
Player one guessed 9
Player two guessed 9
Player three guessed 2
Player will have to try it agin.
Number to guess is 6
I‘m guessing 8
I‘m guessing 5
I‘m guessing 8
Player one guessed 8
Player two guessed 5
Player three guessed 8
Player will have to try it agin.
Number to guess is 6
I‘m guessing 3
I‘m guessing 8
I‘m guessing 8
Player one guessed 3
Player two guessed 8
Player three guessed 8
Player will have to try it agin.
Number to guess is 6
I‘m guessing 0
I‘m guessing 4
I‘m guessing 7
Player one guessed 0
Player two guessed 4
Player three guessed 7
Player will have to try it agin.
Number to guess is 6
I‘m guessing 5
I‘m guessing 3
I‘m guessing 5
Player one guessed 5
Player two guessed 3
Player three guessed 5
Player will have to try it agin.
Number to guess is 6
I‘m guessing 6
I‘m guessing 9
I‘m guessing 7
Player one guessed 6
Player two guessed 9
Player three guessed 7
We have a winner!
Player one got it right?true
Player two got it right?false
Player three got it right?false
Game over.
java的垃圾回收机制(GC)
创建对象的时候,它会被存放在称为堆的内存区域中。不管对象如何创建都会在此区域中。此区域并非普通的堆;它是可以回收垃圾的堆(Garbage-Collectible Heap)。java会根据对象的大小来分配内存空间。比如说15个实例变量的对象所占用的空间就可能会比只有两个实例变量的对象要大。当JVM察觉到某个对象不再会被使用的时候,该对象就会被标记成可以回收的。如果内存开始不足,垃圾收集器就会启动来清理垃圾、回收空间,从而使空间能够被再次利用。
需要全局变量(global)或者方法该如何?
public static final修饰。
成百上千的类应该如何提交?是否可以包装成类似单一应用程序的形式?
可以把所有文件包装进依据pkzip格式来存档的java Archive——.jar文件,在jar文件中可以引用一个简单的文本文件(manifest)
里面有定义出jar中哪一个文件带有启动app的main()方法。
HeadFirstJava学习心得——main方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。