[关闭]
@Sarah 2015-04-24T10:02:09.000000Z 字数 8318 阅读 1358

Head first java 代码实现


chapter01

  1. int size = 27;
  2. //声明一个integer类型,名称为size的变量并初始值为27
  3. String name = Fido”;
  4. //声明名声为name的字符串,值为fido
  5. Dog myDog = new Dog(name, size);
  6. ////以name和size声明一个名称为mydog的dog变量
  7. x=size-5;
  8. //把size值减5的结果赋值给变量x
  9. if (x < 15) myDog.bark(8);
  10. //如果x<15让狗叫八次
  11. while(x>3){
  12. //当x>3 就持续执行循环
  13. myDog.play();
  14. }
  15. //让狗执行play动作,{}里是循环内容
  16. int[] numList = {2,4,6,8};
  17. //声明有四个元素的整形数组
  18. System.out.print(“Hello”);
  19. System.out.print(“Dog: + name);
  20. String num = 8”;
  21. //声明字符串变量num并赋值为8
  22. int z = Integer.parseInt(num);
  23. //讲字符串8转换成整数8
  24. try {
  25. //尝试列出可能会引起异常状况的指令
  26. readTheFile(“myFile.txt”);
  27. }
  28. //读取myfile.txt这个文件
  29. catch(FileNotFoundException ex) {
  30. //捕获万一发生找不到文件异常状况
  31. System.out.print(“File not found.”);
  32. //找不到就说找不到
  33. //{}里面就是异常处理情况

此处输入图片的描述

循环

  1. //声明 设定 调用方法等语句
  2. int x = 3;
  3. String name = Dirk”;
  4. x = x * 17;
  5. System.out.print(“x is + x);
  6. double d = Math.random();
  1. //反复做某事 while与for循环
  2. while (x > 12) {
  3. x = x -1;
  4. }
  5. for (int x = 0; x < 10; x = x + 1) {
  6. System.out.print(“x is now + x);
  7. }
  1. //在适当条件下做某事 if/else条件分支
  2. if (x == 10) {
  3. System.out.print(“x must be 10”);
  4. } else {
  5. System.out.print(“x isnt 10”);
  6. }
  7. if ((x < 3) & (name.equals(“Dirk”))) {
  8. System.out.println(“Gently”);
  9. }
  10. System.out.print(“this line runs no matter what”);
  1. //while循环范例
  2. public class Loopy {
  3. public static void main (String[] args) {
  4. int x = 1;
  5. System.out.println(“Before the Loop”);
  6. while (x < 4) {
  7. System.out.println(“In the loop”);
  8. System.out.println(“Value of x is + x);
  9. x = x + 1;
  10. }
  11. System.out.println(“This is after the loop”);
  12. }
  13. }
  1. //if/else范例
  2. class IfTest2 {
  3. public static void main (String[] args) {
  4. int x = 2;
  5. if (x == 3) {
  6. System.out.println(“x must be 3”);
  7. } else {
  8. System.out.println(“x is NOT 3”);
  9. }
  10. System.out.println(“This runs no matter what”);
  11. }
  12. }
  1. //从99数到0
  2. public class BeerSong {
  3. public static void main (String[] args) {
  4. int beerNum = 99;
  5. String word = bottles”;
  6. while (beerNum > 0) {
  7. if (beerNum == 1) {
  8. word = bottle”; // 单数的瓶子
  9. }
  10. System.out.println(beerNum + + word + of beer on the wall”);
  11. System.out.println(beerNum + + word + of beer.”);
  12. System.out.println(“Take one down.”);
  13. System.out.println(“Pass it around.”);
  14. beerNum = beerNum - 1;
  15. if (beerNum > 0) {
  16. System.out.println(beerNum + + word + of beer on the wall”);
  17. } else {
  18. System.out.println(“No more bottles of beer on the wall”);
  19. }//else循环结束
  20. } //while循环结束
  21. } //main方法结束
  22. } //class结束

要点

*语句以分号结束
*程序块以{}划出范围
*用名称与类型声明变量
*等号是赋值运算符
*两个等号用来当等式
*只要是真,while会一直执行块内的程序
*把bollean测试放在括号中


chapter02 对象


形象化对象
此处输入图片的描述

*对象本身已知的事物称为实例变量
*类是对象的蓝图

创建对象
此处输入图片的描述

创建与测试movie对象实现

  1. class Movie {
  2. String title;
  3. String genre;
  4. int rating;
  5. void playIt() {
  6. System.out.println(“Playing the movie”);
  7. }
  8. }
  9. public class MovieTestDrive {
  10. public static void main(String[] args) {
  11. Movie one = new Movie();
  12. one.title = Gone with the Stock”;
  13. one.genre = Tragic”;
  14. one.rating = -2;
  15. Movie two = new Movie();
  16. two.title = Lost in Cubicle Space”;
  17. two.genre = Comedy”;
  18. two.rating = 5;
  19. two.playIt();
  20. Movie three = new Movie();
  21. three.title = Byte Club”;
  22. three.genre = Tragic but ultimately uplifting”;
  23. three.rating = 127;
  24. }
  25. }

main()的真正用途

*测试真正的类
*启动你的JAVA应用程序

猜数字游戏

此处输入图片的描述

代码实现

  1. public class GuessGame {
  2. Player p1;
  3. Player p2;
  4. Player p3;
  5. //用3个实例变量表示三个player对象
  6. public void startGame() {
  7. p1 = new Player();
  8. p2 = new Player();
  9. p3 = new Player();
  10. //创建出player对象
  11. int guessp1 = 0;
  12. int guessp2 = 0;
  13. int guessp3 = 0;
  14. //声明三个变量来保存是否猜中
  15. boolean p1isRight = false;
  16. boolean p2isRight = false;
  17. boolean p3isRight = false;
  18. //声明三个变量来保存猜测的数字
  19. int targetNumber = (int) (Math.random() * 10);
  20. System.out.println(“Im thinking of a number between 0 and 9...”);
  21. //产生谜底数字
  22. while(true) {
  23. System.out.println(“Number to guess is + targetNumber);
  24. p1.guess();
  25. p2.guess();
  26. p3.guess();
  27. //调用player的guess()方法
  28. guessp1 = p1.number;
  29. System.out.println(“Player one guessed + guessp1);
  30. guessp2 = p2.number;
  31. System.out.println(“Player two guessed + guessp2);
  32. guessp3 = p3.number;
  33. System.out.println(“Player three guessed + guessp3);
  34. //取得每个player猜测的数字并列出
  35. if (guessp1 == targetNumber) {
  36. p1isRight = true;
  37. }
  38. if (guessp2 == targetNumber) {
  39. p2isRight = true;
  40. }
  41. if (guessp3 == targetNumber) {
  42. p3isRight = true;
  43. }
  44. //检查是否猜中,若猜中则去设定是否猜中的变量
  45. if (p1isRight || p2isRight || p3isRight) {
  46. //如果有一个或多个猜中
  47. System.out.println(“We have a winner!”); System.out.println(“Player one got it right? + p1isRight);
  48. System.out.println(“Player two got it right? + p2isRight);
  49. System.out.println(“Player three got it right? + p3isRight);
  50. System.out.println(“Game is over.”);
  51. break; //游戏结束终止循环
  52. } else {
  53. // 都没猜到所以要继续下去
  54. System.out.println(“Players will have to try again.”);
  55. } // if/else结束
  56. } // 循环结束
  57. } // 方法结束
  58. } // 类结束

运行猜数字游戏

  1. public class Player {
  2. int number = 0; // 要被猜的数字
  3. public void guess() {
  4. number = (int) (Math.random() * 10);
  5. System.out.println(“Im guessing “+ number);
  6. }
  7. }
  8. public class GameLauncher {
  9. public static void main (String[] args) {
  10. GuessGame game = new GuessGame();
  11. game.startGame();
  12. }
  13. }

chapter03 primitive主数据类型和引用


变量有两种

主数据类型
引用

声明变量:变量必须有类型和名称。
名称要避开保留字
此处输入图片的描述

圆点运算符

取得圆点前面的对象,然后求出该对象在圆点后面的事物

  1. myDog.bark();
  2. //名为mydog的变量引用对象上的bark()
  1. int myDog = new Dog();
  2. //对象的声明 创建 与赋值

引用范例

  1. class Dog {
  2. String name;
  3. public static void main (String[] args) {
  4. // 创建dog对象
  5. Dog dog1 = new Dog();
  6. dog1.bark();
  7. dog1.name = Bart”;
  8. //创建dog数组
  9. Dog[] myDogs = new Dog[3];
  10. // 关门放狗
  11. myDogs[0] = new Dog();
  12. myDogs[1] = new Dog();
  13. myDogs[2] = dog1;
  14. // 通过数据引用存取Dog
  15. myDogs[0].name = Fred”;
  16. myDogs[1].name = Marge”;
  17. // myDog[2]的名字
  18. System.out.print(“last dogs name is “);
  19. System.out.println(myDogs[2].name);
  20. // 逐个对Dog执行bark()
  21. int x = 0;
  22. while(x < myDogs.length) {
  23. //数组有个length变量能够返回元素的数目
  24. myDogs[x].bark();
  25. x = x + 1;
  26. }
  27. }
  28. public void bark() {
  29. System.out.println(name + says Ruff!”);
  30. }
  31. public void eat() { }
  32. public void chaseCat() { }
  33. }

要点

变量的声明必须有类型和名称
引用变量的值代表位于堆之对象的存取方法
引用变量如同遥控器,对引用变量使用圆点运算符可以如同按下遥控器按钮般的存取它的方法或者实例变量
数组一定是一个对象


chapter04 方法操作实例变量

  1. Dog d = new Dog();
  2. d.bark(3);
  3. //调用dog上的bark()方法,并传入3这个值作为此方法的参数
  4. //以int类型表示的值3会传递给bark()
  5. void bark (int numOfBarks) {
  6. //此值会传给numofbarks这个参数(int类型的变量)
  7. while (numOfBarks>0){
  8. system.out.printlin(“ruff”);
  9. numOfBarks=numOfBarks-1;
  10. //把numOfBarks当作一般的变量使用
  11. }
  12. }
  1. class Dog {
  2. int size;
  3. String name;
  4. void bark() {
  5. if (size > 60) {
  6. System.out.println(“Wooof! Wooof!”);
  7. } else if (size > 14) {
  8. System.out.println(“Ruff! Ruff!”);
  9. } else {
  10. System.out.println(“Yip! Yip!”);
  11. }
  12. }
  13. }
  1. class DogTestDrive {
  2. public static void main (String[] args){
  3. Dog one = new Dog();
  4. one.size = 70;
  5. Dog two = new Dog();
  6. two.size = 8;
  7. Dog three = new Dog();
  8. three.size = 35;
  9. one.bark();
  10. two.bark();
  11. three.bark();
  12. }
  13. }

A method uses parameters. A caller passes arguments.
You can send more than one thing to a method

  1. void go() {
  2. TestStuff t = new TestStuff();
  3. t.takeTwo(12, 34);
  4. }
  5. void takeTwo(int x, int y) {
  6. int z = x + y;
  7. System.out.println(“Total is + z);
  8. }
  9. //Calling a two-parameter method, and sending it two arguments.
  1. void go() {
  2. int foo = 7;
  3. int bar = 3; t.takeTwo(foo, bar);
  4. }
  5. void takeTwo(int x, int y) {
  6. int z = x + y;
  7. System.out.println(“Total is + z);
  8. }
  9. //You can pass variables into a method, as long as the variable type matches the parameter type.
  1. int x = 7;
  2. /*Declare an int variable and assign it the value ‘7’. The bit pattern for 7 goes into the variable named x.*/
  3. void go(int z){ }
  4. //Declare a method with an int parameter named z.
  5. foo.go(x);
  6. void go(int z){ }
  7. /*Call the go() method, passing the variable x as the argument. The bits in x are copied, and the copy lands in z.*/
  8. void go(int z){ z = 0;
  9. }
  10. /*Change the value of z inside the method. The value of x doesn’t change! The argument passed to the z parameter was only a copy of x.
  11. The method can’t change the bits that were in the calling variable x.*/
  1. class ElectricGuitar {
  2. String brand;
  3. int numOfPickups; boolean rockStarUsesIt;
  4. String getBrand() { return brand;
  5. }
  6. void setBrand(String aBrand) { brand = aBrand;
  7. }
  8. int getNumOfPickups() { return numOfPickups;
  9. }
  10. void setNumOfPickups(int num) { numOfPickups = num;
  11. }
  12. boolean getRockStarUsesIt() { return rockStarUsesIt;
  13. }
  14. void setRockStarUsesIt(boolean yesOrNo) { rockStarUsesIt = yesOrNo;
  15. }
  16. }

Encapsulating the GoodDog class

  1. class GoodDog {
  2. private int size;
  3. //Make the instance variable private.
  4. public int getSize() {
  5. return size;
  6. }
  7. public void setSize(int s) {
  8. //Make the getter and setter methods public.
  9. size = s;
  10. }
  11. void bark() {
  12. if (size > 60) {
  13. System.out.println(“Wooof! Wooof!”);
  14. } else if (size > 14) {
  15. System.out.println(“Ruff! Ruff!”);
  16. } else {
  17. System.out.println(“Yip! Yip!”);
  18. }
  19. }
  20. }
  21. class GoodDogTestDrive {
  22. public static void main (String[] args) {
  23. GoodDog one = new GoodDog();
  24. one.setSize(70);
  25. GoodDog two = new GoodDog();
  26. two.setSize(8);
  27. System.out.println(“Dog one: + one.getSize()); System.out.println(“Dog two: + two.getSize()); one.bark();
  28. two.bark();
  29. }
  30. }
  31. //Even though the methods don’t really add new functionality, the cool thing is that you can change your mind later. you can come back and make a method safer, faster, better.

How do objects in an array behave?

  1. Dog[] pets;
  2. pets = new Dog[7];
  3. //Declare and create a Dog array, to hold 7 Dog references.
  4. pets[0] = new Dog();
  5. pets[1] = new Dog();
  6. //Create two new Dog objects, and assign them to the first two array elements.
  7. pets[0].setSize(30);
  8. int x = pets[0].getSize();
  9. pets[1].setSize(8);
  10. //Call methods on the two Dog objects.

The difference between instance and local variables

Instance variables are declared inside a class but not within a method.

  1. class Horse {
  2. private double height = 15.2;
  3. private String breed;
  4. // more code...
  5. }

Local variables are declared within a method.

  1. class AddThing {
  2. int a;
  3. int b = 12;
  4. public int add() {
  5. int total = a + b;
  6. return total;
  7. }
  8. }

Local variables MUST be initialized before use!

To compare two primitives, use the == operator

  1. int a = 3;
  2. byte b = 3;
  3. if (a == b) { // true }

To see if two references are the same (which means they refer to the same object on the heap) use the == operator

  1. Foo a = new Foo();
  2. Foo b = new Foo();
  3. Foo c = a;
  4. if (a == b) { // false }
  5. if (a == c) { // true }
  6. if (b == c) { // false }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注