⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7087d035f737001b1c82868736dfcb2e

📁 java编程思想是一本非常经典的书籍 此源码包涵了书籍的所有源码 书籍源码
💻
字号:
package the10;
import java.util.*;

public class PetCount2 {
	 
	//  private static Test monitor = new Test();
	  private static Random rand = new Random();
	  public static void main(String[] args) {
	    Object[] pets = new Object[15];
	    Class[] petTypes = {
	      // Class literals:
	      Pet.class,
	      Dog.class,
	      Pug.class,
	      Cat.class,
	      Rodent.class,
	      Gerbil.class,
	      Hamster.class,
	    };
	    try {
	      for(int i = 0; i < pets.length; i++) {
	        // Offset by one to eliminate Pet.class:
	        int rnd = 1 + rand.nextInt(petTypes.length - 1);
	        pets[i] = petTypes[rnd].newInstance();
	      }
	    } catch(InstantiationException e) {
	      System.out.println("Cannot instantiate");
	      System.exit(1);
	    } catch(IllegalAccessException e) {
	      System.out.println("Cannot access");
	      System.exit(1);
	    }
	    AssociativeArray map =
	      new AssociativeArray(petTypes.length);
	    for(int i = 0; i < petTypes.length; i++)
	      map.put(petTypes[i].toString(), new Counter());
	    for(int i = 0; i < pets.length; i++) {
	      Object o = pets[i];
	      if(o instanceof Pet)
	        ((Counter)map.get("class c10.Pet")).i++;
	      if(o instanceof Dog)
	        ((Counter)map.get("class c10.Dog")).i++;
	      if(o instanceof Pug)
	        ((Counter)map.get("class c10.Pug")).i++;
	      if(o instanceof Cat)
	        ((Counter)map.get("class c10.Cat")).i++;
	      if(o instanceof Rodent)
	        ((Counter)map.get("class c10.Rodent")).i++;
	      if(o instanceof Gerbil)
	        ((Counter)map.get("class c10.Gerbil")).i++;
	      if(o instanceof Hamster)
	        ((Counter)map.get("class c10.Hamster")).i++;
	    }
	    // List each individual pet:
	    for(int i = 0; i < pets.length; i++)
	      System.out.println(pets[i].getClass());
	    // Show the counts:
	    System.out.println(map);
	 /**   monitor.expect(new Object[] {
	      new TestExpression("%% class c10\\." +
	        "(Dog|Pug|Cat|Rodent|Gerbil|Hamster)",
	        pets.length),
	      new TestExpression("%% class c10\\." +
	        "(Pet|Dog|Pug|Cat|Rodent|Gerbil|Hamster) : \\d+",
	        petTypes.length)
	    });*/
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -