14章类型信息-之类型转换前先做检查--之使用类字面常量--类名.class--以及动态instanceof(isInstance方法)----递归计数(计算各个类的个数)

实例代码:

实体类父类:

//: typeinfo/pets/Individual.java
package 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));
  }
} ///:~

继承类:

//: typeinfo/pets/Cat.java
package typeinfo.pets;

public class Cat extends Pet {
  public Cat(String name) { super(name); }
  public Cat() { super(); }
} ///:~

//: typeinfo/pets/Dog.java
package typeinfo.pets;

public class Dog extends Pet {
  public Dog(String name) { super(name); }
  public Dog() { super(); }
} ///:~

//: typeinfo/pets/Rodent.java
package typeinfo.pets;

public class Rodent extends Pet {
  public Rodent(String name) { super(name); }
  public Rodent() { super(); }
} ///:~

创建宠物的抽象类:

//: 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;
  }
} ///:~

实现(创建宠物抽象类)的实现类:

//: 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>> types  =
    Collections.unmodifiableList(Arrays.asList(
     Dog.class, Cat.class,  Rodent.class));//这里的Collections.unmodifiableList是指

//返回指定列表的不可修改视图。此方法允许模块为用户提供对内部列表的“只读”访问
  public List<Class<? extends Pet>> types() {
    return types;
  }
}

为了将LiteralPetCreator作为默认实现,创建一个使用了LiteralPetCreator的外观类:

//: 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);
  }
} ///:~

用于计算各个宠物个数的类:

package typeinfo.pets;

//: typeinfo/PetCount.java
// Using instanceof.
import typeinfo.pets.*;
import java.util.*;
import static net.mindview.util.Print.*;

public class PetCount {
  static class PetCounter extends HashMap<String,Integer> {
    public void count(String type) {
      Integer quantity = get(type);
      if(quantity == null)
        put(type, 1);
      else
        put(type, quantity + 1);
    }
  }
  public static void countPets(PetCreator creator) {
    PetCounter counter= new PetCounter();
    for(Pet pet : creator.createArray(20)) {
      // List each individual pet:
      printnb(pet.getClass().getSimpleName() + " ");
      if(pet instanceof Pet)
        counter.count("Pet");
      if(pet instanceof Dog)
        counter.count("Dog");
      if(pet instanceof Cat)
        counter.count("Cat");
      if(pet instanceof Rodent)
        counter.count("Rodent");
    }
    // Show the counts:
    print();
    print(counter);
  }
}

测试类:

package typeinfo.pets;

//: typeinfo/PetCount2.java
import typeinfo.pets.*;

public class PetCount2 {
  public static void main(String[] args) {
    PetCount.countPets(Pets.creator);
  }
} /* (Execute to see output) *///:~

//测试打印:Rodent Rodent Dog Rodent Dog Rodent Dog Rodent Cat Dog Cat Cat Cat Dog Rodent Dog Dog Dog Rodent Dog 
//{Cat=4, Pet=20, Dog=9, Rodent=7}

工具类:

//: net/mindview/util/Print.java
// Print methods that can be used without
// qualifiers, using Java SE5 static imports:
package net.mindview.util;
import java.io.*;

public class Print {
  // Print with a newline:
  public static void print(Object obj) {
    System.out.println(obj);
  }
  // Print a newline by itself:
  public static void print() {
    System.out.println();
  }
  // Print with no line break:
  public static void printnb(Object obj) {
    System.out.print(obj);
  }
  // The new Java SE5 printf() (from C):
  public static PrintStream
  printf(String format, Object... args) {
    return System.out.printf(format, args);
  }
} ///:~

---------------------动态的instanceof------------------isInstance方法,

-----用到上面的LiteralPetCreator、Pets类-------------------------------

动态计算各个宠物个数的类:

package typeinfo.pets;

//: typeinfo/PetCount3.java
// Using isInstance()
import typeinfo.pets.*;
import java.util.*;
import net.mindview.util.*;
import static net.mindview.util.Print.*;

public class PetCount3 {
  static class PetCounter
  extends LinkedHashMap<Class<? extends Pet>,Integer> {
    public PetCounter() {
      super(MapData.map(LiteralPetCreator.types, 0));
    }
    public void count(Pet pet) {
      // Class.isInstance() eliminates instanceofs:
      for(Map.Entry<Class<? extends Pet>,Integer> pair
          : entrySet())
        if(pair.getKey().isInstance(pet))
          put(pair.getKey(), pair.getValue() + 1);
    }
    public String toString() {
      StringBuilder result = new StringBuilder("{");
      for(Map.Entry<Class<? extends Pet>,Integer> pair
          : entrySet()) {
        result.append(pair.getKey().getSimpleName());
        result.append("=");
        result.append(pair.getValue());
        result.append(", ");
      }
      result.delete(result.length()-2, result.length());
      result.append("}");
      return result.toString();
    }
  }
  //这个是测试结果的main方法

public static void main(String[] args) {
    PetCounter petCount = new PetCounter();
    for(Pet pet : Pets.createArray(20)) {
      printnb(pet.getClass().getSimpleName() + " ");
      petCount.count(pet);
    }
    print();
    print(petCount);
  }
}

