HeadFirst Java学习笔记——类间交互

虽然之前通过看视频和入门书学习了简单的java知识,不过感觉自己离真正的入门还差了点呢,所以开启刷书之路,先从这本相对来说简单而又生动的HeadFitst JAVA开始吧。

类与对象:
类不是对象,却是用来创建对象的模型

main的两种用途:
测试真正的类
启动java应用程序

书中代码:猜数字的小游戏
(这个小程序的目的是体现java中类之间的交互,把main解放出来)
game产生一个一定范围内的随机数,几个player猜测该数字
类的设计:GuessGame,Player,GameLaunchers

思路是:
GameLaunchers作为登录器,实例化 GuessGame;
GuessGame对象的成员变量决定了数字范围,玩家数等游戏信息,并由start方法运行。
Player对象呢,就只有一个ID号,哦对了,还有猜的数字的相关信息和方法

废话不多说啦,把代码拉出来看看
先上登录器的代码
   
   
  1. package GuessingGame;
  2. import java.io.IOException;
  3. public class GameLauncher {
  4. public static void main(String[] args) throws IOException {
  5. GuessGame newgame=new GuessGame(1,30,2);
  6. while(true){
  7. newgame.start();
  8. System.out.println("按Q键退出,其他任意键继续");
  9. if(System.in.read()=='q'||System.in.read()=='Q')break;
  10. }
  11. }
  12. }

再是核心游戏功能的实现
   
   
  1. package GuessingGame;
  2. import java.util.Scanner;
  3. public class GuessGame {
  4. int minNum=0;
  5. int maxNum=3;
  6. int guessNum=-1;
  7. int playerNum=3;
  8. Player[] plarr;
  9. public GuessGame(){
  10. this.plarr =new Player[this.playerNum];
  11. for(int i=0;i<plarr.length;i++){
  12. plarr[i]=new Player(i+1);
  13. }
  14. System.out.println("Ready");
  15. }
  16. public GuessGame(int minN,int maxN,int playerNum){
  17. this.minNum=minN;
  18. this.maxNum=maxN;
  19. this.guessNum=minNum-1;
  20. this.playerNum=playerNum;
  21. this.plarr =new Player[this.playerNum];
  22. for(int i=0;i<plarr.length;i++){
  23. plarr[i]=new Player(i+1);
  24. }
  25. System.out.println("Ready");
  26. }
  27. public void start(){
  28. System.out.println("I'm thinking of a number between "+minNum+" and "+maxNum);
  29. this.guessNum=(int)((Math.random())*(maxNum-minNum))+minNum;
  30. System.out.println("Guess what it is!");
  31. Scanner sc=new Scanner(System.in);
  32. for(int i=0;i<this.plarr.length;i++){
  33. System.out.println("Please Guess, "+"Player "+plarr[i].id);
  34. plarr[i].guessNum(sc.nextInt());
  35. }
  36. System.out.println("Now it's the time to judge:");
  37. boolean hasRight=false;
  38. StringBuilder str=new StringBuilder();
  39. for(int i=0;i<plarr.length;i++){
  40. if(plarr[i].guess==this.guessNum){hasRight=true;str.append("player "+plarr[i].id+" ,");}
  41. }
  42. if(hasRight){
  43. str.deleteCharAt(str.length()-1);
  44. str.append("win the game");
  45. }else{
  46. str.replace(0,str.length(), "No one get the right number");
  47. }
  48. System.out.println("The number is "+this.guessNum+". "+str);
  49. }
  50. }
然后是简单的player类
   
   
  1. package GuessingGame;
  2. public class Player {
  3. int id;
  4. int guess;
  5. public Player(int id){
  6. this.id=id;
  7. }
  8. public void guessNum(int guess){
  9. this.guess=guess;
  10. System.out.println("Player "+this.id+" is guess "+guess);
  11. }
  12. }

最后我试了下java的打包发布,把它打包成了jar包,在cmd中跑了一下,不过要记得为多文件的它设置一个入口,在这里就是GuessLauncher这个类了。

虽然简单,不过还是很开心~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值