HeadFirst Java 1-20

创建对象
需要两个类,一个是备操作于对象的类(例如Dog、AlarmClock),另一个是用来测试该类的类
【1】编写类

class Dog {
   int size;
   String bread;
   String name; //实例变量
   void bark (){
       System.out.println("Ruff!Ruff!");
       }
}

【编写测试用的类】

class DogTextDrive {
  public static void main (String [] args) {
  //Dog测试码
  }
}

【在测试用的类中建立对象并存取对象的变量和方法】

class DogTestDrive {
  pubic static void main(String[] args){
  Dog d = new Dog();//建立Dog对象
  d.size = 40;//存取该对象的变量
  d.bark();//调用它的方法
  }
}

【创建与测试MOvie对象】

class Movie {
 String title;
 String genre;
 int rating;
 void playIt(){
   System.out.println("Playing the movie");
   }
 }

public class MovieTestDrive {
 public static void main(String[] args) {
  Movie one = new Movie();
  one.title = "Gone with the Stock";
  one.genre = "Tragic";
  one.rating = -2;
  Movie two = new Movie ();
  two.title = "Lost in Cubicle Space";
  two.genre = "Comedy";
  two.rating = 5;
  two.playIt();
  Movie tree = new Movie();
  three.title = "Byte Club";
  three.genre = "Tragiic but ultimately uplifting";
  three.rating = 127;
  }
 }

main( )的两种用途{1.测试真正的类 2.启动程序}
真正的java程序只会让对象与对象交互(相互调用),以下范例是示范对象与对象的互动

//猜数字游戏
public class **GuessGame**{
 Player p1;                           //用三个实例变量
 Player p2;
 Player p3;
public void startGame(){              //创建Player对象
 p1 = new Player ();
 p2 = new Player ();
 p3 = new Player ();

 int guessp1 = 0;                  
 int guessp2 = 0;
 int guessp3 = 0;

 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 "+targerNumber);
 p1.guess();
 p2.guess();
 p3.guess();

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 is right? "+ p3isRight);
   System.out.println("Game is over.");
   break;
   }else {
   //都没有猜到,继续下去。
     System.out.println("Players will have to try again.");
     }//else结束
    }//循环结束
   }//方法结束
  }//类结束

//运行猜数字游戏
public class Player {
 int number = 0;
 pubic void guess(){
  number = (int) (Math.random()*10);
  System.out.println("I'm guessing "+number);
  }
 }
public class GameLauncher {
    public static void main (String[] args) {
        GuessGame game = new GuessGame();
        game.startGame ();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值