A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ximi 中级黑马   /  2014-8-25 00:23  /  685 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.xmm.trri;

  2. /**
  3. * 接口:水果
  4. */
  5. public interface IFruit {

  6.         /**
  7.          * 方法:成长
  8.          * @param   sun 太阳
  9.          * @return  成长信息
  10.          * @throws  Exception
  11.          */
  12.         String grow(String sun) throws Exception;
  13. }

  14. /**
  15. * 抽象类:苹果
  16. * 继承:水果
  17. */
  18. public abstract class AbstractApple implements IFruit {

  19.         /**
  20.          * 方法:成长
  21.          * @param   sun 太阳
  22.          * @return  成长信息
  23.          * @throws  Exception
  24.          */
  25.         @Override
  26.         public String grow(String sun) throws Exception {
  27.                 System.out.println(sun + "正在朝阳这苹果树呢!");
  28.                 return "";
  29.         }
  30. }

  31. /**
  32. * 类:红苹果
  33. * 继承:苹果
  34. */
  35. public class RedApple extends AbstractApple {

  36.         //名字
  37.         private String name;
  38.         //颜色
  39.         public String color;
  40.         //个数
  41.         protected int size;
  42.         //单个售价
  43.         public static final int price = 10;
  44.        
  45.         /**
  46.          * 无参构造函数
  47.          * 一定声明为public类型,否则getConstructors无法得到
  48.          */
  49.         public RedApple() {
  50.                 super();
  51.                 System.out.println("初始化[RedApple]无参构造函数");
  52.                 setName("红富士");
  53.                 color = "红色";
  54.                 size = 5;
  55.         }
  56.        
  57.         /**
  58.          * 有参构造函数
  59.          * @param name  名字
  60.          * @param color 颜色
  61.          * @param size  个数
  62.          */
  63.         public RedApple(String name, String color, int size) {
  64.                 this.setName(name);
  65.                 this.name = name;
  66.                 this.color = color;
  67.                 this.size = size;
  68.         }
  69.         /**
  70.          * 获得:红苹果名称
  71.          */
  72.         public String getName() {
  73.                 return name;
  74.         }
  75.         /**
  76.          * 设置:红苹果名称
  77.          */
  78.         public void setName(String name) {
  79.                 this.name = name;
  80.         }

  81.         /**
  82.          * 方法:成长
  83.          * @param   sun 太阳
  84.          * @return  成长信息
  85.          * @throws  Exception
  86.          */
  87.         @Override
  88.         public String grow(String sun) throws Exception {
  89.                 return super.grow(sun);
  90.         }
  91.         /**
  92.          * 构建消息
  93.          * @param   sun 太阳
  94.          * @return  成长信息
  95.          */
  96.         private String buildMessage(String sun){
  97.                 return "红富士苹果正在享受" + sun + "呢!";
  98.         }
  99. }
  100. package com.xmm.trri;

  101. import java.lang.reflect.Array;
  102. import java.lang.reflect.Constructor;
  103. import java.lang.reflect.Field;
  104. import java.lang.reflect.InvocationTargetException;
  105. import java.lang.reflect.Method;
  106. import java.lang.reflect.Modifier;
  107. import java.util.Arrays;

  108. public class TestApple {

  109.         public static void main(String[] args) {
  110.                
  111.                 try {
  112.                         //获得类对象
  113.                         Class<?> clazz = Class.forName("com.xmm.trri.RedApple");
  114.                         printInfo("获得类对象", clazz);
  115.                         /*--获得类对象: class com.xmm.trri.RedApple--*/
  116.                        
  117.                         // 获得超类
  118.                         Class<?> superClass = clazz.getSuperclass();
  119.                         printInfo("获得超类", superClass);
  120.                         /*--获得超类: class com.xmm.trri.AbstractApple--*/
  121.                        
  122.                         // 获得所有父接口
  123.                         Class<?>[] interfaces = clazz.getInterfaces();
  124.                         printInfo("获得所有父接口", Arrays.toString(interfaces));
  125.                         /*--获得所有父接口: []--???--*/
  126.                        
  127.                         // 实例化
  128.                         RedApple redApple = (RedApple) clazz.newInstance();
  129.                         printInfo("实例化", redApple);
  130.                         /*--实例化: com.xmm.trri.RedApple@961dff--*/
  131.                        
  132.                         // 获得访问属性为 public 的构造方法
  133.                         Constructor<?>[] constructors = clazz.getConstructors();
  134.                         printInfo("获得构造方法", Arrays.toString(constructors));
  135.                         /*--获得构造方法: [public com.xmm.trri.RedApple(),
  136.                          *  public com.xmm.trri.RedApple(java.lang.String,java.lang.String,int)]--*/
  137.                        
  138.                         // 获得指定参数的构造方法
  139.                         Constructor<?> constructor = clazz.getDeclaredConstructor(
  140.                                                                                 String.class, String.class, int.class);
  141.                         printInfo("获得指定构造方法", constructor);
  142.                         /*--获得指定构造方法: public com.xmm.trri.RedApple(java.lang.String,java.lang.String,int)--*/
  143.                        
  144.                         // 获得方法,getMethod 只能获得 public 方法,包括父类和接口继承的方法
  145.                         Method method = clazz.getMethod("grow", String.class);
  146.                         printInfo("获得公有方法", method);
  147.                         /*--获得公有方法: public java.lang.String com.xmm.trri.RedApple.grow(java.lang.String)
  148.                          *             throws java.lang.Exception--*/
  149.                        
  150.                         // 调用方法
  151.                         method.invoke(redApple, "中午太阳");
  152.                         /*--中午太阳正在照耀这苹果树呢!--*/
  153.                        
  154.                        
  155.                         // 获得修饰符,包括 private/public/protect,static
  156.                         String modifier = Modifier.toString(method.getModifiers());
  157.                         printInfo("获得方法修饰符", modifier);
  158.                         /*--获得方法修饰符: public--*/
  159.                        
  160.                         // 获得参数类型
  161.                         Class<?>[] paramTypes = method.getParameterTypes();
  162.                         printInfo("获得方法参数类型", Arrays.toString(paramTypes));
  163.                         /*--获得方法参数类型: [class java.lang.String]--*/
  164.                        
  165.                         // 获得返回值类型
  166.                         Class<?> returnType = method.getReturnType();
  167.                         printInfo("获得方法返回值类型", returnType);
  168.                         /*--获得方法返回值类型: class java.lang.String--*/
  169.                        
  170.                         // 获得异常类型
  171.                         Class<?>[] excepTypes = method.getExceptionTypes();
  172.                         printInfo("获得方法异常类型", Arrays.toString(excepTypes));
  173.                         /*--获得方法异常类型: [class java.lang.Exception]--*/
  174.                        
  175.                         // 调用私有方法,getDeclaredMethod 获得类自身的方法,
  176.                         // 包括 public,protect,private 方法
  177.                         Method method2 = clazz.getDeclaredMethod("buildMessage", String.class);
  178.                         // 暴力反射
  179.                         method2.setAccessible(true);
  180.                         String result = (String)method2.invoke(redApple, "中午太阳");
  181.                         printInfo("获得私有方法", result);
  182.                         /*--获得私有方法: 红富士苹果正在享受中午太阳呢!--*/
  183.                        
  184.                         // 获得全部字段
  185.                         Field[] fields = clazz.getFields();
  186.                         printInfo("获得类全部字段", Arrays.toString(fields));
  187.                         /*--
  188.                          * 获得类全部字段:[public java.lang.String com.xmm.trri.RedApple.color,
  189.                [public static final int com.xmm.trri.RedApple.price]
  190.                          * --*/
  191.                        
  192.                         // 获得类自身定义的指定字段
  193.                         Field field = clazz.getDeclaredField("name");
  194.                         printInfo("获得自身指定的名称字段", field);
  195.                         /*--获得自身指定的名称字段: private java.lang.String com.xmm.trri.RedApple.name--*/
  196.                        
  197.                         // 获得类及其父类,父接口定义的 public 字段
  198.                         Field field2 = clazz.getField("color");
  199.                         printInfo("获得公有的指定名称字段", field2);
  200.                         /*--获得公有的指定名称字段: public java.lang.String com.xmm.trri.RedApple.color--*/
  201.                        
  202.                         // 获得字段权限修饰符,包括private/public/protect,static,final
  203.                         String fieldModifier = Modifier.toString(field2.getModifiers());
  204.                         printInfo("获得字段权限修饰符", fieldModifier);
  205.                         /*--获得字段权限修饰符: public--*/
  206.                        
  207.                         // 操作数组
  208.                         int[] exampleArray = { 1, 2, 3, 4, 5 };
  209.                        
  210.                         // 获得数组类型
  211.                         Class<?> componentType = exampleArray.getClass().getComponentType();
  212.                         printInfo("数组类型", componentType.getName());
  213.                         /*--数组类型: int--*/
  214.                        
  215.                         // 获得长度
  216.                         printInfo("数组长度",
  217.                         Array.getLength(exampleArray));
  218.                         /*--数组长度: 5--*/
  219.                        
  220.                         // 获得指定元素
  221.                         printInfo("获得数组元素",
  222.                         Array.get(exampleArray, 2));
  223.                         /*--获得数组元素: 3--*/
  224.                        
  225.                         // 修改指定元素
  226.                         Array.set(exampleArray, 2, 6);
  227.                         printInfo("修改数组元素", Arrays.toString(exampleArray));
  228.                         /*--修改数组元素: [1, 2, 6, 4, 5]--*/
  229.                        
  230.                         // 获得当前的类加载器
  231.                         printInfo("获得当前类加载器",
  232.                         redApple.getClass().getClassLoader().getClass().getName());
  233.                         /*--获得当前类加载器: sun.misc.Launcher$AppClassLoader--*/
  234.                        
  235.                        
  236.                 } catch (ClassNotFoundException e) {
  237.                         e.printStackTrace();
  238.                 }catch (IllegalAccessException e) {
  239.                         e.printStackTrace();
  240.                 } catch (IllegalArgumentException e) {
  241.                         e.printStackTrace();
  242.                 } catch (InvocationTargetException e) {
  243.                         e.printStackTrace();
  244.                 }catch (NoSuchFieldException e) {
  245.                         e.printStackTrace();
  246.                 } catch (SecurityException e) {
  247.                         e.printStackTrace();
  248.                 }catch (NoSuchMethodException e) {
  249.                         e.printStackTrace();
  250.                 } catch (InstantiationException e) {
  251.                         e.printStackTrace();
  252.                 }
  253.                
  254.         }
  255.        
  256.         public static void printInfo(String info, Object obj) {
  257.                 if (obj.getClass().isArray()) {
  258.                         System.out.println(info + ": ");
  259.                         int length = Array.getLength(obj);
  260.                         System.out.println("Array Size: " + length);
  261.                         for (int i = 0; i < length; i++) {
  262.                                 System.out.print("Array[" + i + "]: " + Array.get(obj, i)
  263.                                                 + ", ");
  264.                         }
  265.                         if (length != 0)
  266.                                 System.out.println();
  267.                 }
  268.                 System.out.println(info + ": " + obj.toString());
  269.         }
  270. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马