【ThinkingInJava】29、对类进行计数
来源:互联网 发布:网站域名备案注册证书 编辑:程序博客网 时间:2024/06/06 17:11
<pre name="code" class="java">//: typeinfo/pets/PetCreator.java// Creates random sequences of Pets.package Lesson14TypeInformation.pets;import java.util.*;public abstract class PetCreator { private Random rand = new Random(47); // The List of the different types of Pet to create: public abstract List<Class<? extends Pet>> types(); public Pet randomPet() { // Create one random Pet int n = rand.nextInt(types().size()); try { return types().get(n).newInstance(); } catch(InstantiationException e) { throw new RuntimeException(e); } catch(IllegalAccessException e) { throw new RuntimeException(e); } } public Pet[] createArray(int size) { Pet[] result = new Pet[size]; for(int i = 0; i < size; i++) result[i] = randomPet(); return result; } public ArrayList<Pet> arrayList(int size) { ArrayList<Pet> result = new ArrayList<Pet>(); Collections.addAll(result, createArray(size)); return result; }} ///:~
//: typeinfo/pets/ForNameCreator.javapackage Lesson14TypeInformation.pets;import java.util.*;public class ForNameCreator extends PetCreator { private static List<Class<? extends Pet>> types = new ArrayList<Class<? extends Pet>>(); // Types that you want to be randomly created: private static String[] typeNames = { "Lesson14TypeInformation.pets.Mutt", "Lesson14TypeInformation.pets.Pug", "Lesson14TypeInformation.pets.EgyptianMau", "Lesson14TypeInformation.pets.Manx", "Lesson14TypeInformation.pets.Cymric", "Lesson14TypeInformation.pets.Rat", "Lesson14TypeInformation.pets.Mouse", "Lesson14TypeInformation.pets.Hamster" }; @SuppressWarnings("unchecked") private static void loader() { try { for(String name : typeNames) types.add( (Class<? extends Pet>)Class.forName(name)); } catch(ClassNotFoundException e) { throw new RuntimeException(e); } } static { loader(); } public List<Class<? extends Pet>> types() {return types;}} ///:~
/*** 书本:《Thinking In Java》* 功能:为了对pet进行计数,我们做一个能跟踪各种不同类型的Pet的数量的工具,用map* 文件:PetCount.java* 时间:2015年4月13日17:25:39* 作者:cutter_point*/package Lesson14TypeInformation;import java.util.HashMap;import Lesson14TypeInformation.pets.*;import static net.mindview.util.Print.*;public class PetCount {static class PetCounter extends HashMap<String, Integer>{public void count(String type)//添加一个pet{Integer quantity = this.get(type);if(quantity == null)//如果没有那么就变为1this.put(type, 1);else//如果有,那么就在基础上加一个this.put(type, quantity+1);}}public static void countPets(PetCreator creator){PetCounter counter = new PetCounter();for(Pet pet : creator.createArray(20)){printnb(pet.getClass().getSimpleName() + " ");if(pet instanceof Pet)//判断是不是这个类counter.count("Pet");if(pet instanceof Dog)counter.count("Dog");if(pet instanceof Mutt)counter.count("Mutt");if(pet instanceof Pug)counter.count("Pug");if(pet instanceof Cat)counter.count("Cat");if(pet instanceof Manx)counter.count("EgyptianMau");if(pet instanceof Manx)counter.count("Manx");if(pet instanceof Manx)counter.count("Cymric");if(pet instanceof Rodent)counter.count("Rodent");if(pet instanceof Rat)counter.count("Rat");if(pet instanceof Mouse)counter.count("Mouse");if(pet instanceof Hamster)counter.count("Hamster");}print();print(counter);}public static void main(String[] args) {countPets(new ForNameCreator());}}
前面的那两个帮助类是为了产生一些pet类,而pet类的格式是:
//: typeinfo/pets/Cat.javapackage Lesson14TypeInformation.pets;public class Cat extends Pet { public Cat(String name) { super(name); } public Cat() { super(); }} ///:~
输出:
Rat -obj2 Manx -obj2 Cymric -obj2 Mutt -obj2 Pug -obj2 Cymric -obj2 Pug -obj2 Manx -obj2 Cymric -obj2 Rat -obj2 EgyptianMau -obj2 Hamster -obj2 EgyptianMau -obj2 Mutt -obj2 Mutt -obj2 Cymric -obj2 Mouse -obj2 Pug -obj2 Mouse -obj2 Cymric -obj2
{Rat=2, Cymric=7, Cat=9, Pet=20, Dog=6, Manx=7, EgyptianMau=7, Pug=3, Mouse=2, Rodent=5, Hamster=1, Mutt=3} obj1
0 0
- 【ThinkingInJava】29、对类进行计数
- 【ThinkingInJava】31、对pet进行计数(3)
- 【ThinkingInJava】32、递归计数
- 创建触发器对记录变更进行计数
- ThinkingInJava
- 【ThinkingInJava】54、对List操作中本质的部分进行测试还有Queue中的操作测试性能比较
- 【Java】【TIJ】使用static实现对创建的类对象进行计数
- Python使用collections的Counter类对字符串、列表、字典的数据元素进行计数
- ThinkingInJava对this关键字的介绍
- 【Programming Pearls】对文档中的单词进行计数问题
- HTML5 实现对用户访问某一页面进行计数
- 使用SysTick的普通计数模式对延迟进行管理
- Java 使用Map对出现的字符进行计数
- Linux下对文件进行去重计数以及排序
- 【ThinkingInJava】55、对set中的操作测试性能比较
- 【ThinkingInJava】17、匿名类的工厂方法
- 对类进行封装
- 学习笔记一:如何:对某词在字符串中出现的次数进行计数 (LINQ)
- 12 hours schedule
- 每天一个linux命令参数5.17
- 黑马程序员——java编程--正则表达式
- 黑马程序员——高新技术--jdk1.5新特性
- 黑马程序员——高新技术--反射
- 【ThinkingInJava】29、对类进行计数
- 黑马程序员——高新技术--7k面试题之交通灯系统
- MySQL性能优化的21个最佳实践 和 mysql使用索引
- printStream 和printWriter区别
- 关于面向对象编程的一点思考
- Android 给listview/gridview设置加载动画(逐条加载条目动画)
- 第11周 【项目0 - 是春哥啊】
- 【ThinkingInJava】31、对pet进行计数(3)
- sql学习笔记(13)-----------sql函数总结