//打印结果:Rodent Rodent Cat Rodent Cat Rodent Cat Rodent Dog Cat Dog Dog Dog Cat Rodent Cat Cat Cat Rodent Cat {Dog=4, Cat=9, Rodent=7}

工具类:

1、

//: net/mindview/util/MapData.java
// A Map filled with data using a generator object.
package net.mindview.util;
import java.util.*;

public class MapData<K,V> extends LinkedHashMap<K,V> {
  // A single Pair Generator:
  public MapData(Generator<Pair<K,V>> gen, int quantity) {
    for(int i = 0; i < quantity; i++) {
      Pair<K,V> p = gen.next();
      put(p.key, p.value);
    }
  }
  // Two separate Generators:
  public MapData(Generator<K> genK, Generator<V> genV,
      int quantity) {
    for(int i = 0; i < quantity; i++) {
      put(genK.next(), genV.next());
    }
  }
  // A key Generator and a single value:
  public MapData(Generator<K> genK, V value, int quantity){
    for(int i = 0; i < quantity; i++) {
      put(genK.next(), value);
    }
  }
  // An Iterable and a value Generator:
  public MapData(Iterable<K> genK, Generator<V> genV) {
    for(K key : genK) {
      put(key, genV.next());
    }
  }
  // An Iterable and a single value:
  public MapData(Iterable<K> genK, V value) {
    for(K key : genK) {
      put(key, value);
    }
  }
  // Generic convenience methods:
  public static <K,V> MapData<K,V>
  map(Generator<Pair<K,V>> gen, int quantity) {
    return new MapData<K,V>(gen, quantity);
  }
  public static <K,V> MapData<K,V>
  map(Generator<K> genK, Generator<V> genV, int quantity) {
    return new MapData<K,V>(genK, genV, quantity);
  }
  public static <K,V> MapData<K,V>
  map(Generator<K> genK, V value, int quantity) {
    return new MapData<K,V>(genK, value, quantity);
  }
  public static <K,V> MapData<K,V>
  map(Iterable<K> genK, Generator<V> genV) {
    return new MapData<K,V>(genK, genV);
  }
  public static <K,V> MapData<K,V>
  map(Iterable<K> genK, V value) {
    return new MapData<K,V>(genK, value);
  }
} ///:~

2、

//: net/mindview/util/Generator.java
// A generic interface.
package net.mindview.util;
public interface Generator<T> { T next(); } ///:~

3、

//: net/mindview/util/Pair.java
package net.mindview.util;

public class Pair<K,V> {
  public final K key;
  public final V value;
  public Pair(K k, V v) {
    key = k;
    value = v;
  }
} ///:~

--------------------------------递归计数-----------------------

------------------------不局限于pet的工具类-------------------------

工具类:

//: net/mindview/util/TypeCounter.java
// Counts instances of a type family.
package net.mindview.util;
import java.util.*;

public class TypeCounter extends HashMap<Class<?>,Integer>{
  private Class<?> baseType;
  public TypeCounter(Class<?> baseType) {
    this.baseType = baseType;
  }
  public void count(Object obj) {
    Class<?> type = obj.getClass();
    if(!baseType.isAssignableFrom(type))
      throw new RuntimeException(obj + " incorrect type: "
        + type + ", should be type or subtype of "
        + baseType);
    countClass(type);
  }
  private void countClass(Class<?> type) {
    Integer quantity = get(type);
    put(type, quantity == null ? 1 : quantity + 1);
    Class<?> superClass = type.getSuperclass();
    if(superClass != null &&
       baseType.isAssignableFrom(superClass))//判定此 Class 对象所表示的类或接口与指定的 Class 参数

//所表示的类或接口是否相同,或是否是其超类或超接口。如果是则返回 true;否则返回 false
      countClass(superClass);
  }
  public String toString() {
    StringBuilder result = new StringBuilder("{");
    for(Map.Entry<Class<?>,Integer> pair : entrySet()) {
      result.append(pair.getKey().getSimpleName());
      result.append("=");
      result.append(pair.getValue());
      result.append(", ");
    }
    result.delete(result.length()-2, result.length());
    result.append("}");
    return result.toString();
  }
} ///:~

测试类:

package typeinfo.pets;

//: typeinfo/PetCount4.java
import typeinfo.pets.*;
import net.mindview.util.*;
import static net.mindview.util.Print.*;

public class PetCount4 {
  public static void main(String[] args) {
    TypeCounter counter = new TypeCounter(Pet.class);
    for(Pet pet : Pets.createArray(20)) {
      printnb(pet.getClass().getSimpleName() + " ");
      counter.count(pet);
    }
    print();
    print(counter);
  }
}

