Pet类
来源:互联网 发布:知乎一句话 编辑:程序博客网 时间:2024/04/30 02:04
Code1
//: typeinfo/pets/Cat.javapackage typeinfo.pets;public class Cat extends Pet { public Cat(String name) { super(name); } public Cat() { super(); }} ///:~
Code2
//: typeinfo/pets/Cymric.javapackage typeinfo.pets;public class Cymric extends Manx { public Cymric(String name) { super(name); } public Cymric() { super(); }} ///:~
Code3
//: typeinfo/pets/Dog.javapackage typeinfo.pets;public class Dog extends Pet { public Dog(String name) { super(name); } public Dog() { super(); }} ///:~
Code4
//: typeinfo/pets/EgyptianMau.javapackage typeinfo.pets;public class EgyptianMau extends Cat { public EgyptianMau(String name) { super(name); } public EgyptianMau() { super(); }} ///:~
Code5
//: typeinfo/pets/ForNameCreator.javapackage typeinfo.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 = { "typeinfo.pets.Mutt", "typeinfo.pets.Pug", "typeinfo.pets.EgyptianMau", "typeinfo.pets.Manx", "typeinfo.pets.Cymric", "typeinfo.pets.Rat", "typeinfo.pets.Mouse", "typeinfo.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;}} ///:~
Code6
//: typeinfo/pets/Hamster.javapackage typeinfo.pets;public class Hamster extends Rodent { public Hamster(String name) { super(name); } public Hamster() { super(); }} ///:~
Code7
//: typeinfo/pets/Individual.javapackage typeinfo.pets;public class Individual implements Comparable<Individual> { private static long counter = 0; private final long id = counter++; private String name; public Individual(String name) { this.name = name; } // 'name' is optional: public Individual() {} public String toString() { return getClass().getSimpleName() + (name == null ? "" : " " + name); } public long id() { return id; } public boolean equals(Object o) { return o instanceof Individual && id == ((Individual)o).id; } public int hashCode() { int result = 17; if(name != null) result = 37 * result + name.hashCode(); result = 37 * result + (int)id; return result; } public int compareTo(Individual arg) { // Compare by class name first: String first = getClass().getSimpleName(); String argFirst = arg.getClass().getSimpleName(); int firstCompare = first.compareTo(argFirst); if(firstCompare != 0) return firstCompare; if(name != null && arg.name != null) { int secondCompare = name.compareTo(arg.name); if(secondCompare != 0) return secondCompare; } return (arg.id < id ? -1 : (arg.id == id ? 0 : 1)); }} ///:~
Code8
//: typeinfo/pets/LiteralPetCreator.java// Using class literals.package typeinfo.pets;import java.util.*;public class LiteralPetCreator extends PetCreator { // No try block needed. @SuppressWarnings("unchecked") public static final List<Class<? extends Pet>> allTypes = Collections.unmodifiableList(Arrays.asList( Pet.class, Dog.class, Cat.class, Rodent.class, Mutt.class, Pug.class, EgyptianMau.class, Manx.class, Cymric.class, Rat.class, Mouse.class,Hamster.class)); // Types for random creation: private static final List<Class<? extends Pet>> types = allTypes.subList(allTypes.indexOf(Mutt.class), allTypes.size()); public List<Class<? extends Pet>> types() { return types; } public static void main(String[] args) { System.out.println(types); }} /* Output:[class typeinfo.pets.Mutt, class typeinfo.pets.Pug, class typeinfo.pets.EgyptianMau, class typeinfo.pets.Manx, class typeinfo.pets.Cymric, class typeinfo.pets.Rat, class typeinfo.pets.Mouse, class typeinfo.pets.Hamster]*///:~
Code9
//: typeinfo/pets/Manx.javapackage typeinfo.pets;public class Manx extends Cat { public Manx(String name) { super(name); } public Manx() { super(); }} ///:~
Code10
//: typeinfo/pets/Mouse.javapackage typeinfo.pets;public class Mouse extends Rodent { public Mouse(String name) { super(name); } public Mouse() { super(); }} ///:~
Code11
//: typeinfo/pets/Mutt.javapackage typeinfo.pets;public class Mutt extends Dog { public Mutt(String name) { super(name); } public Mutt() { super(); }} ///:~
Code12
//: typeinfo/pets/Person.javapackage typeinfo.pets;public class Person extends Individual { public Person(String name) { super(name); }} ///:~
Code13
//: typeinfo/pets/Pet.javapackage typeinfo.pets;public class Pet extends Individual { public Pet(String name) { super(name); } public Pet() { super(); }} ///:~
Code14
//: typeinfo/pets/PetCreator.java// Creates random sequences of Pets.package typeinfo.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; }} ///:~
Code15
//: typeinfo/pets/Pets.java// Facade to produce a default PetCreator.package typeinfo.pets;import java.util.*;public class Pets { public static final PetCreator creator = new LiteralPetCreator(); public static Pet randomPet() { return creator.randomPet(); } public static Pet[] createArray(int size) { return creator.createArray(size); } public static ArrayList<Pet> arrayList(int size) { return creator.arrayList(size); }} ///:~
Code16
//: typeinfo/pets/Pug.javapackage typeinfo.pets;public class Pug extends Dog { public Pug(String name) { super(name); } public Pug() { super(); }} ///:~
Code17
//: typeinfo/pets/Rat.javapackage typeinfo.pets;public class Rat extends Rodent { public Rat(String name) { super(name); } public Rat() { super(); }} ///:~
Code18
//: typeinfo/pets/Rodent.javapackage typeinfo.pets;public class Rodent extends Pet { public Rodent(String name) { super(name); } public Rodent() { super(); }} ///:~
0 0
- Pet类
- Pet
- Pet
- Pet
- Pet
- PET
- PET
- 定义一个Pet类,定义Cat,Dog,为子类继承Pet类。
- PET模型
- pet sqlhelper
- pet shop
- PET基础
- DOMText(pet)
- SBT(pet)
- hdu4707 Pet
- hd4707 Pet
- Pet--hdu4707
- hdu4707 Pet
- 求正整数 n 所有可能的和式的组合
- android多语言切换
- 六、Linux/UNIX操作命令积累【kill、netstat、df、du】
- Java虚拟机ClassLoader知识详解(2)
- Spring中引入properties文件
- Pet类
- hihoCode 第49周 欧拉路 的C实现方法
- 设计模式——单例模式
- LeetCode 13:"Roman to Integer"
- [CodeForces445A]DZY Loves Chessboard[bfs]
- Add Binary
- Uncaught Error: Bootstrap's JavaScript requires jQuery
- POJ - 3692 Kindergarten 二分图 最大匹配
- 微分方程(1)---微分方程的数值解法