1. fda
  2. dfa
  3. 第三题u
    package net.mindview.typeinfo.test4;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List; abstract class Shape {
    void draw(){
    /*
    * 重点在这里:
    * this指代的是实例对象,由于使用+连接字符串, 会自动调用对象的toString()方法.
    */
    System.out.println(this + ".draw()");
    } public abstract String toString();
    } class Circle extends Shape{
    @Override
    public String toString() {
    return "Circle";
    }
    } class Square extends Shape{
    @Override
    public String toString() {
    return "Square";
    }
    } class Triangle extends Shape{
    @Override
    public String toString() {
    return "Triangle";
    }
    } //菱形
    class Rhomboid extends Shape{
    @Override
    public String toString() {
    return "Rhomboid";
    }
    } public class Shapes { public static void main(String[] args) {
    List<Shape> shapes = new ArrayList<Shape>(Arrays.asList(
    new Circle(), new Square(), new Triangle(), new Rhomboid())); for(Shape shape:shapes){
    shape.draw();
    } for(int i=;i<shapes.size(); i++){
    Shape shape = shapes.get(i);
    if(i == ){
    Rhomboid r = (Rhomboid)shape;
    Circle c = (Circle)shape;
    }
    }
    }
    }

    运行结果:

    Circle.draw()
    Square.draw()
    Exception in thread "main" Triangle.draw()
    Rhomboid.draw()
    java.lang.ClassCastException: net.mindview.typeinfo.test4.Rhomboid cannot be cast to net.mindview.typeinfo.test4.Circle
    at net.mindview.typeinfo.test4.Shapes.main(Shapes.java:63)

    不可以向下转型到Circle

  4. 第四题

    package net.mindview.typeinfo.test4;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List; abstract class Shape {
    void draw(){
    /*
    * 重点在这里:
    * this指代的是实例对象,由于使用+连接字符串, 会自动调用对象的toString()方法.
    */
    System.out.println(this + ".draw()");
    } public abstract String toString();
    } class Circle extends Shape{
    @Override
    public String toString() {
    return "Circle";
    }
    } class Square extends Shape{
    @Override
    public String toString() {
    return "Square";
    }
    } class Triangle extends Shape{
    @Override
    public String toString() {
    return "Triangle";
    }
    } //菱形
    class Rhomboid extends Shape{
    @Override
    public String toString() {
    return "Rhomboid";
    }
    } public class Shapes {
    public static void main(String[] args) {
    List<Shape> shapes = new ArrayList<Shape>(Arrays.asList(
    new Circle(), new Square(), new Triangle(), new Rhomboid())); for(Shape shape:shapes){
    shape.draw();
    } for(int i=;i<shapes.size(); i++){
    Shape shape = shapes.get(i);
    if(i == ){
    //添加instanceof判断
    if (shape instanceof Rhomboid){
    Rhomboid r = (Rhomboid)shape;
    }
    if(shape instanceof Circle){
    Circle c = (Circle)shape;
    }
    }
    }
    }
    }

    使用Class.newInstance方法,必须有一个无参构造方法

  5. 第五题:
    package net.mindview.typeinfo;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List; abstract class Shape {
    void draw(){
    /*
    * 重点在这里:
    * this指代的是实例对象,由于使用+连接字符串, 会自动调用对象的toString()方法.
    */
    System.out.println(this + ".draw()");
    } void rotate(){
    Class<? extends Shape> clazz = this.getClass();
    if(!clazz.getSimpleName().equals("Circle")){
    System.out.println("旋转"+ this);
    }else{
    System.out.println(this+"不需要旋转");
    }
    } public abstract String toString();
    } class Circle extends Shape{
    @Override
    public String toString() {
    return "Circle";
    }
    } class Square extends Shape{
    @Override
    public String toString() {
    return "Square";
    }
    } class Triangle extends Shape{
    @Override
    public String toString() {
    return "Triangle";
    }
    } //菱形
    class Rhomboid extends Shape{
    @Override
    public String toString() {
    return "Rhomboid";
    }
    } public class Shapes { public static void main(String[] args) {
    List<Shape> shapes = new ArrayList<Shape>(Arrays.asList(
    new Circle(), new Square(), new Triangle(), new Rhomboid())); for(Shape shape:shapes){
    shape.draw();
    shape.rotate();
    } /*for(int i=0;i<shapes.size(); i++){
    Shape shape = shapes.get(i);
    if(i == 3){
    Rhomboid r = (Rhomboid)shape;
    Circle c = (Circle)shape;
    }
    }*/
    }
    }
  6. 第七题
    package net.mindview.typeinfo.test8;
    
    import java.util.HashSet;
    import java.util.Set; interface A {}
    interface B {}
    interface C {} class D implements B{
    private int di=;
    private String dname="";
    static{
    System.out.println("this is D"); }
    } class E extends D implements A, B, C{
    private int ei=;
    private String ename="";
    static{
    System.out.println("this is E"); }
    } class F extends E implements A {
    private int fi=;
    private String fname="";
    static{
    System.out.println("this is F"); }
    }
    /**
    * 接受任意对象作为参数, 递归打印出该对象继承体系中所有的的类.
    * 包括继承的父类, 和实现的接口
    *
    * 分析: 一个类智能继承一个父类, 可以实现多个接口.
    * 父类可以继承一个类,实现多个接口. 实现的接口可能和子类重复, 因此需要去重.
    * 递归实现
    * @author samsung
    *
    */
    class G {
    public void printSuperClass(Class c){
    if(c == null) return ; System.out.println(c.getName()); //得到这个类的接口
    Class[] interfaces = c.getInterfaces(); for(Class interfaceClass:interfaces){
    printSuperClass(interfaceClass);
    }
    printSuperClass(c.getSuperclass());
    }
    } public class Test8 {
    public static void main(String[] args) {
    G g = new G();
    g.printSuperClass(F.class);
    }
    }

    运行结果:

    net.mindview.typeinfo.test8.F
    net.mindview.typeinfo.test8.A
    net.mindview.typeinfo.test8.E
    net.mindview.typeinfo.test8.A
    net.mindview.typeinfo.test8.B
    net.mindview.typeinfo.test8.C
    net.mindview.typeinfo.test8.D
    net.mindview.typeinfo.test8.B
    java.lang.Object
  7. 第八题
    package net.mindview.typeinfo.test8;
    
    interface A {}
    interface B {}
    interface C {} class D {
    static{
    System.out.println("this is D"); }
    } class E extends D implements A, B, C{
    static{
    System.out.println("this is E"); }
    } class F extends E {
    static{
    System.out.println("this is F"); }
    } class G {
    public void printSuperClass(Class c){
    Class upClass = c.getSuperclass(); try {
    System.out.println(upClass.newInstance());
    } catch (InstantiationException e) {
    System.out.println("实例化Instance 失败");
    System.exit();
    } catch (IllegalAccessException e) {
    System.out.println("实例化Instance 失败");
    System.exit();
    }
    if(upClass.getSuperclass() != null ){
    printSuperClass(upClass);
    }
    }
    } public class Test8 {
    public static void main(String[] args) {
    G g = new G();
    g.printSuperClass(F.class);
    }
    }

    运行结果:

    this is D
    this is E
    net.mindview.typeinfo.test8.E@17cb0a16
    net.mindview.typeinfo.test8.D@1303368e
    java.lang.Object@37f2ae62
  8. 第九题
    package net.mindview.typeinfo.test9;
    
    import java.lang.reflect.Field;
    
    interface A {}
    interface B {}
    interface C {} class D {
    public int i=;
    public String name="";
    static{
    System.out.println("this is D"); }
    } class E extends D implements A, B, C{
    public int i=;
    public String name="";
    static{
    System.out.println("this is E"); }
    } class F extends E {
    public int i=;
    public String name="";
    static{
    System.out.println("this is F"); }
    } class G {
    public void printSuperClass(Class c){
    Class upClass = c.getSuperclass();
    try {
    //获取类中的字段
    Field[] fs = upClass.getDeclaredFields();
    System.out.println(fs.length);
    for(Field f:fs){
    //打印字段名
    System.out.println(f.getName());
    //获取字段值
    Object value = upClass.getDeclaredField(f.getName());
    System.out.println(value);
    }
    } catch (Exception e1) {
    e1.printStackTrace();
    } try {
    System.out.println(upClass.newInstance());
    } catch (InstantiationException e) {
    System.out.println("实例化Instance 失败");
    System.exit();
    } catch (IllegalAccessException e) {
    System.out.println("实例化Instance 失败");
    System.exit();
    }
    if(upClass.getSuperclass() != null ){
    printSuperClass(upClass);
    }
    }
    } public class Test9 {
    public static void main(String[] args) {
    G g = new G();
    g.printSuperClass(F.class);
    }
    }

    运行结果:

    i
    public int net.mindview.typeinfo.test9.E.i
    name
    public java.lang.String net.mindview.typeinfo.test9.E.name
    this is D
    this is E
    net.mindview.typeinfo.test9.E@1440578d i
    public int net.mindview.typeinfo.test9.D.i
    name
    public java.lang.String net.mindview.typeinfo.test9.D.name
    net.mindview.typeinfo.test9.D@26f04d94 java.lang.Object@38a3c5b6
  9. 第十题
    package net.mindview.typeinfo.test10;
    class Pot{}
    public class Test10 {
    public static void judgeType(Object c){
    Class arrType = c.getClass().getComponentType();
    System.out.println(arrType.getName());
    }
    public static void main(String[] args) {
    judgeType(new char[]);
    judgeType(new String[]);
    judgeType(new long[]);
    judgeType(new boolean[]);
    judgeType(new Pot[]); } }

    运行结果:

    char
    java.lang.String
    long
    boolean
    net.mindview.typeinfo.test10.Pot
  10. f
  11. asf
  12. a
  13. 第十四题
    package net.mindview.typeinfo.test14;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random; import net.mindview.typeinfo.factory.Factory; /**
    * 部件类
    *
    * 比如: 有过滤器部件, 皮带部件等.
    * 空气净化器中需要有过滤器部件, 因此要有一个制造空气净化器的过滤器的工厂
    * 汽车尾气净化器也需要有过滤器部件, 因此需要一个制造汽车尾气的过滤器的工厂
    *
    * 皮带
    * 车轮需要皮带, 因此需要一个制造车轮的皮带的工厂
    * @author samsung
    *
    */
    class Part{
    @Override
    public String toString() {
    return this.getClass().getSimpleName();
    } //目前已注册的部件工厂
    static List<String> partNames = Arrays.asList(
    "net.mindview.typeinfo.test14.FuelFilter",
    "net.mindview.typeinfo.test14.AirFilter",
    "net.mindview.typeinfo.test14.CabinFilter",
    "net.mindview.typeinfo.test14.OilFilter",
    "net.mindview.typeinfo.test14.FanBelt",
    "net.mindview.typeinfo.test14.GeneratorBelt",
    "net.mindview.typeinfo.test14.PowerSteeringBelt" );
    static{ } private static Random rand = new Random(); //随机得到一个已注册的部件工厂, 并制造部件
    public static Part createRandom(){
    int n = rand.nextInt(partNames.size());
    try {
    return (Part) Class.forName(partNames.get(n)).newInstance();
    } catch (InstantiationException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    return null;
    }
    } //过滤器部件
    class Filter extends Part { } //燃料过滤器部件
    class FuelFilter extends Filter {
    public static class Factory implements net.mindview.typeinfo.factory.Factory<FuelFilter>{ @Override
    public FuelFilter create() {
    return new FuelFilter();
    }
    }
    } //空气净化器部件
    class AirFilter extends Filter {
    public static class Factory implements net.mindview.typeinfo.factory.Factory<AirFilter>{
    @Override
    public AirFilter create() {
    return new AirFilter();
    }
    }
    } //机舱空气过滤器部件
    class CabinFilter extends Filter {
    public static class Factory implements net.mindview.typeinfo.factory.Factory<CabinFilter>{
    @Override
    public CabinFilter create() {
    return new CabinFilter();
    }
    }
    } //燃油过滤器部件
    class OilFilter extends Filter {
    public static class Factory implements net.mindview.typeinfo.factory.Factory<OilFilter>{
    @Override
    public OilFilter create() {
    return new OilFilter();
    }
    }
    } //皮带部件
    class Belt extends Part{} //风扇皮带部件
    class FanBelt extends Belt {
    public static class Factory implements net.mindview.typeinfo.factory.Factory<FanBelt>{
    @Override
    public FanBelt create() {
    return new FanBelt();
    }
    }
    } //发动机皮带部件
    class GeneratorBelt extends Belt {
    public static class Factory implements net.mindview.typeinfo.factory.Factory<GeneratorBelt>{
    @Override
    public GeneratorBelt create() {
    return new GeneratorBelt();
    }
    }
    } //转向动力装置皮带部件
    class PowerSteeringBelt extends Belt {
    public static class Factory implements net.mindview.typeinfo.factory.Factory<PowerSteeringBelt>{
    @Override
    public PowerSteeringBelt create() {
    return new PowerSteeringBelt();
    }
    }
    } /**
    * 查询目前已注册的工厂类
    * @author samsung
    *
    */
    public class RegisteredFactories { public static void main(String[] args) {
    for(int i=;i<;i++){
    System.out.println(Part.createRandom()); } } }
  14. 第十五题: 内容太多, 直接参考demo
  15. af
  16. a
  17. 第十八题:
    package net.mindview.typeinfo;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import java.util.regex.Pattern; public class ShowMethods {
    private ShowMethods(){}
    private static String usage = ""
    + "usage:\n"
    + "ShowMethods qualified.class.name\n"
    + "To show all methods in class or:\n"
    + "ShowMethods qualified.class.name. word\n"
    + "To search for methodds invoiving 'word'"; private static Pattern p = Pattern.compile("\\w+\\.");
    public static void main(String[] args) {
    if(args.length<){
    System.out.println(usage);
    System.exit();
    } int lines = ;
    try {
    Class<?> c = Class.forName(args[]);
    //getMethods获取的是整个继承树中所有的方法
    Method[] methods = c.getMethods();
    //获取已有的构造器
    Constructor[] ctors = c.getConstructors(); if(args.length == ){
    //打印所有继承树中的方法名
    for(Method method: methods){
    System.out.println(p.matcher(method.toString()).replaceAll(""));
    }
    //打印全部构造器
    for(Constructor ctor: ctors){
    System.out.println(p.matcher(ctor.toString()).replaceAll(""));
    }
    lines = methods.length + ctors.length;
    }else {
    //打印指定类中的方法
    for(Method method: methods){
    if(method.toString().indexOf(args[]) != -){
    System.out.println(p.matcher(method.toString()).replaceAll(""));
    lines++;
    }
    }
    //打印构造器
    for(Constructor ctor :ctors){
    if(ctor.toString().indexOf(args[])!=-){
    System.out.println(p.matcher(ctor.toString()).replaceAll(""));
    lines++;
    }
    }
    } } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    } }
  18. af
  19. a
  20. f
  21. af
  22. a
  23. fa
  24. f

java编程思想第四版第十四章 类型信息习题的更多相关文章

  1. Java编程思想(第4版) 中文清晰PDF完整版

    Java编程思想(第4版) 中文清晰PDF完整版 [日期:2014-08-11] 来源:Linux社区  作者:Linux [字体:大 中 小]     <Java编程思想>这本书赢得了全 ...

  2. 20190908 On Java8 第十九章 类型信息

    第十九章 类型信息 RTTI(RunTime Type Information,运行时类型信息)能够在程序运行时发现和使用类型信息. Java 主要有两种方式在运行时识别对象和类信息: "传 ...

  3. 关于 java编程思想第五版 《On Java 8》

    On Java 8中文版 英雄召集令 这是该项目的GITHUB地址:https://github.com/LingCoder/OnJava8 广招天下英雄,为开源奉献!让我们一起来完成这本书的翻译吧! ...

  4. java编程思想第四版第十四章 类型信息总结

    1. Class 对象: 所有的类都是在对其第一次使用的时候,动态加载到JVM中的.当程序创建第一个对类的静态成员的引用时,就会加载这个类.这说明构造器也是类的静态方法.即使在构造器之前并没有stat ...

  5. Java编程思想 4th 第2章 一切都是对象

    Java是基于C++的,但Java是一种更纯粹的面向对象程序设计语言,和C++不同的是,Java只支持面向对象编程,因此Java的编程风格也是纯OOP风格的,即一切都是类,所有事情通过类对象协作来完成 ...

  6. 重新精读《Java 编程思想》系列之组合与继承

    Java 复用代码的两种方式组合与继承. 组合 组合只需将对象引用置于新类中即可. 比如我们有一个B类,它具有一个say方法,我们在A类中使用B类的方法,就是组合. public class B { ...

  7. JAVA编程思想(第四版)学习笔记----4.8 switch(知识点已更新)

    switch语句和if-else语句不同,switch语句可以有多个可能的执行路径.在第四版java编程思想介绍switch语句的语法格式时写到: switch (integral-selector) ...

  8. java编程思想第四版中net.mindview.util包下载,及源码简单导入使用

    在java编程思想第四版中需要使用net.mindview.util包,大家可以直接到http://www.mindviewinc.com/TIJ4/CodeInstructions.html 去下载 ...

  9. 《Java编程思想第四版》附录 B 对比 C++和 Java

    <Java编程思想第四版完整中文高清版.pdf>-笔记 附录 B 对比 C++和 Java “作为一名 C++程序员,我们早已掌握了面向对象程序设计的基本概念,而且 Java 的语法无疑是 ...

随机推荐

  1. luoguP4779 【模板】单源最短路径

    题目描述 单源最短路径模板. 使用 SPFA 肯定是不行的啦,网格图hack. 所以我们使用 Dijkstra 算法. 这里有一篇写的很好的 blog,无必要赘述.最后贴上代码. #include&l ...

  2. Spring Boot项目中如何定制HTTP消息转换器

    在构建RESTful数据服务过程中,我们定义了controller.repositories,并用一些注解修饰它们,但是到现在为止我们还没执行过对象的转换--将java实体对象转换成HTTP的数据输出 ...

  3. MyBatis之启动分析(一)

    前言 MyBatis 作为目前最常用的持久层框架之一,分析其源码,对我们的使用过程中可更好的运用它.本系列基于mybatis-3.4.6进行分析. MyBatis 的初始化工作就是解析主配置文件,映射 ...

  4. 小白学 Python(11):基础数据结构(元组)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  5. Java基础(二十五)Java IO(2)文件File类

    File类是一个与流无关的类.File类的对象可以获取文件及其文件所在的目录.文件的长度等信息. 1.File对象的常用构造方法. (1)File(String pathname) File file ...

  6. python之ORM(对象关系映射)

    实现了数据模型与数据库的解耦,通过简单的配置就可以轻松更换数据库,而不需要更改代码.orm操作本质上会根据对接的数据库引擎,翻译成对应的sql语句.所有使用Django开发的项目无需关心程序底层使用的 ...

  7. docker-compose下的java应用启动顺序两部曲之一:问题分析

    在docker-compose编排多个容器时,需要按实际情况控制各容器的启动顺序,本文是<docker-compose下的java应用启动顺序两部曲>的第一篇,文中会分析启动顺序的重要性, ...

  8. Linux下安装db2V9.7

    vi /etc/hosts(127.0.0.1 localhost192.168.1.53 linux-wmv8) vi /etc/services db2inst1 50000/tcp(加在最后) ...

  9. Linux读取外存

    Linux系统不像Windows系统那样,U盘自动识别,即插即用,Linux需要手动挂载U盘.步骤如下: 1.查看闪存: fdisk -l 2.添加挂载目录,一般放在/mnt下 mkdir /mnt/ ...

  10. Go语言入门:Hello world

    本文是「vangoleo的Go语言学习笔记」系列文章之一. 官网: http://www.vangoleo.com/go/go-hello-world/ 在上一篇文章你好,Go语言中,我们对Go语言的 ...