java的typeinfo_有木有大大 有typeinfo.pets的包

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

/*把下面这部分复制到你的代码底端就不用import了*/

class Individual implements Comparable {

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() {}

@Override

public String toString() {

return getClass().getSimpleName() +

(name == null ? "" : " " + name);

}

public long id() { return id; }

@Override

public boolean equals(Object o) {

return o instanceof Individual &&

Objects.equals(id, ((Individual)o).id);

}

@Override

public int hashCode() {

return Objects.hash(name, id);

}

@Override

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));

}

}

class Pet extends Individual {

public Pet(String name) { super(name); }

public Pet() { super(); }

}

abstract class PetCreator {

private Random rand = new Random(47L);

public PetCreator() {

}

public abstract List> types();

public Pet randomPet() {

int n = this.rand.nextInt(this.types().size());

try {

return (Pet)((Class)this.types().get(n)).newInstance();

} catch (InstantiationException var3) {

throw new RuntimeException(var3);

} catch (IllegalAccessException var4) {

throw new RuntimeException(var4);

}

}

public Pet[] createArray(int size) {

Pet[] result = new Pet[size];

for(int i = 0; i < size; ++i) {

result[i] = this.randomPet();

}

return result;

}

public ArrayList arrayList(int size) {

ArrayList result = new ArrayList();

Collections.addAll(result, this.createArray(size));

return result;

}

}

class Dog extends Pet {

public Dog(String name) { super(name); }

public Dog() { super(); }

}

class Cat extends Pet {

public Cat(String name) { super(name); }

public Cat() { super(); }

}

class Rodent extends Pet {

public Rodent(String name) { super(name); }

public Rodent() { super(); }

}

class Mutt extends Dog {

public Mutt(String name) { super(name); }

public Mutt() { super(); }

}

class Pug extends Dog {

public Pug(String name) { super(name); }

public Pug() { super(); }

}

class EgyptianMau extends Cat {

public EgyptianMau(String name) { super(name); }

public EgyptianMau() { super(); }

}

class Manx extends Cat {

public Manx(String name) { super(name); }

public Manx() { super(); }

}

class Cymric extends Manx {

public Cymric(String name) { super(name); }

public Cymric() { super(); }

}

class Rat extends Rodent {

public Rat(String name) { super(name); }

public Rat() { super(); }

}

class Mouse extends Rodent {

public Mouse(String name) { super(name); }

public Mouse() { super(); }

}

class Hamster extends Rodent {

public Hamster(String name) { super(name); }

public Hamster() { super(); }

}

class LiteralPetCreator extends PetCreator {

// No try block needed.

@SuppressWarnings("unchecked")

public static

final List> ALL_TYPES =

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> TYPES =

ALL_TYPES.subList(ALL_TYPES.indexOf(Mutt.class),

ALL_TYPES.size());

@Override

public List> types() {

return TYPES;

}

public static void main(String[] args) {

System.out.println(TYPES);

}

}

class Pets {

public static final PetCreator creator = new LiteralPetCreator();

public Pets() {

}

public static Pet randomPet() {

return creator.randomPet();

}

public static Pet[] createArray(int size) {

return creator.createArray(size);

}

public static ArrayList arrayList(int size) {

return creator.arrayList(size);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值