打印结果:

//Rodent Rodent Cat Rodent Cat Rodent Cat Rodent Dog Cat Dog Dog Dog Cat Rodent Cat Cat Cat Rodent Cat 
{Pet=20, Cat=9, Dog=4, Rodent=7}

转载于:https://blog.51cto.com/qa962839575/1587143

14章类型信息之使用类字面常量相关推荐

  1. 【笔记】《Java编程思想(第四版)》第14章-类型信息

    第14章 类型信息 RTTI(Run-Time Type Identification)运行阶段类型识别 运行时类型信息使得你可以在程序运行时发现和适用类型信息. 一种是"传统的" ...

  2. Think in Java第四版 读书笔记8第14章 类型信息(RTTI与反射)

    Java如何在运行时识别对象和类的信息? 1.RTTI(Run-time type information) 它假定我们在编译时已经知道了所有类型 2.反射 它允许我们在运行时发现和使用类的信息 14 ...

  3. thinking-in-java(14)类型信息

    [0]开场白 1)运行时类型信息使得你可以在程序运行时发现和使用类型信息: 2)java是如何在运行时识别对象和类信息的?两种方式: 方式1)传统的RTTI(RunTime Type Identifi ...

  4. 《Java编程思想》第四版读书笔记 第十四章 类型信息

    2019独角兽企业重金招聘Python工程师标准>>> 14.2 RTTI运行时类型识别. Class对象包含了与类有关的信息,Java使用Class对象来执行其RTTI.每个类都有 ...

  5. 《快学Scala》第14章——模式匹配和样例类 学习笔记

    Scala有一个十分强大的模式匹配机制,可以应用在很多场合:switch语句.类型查询,以及"析构"(获取复杂表达式中的不同部分).除此之外,Scala还提供了样例类,对模式匹配进 ...

  6. 第14章:信息文档与配置管理和知识与流程管理

    一.信息文档与配置管理 1.信息文档 概念:①某种数据媒体和其中所记录的数据 ②在软件工程中,文档常常用来表示对活动.需求过程或结果,进行描述.定义.规定.报告或认证的任何书面或图示的信息(包括纸质文 ...

  7. java编程思想第四版第十四章 类型信息习题

    fda dfa 第三题u package net.mindview.typeinfo.test4;import java.util.ArrayList; import java.util.Arrays ...

  8. Java编程思想--14类型信息

    第十四章类型信息 14.1 为什么需要RTTI 14.2 Class对象 Class对象 Class.forName(String s) 14.2.1字面类常量 14.2.2 泛化Class的引用 1 ...

  9. thinking in java 学习笔记 14 类型信息

    第十四章 类型信息 尼玛,刚刚看完了亚冠,恒大这样都被连扳3球,尼玛的垃圾孙祥,恨死了那个全北现代 好吧,回到学习上 运行时类型信息使得你可以再程序运行时发现和使用类型信息 本章讨论的是java如何让 ...

最新文章

  1. 用Xamarin.Forms创建移动应用程序
  2. ObjectInputStreamObjectOutputStream工具类
  3. 基于ARM A53开发板,使用按键中断及中断底半部实现《led灯状态取反》的驱动
  4. pprof 的原理与实现
  5. 求一个数组的最长递减子序列 比如{9,4,3,2,5,4,3,2}的最长递减子序列为{9,5,4,3,2}...
  6. CMMI入门 - 通用实践的实施GP 2.1-GP 2.5
  7. Android Studio实现前后台分离的选课系统
  8. 宜信正式开源其 AIOps 落地三大利器
  9. 做个vbs整人小程序
  10. 自定义一个腾讯云短信验证码接口spring-starter
  11. windows双开微信多开微信
  12. RS-485总线,这篇很详细
  13. 22年字节跳动飞书人力套件二面面经
  14. CDH 6系列(CDH 6.0.0、CHD 6.1.0等)安装和使用
  15. 仿淘宝天猫双11下拉倒计时领取豆子
  16. 微信小程序之网易云音乐小案例
  17. vimdiff比较两个文件
  18. 名编辑电子杂志大师教程 | 阅读量浏览量统计
  19. Neat Converter格式转换
  20. 图纸上标注的是实际尺寸吗_施工图纸上尺寸标注的标准是什么?

热门文章

  1. Canvas的绚烂起点
  2. 简说设计模式——组合模式
  3. RabbitMQ消息队列之一:RabbitMQ的环境安装及配置
  4. 万达9.3亿美元并购北欧最大院线 全球市场份额逼近20%
  5. Perfect:Swift 语言服务器端软件框架简介
  6. linux安装oracle
  7. Java:处理PDF
  8. jquery css位置offset()
  9. Xml之Linq遍历
  10. 分享30个网页设计色彩搭配的优秀案例