• 复习java基础第七天(反射)


    一:目标

    Ø理解 Class 类
    Ø理解 Java 的类加载机制
    Ø学会使用 ClassLoader 进行类加载
    Ø理解反射的机制
    Ø掌握 Constructor、Method、Field 类的用法
    Ø理解并掌握动态代理
     
    1、Class类
    –对象照镜子后可以得到的信息:某个类的数据成员名、方法和构造器、某个类到底实现了哪些接口。
    对于每个类而言,JRE 都为其保留一个不变的 Class 类型的对象。
    一个 Class 对象包含了特定某个类的有关信息。
    –  Class 对象只能由系统建立对象。
    –  一个类在 JVM 中只会有一个Class实例。
    –  每个类的实例都会记得自己是由哪个 Class 实例所生成 。
    获取 Class 对象的方式:
     
     
    Class类的常用方法:
     
    三种类加载器(ClassLoader):
     
    练习代码:
     1 package com.shellway.test;
     2 
     3 public class Person {
     4     String name;
     5     String age;
     6 
     7     public String getName() {
     8         return name;
     9     }
    10 
    11     public void setName(String name) {
    12         this.name = name;
    13     }
    14 
    15     public String getAge() {
    16         return age;
    17     }
    18 
    19     public void setAge(String age) {
    20         this.age = age;
    21     }
    22 
    23     public Person(String name, String age) {
    24         super();
    25         this.name = name;
    26         this.age = age;
    27         System.out.println("有参数的构造器。。。。");
    28     }
    29 
    30     public Person() {
    31         System.out.println("无参数的构造器。。。。");
    32     }
    33 }
    Person类
     1 package com.shellway.test;
     2 
     3 import static org.junit.Assert.*;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.InputStream;
     7 import java.lang.reflect.Field;
     8 import org.junit.Test;
     9 
    10 public class ReflectionTest {
    11     @Test
    12     public void testClassLoader() throws ClassNotFoundException,
    13             FileNotFoundException {
    14         ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    15         System.out.println(classLoader);
    16         classLoader = classLoader.getParent();
    17         System.out.println(classLoader);
    18         classLoader = classLoader.getParent();
    19         System.out.println(classLoader);
    20 
    21         // 测试由哪个类加载器进行加载
    22         classLoader = Class.forName("com.shellway.test.Person")
    23                 .getClassLoader();
    24         System.out.println(classLoader);
    25 
    26         // 调用getResourceAsStream获取类路径下文件对应的输入流
    27         InputStream in = null;
    28         in = this.getClass().getClassLoader()
    29                 .getResourceAsStream("com/shellway/test/test.properties");
    30         // new FileInputStream("com/shellway/test/test.properties");
    31         System.out.println(in);
    32     }
    33 
    34     @Test
    35     public void testNewInstance() throws ClassNotFoundException,
    36             InstantiationException, IllegalAccessException {
    37         String className = "com.shellway.test.Person";
    38         Class clazz = Class.forName(className);
    39         Object obj = clazz.newInstance();// 实际上调用的是无参数的构造器创建的实例。
    40         System.out.println(obj);
    41     }
    42 
    43     @Test
    44     public void test() throws ClassNotFoundException {
    45         // 1.得到Class对象的三种方法
    46         // 1.1直接通过类名.class的方式得到
    47         Class clazz = null;
    48         clazz = Person.class;
    49         Field[] fields = clazz.getDeclaredFields();
    50         System.out.println(clazz);
    51 
    52         // 2.1通过对象调用getClass()方法来获取
    53         Person person = new Person();// Object obj =new Person();
    54         clazz = person.getClass(); // clazz = obj.getClass();
    55 
    56         // 3.1通过全类名的方式获取,用的最多的
    57         String className = "com.shellway.test.Person";
    58         clazz = Class.forName(className);
    59     }
    60 }
    ReflectionTest类

     利用反射写一个晚会案例:

    1 Singing=com.shellway.Reflection.impl.Liudehua
    2 Dancing=com.shellway.Reflection.impl.Guofucheng
    3 Xiangsheng=com.shellway.Reflection.impl.Guodegang
    party.properties
     1 package com.shellway.Reflection;
     2 
     3 import com.shellway.Reflection.imp.Dancing;
     4 import com.shellway.Reflection.imp.Singing;
     5 import com.shellway.Reflection.imp.Xiangsheng;
     6 
     7 public class EveningParty {
     8     public static void main(String[] args) throws Exception {
     9         System.out.println("晚会开始!!!!!");
    10         //唱歌
    11         Singing sing = Factory.getSinger();
    12         sing.singing();
    13         //跳舞
    14         Dancing dancing = Factory.getDancer();
    15         dancing.dancing();
    16         //相声
    17         Xiangsheng xiangsheng = Factory.getPerformer();
    18         xiangsheng.show();
    19         System.out.println("晚会结束!!!!!");
    20     }
    21 }
    EveningParty.java
    1 package com.shellway.Reflection.imp;
    2 
    3 public interface Singing {
    4     public void singing();
    5 }
    Singing 接口
    1 package com.shellway.Reflection.imp;
    2 
    3 public interface Dancing {
    4     void dancing();
    5 }
    Dancing 接口
    1 package com.shellway.Reflection.imp;
    2 
    3 public interface Xiangsheng {
    4      void show();
    5 }
    Xiangsheng 接口

    接口实现类:

     1 package com.shellway.Reflection.impl;
     2 
     3 import com.shellway.Reflection.imp.Singing;
     4 
     5 public class Liudehua implements Singing {
     6 
     7     @Override
     8     public void singing() {
     9         System.out.println("刘德华演唱:中国人");
    10     }
    11 }
    歌手刘德华
     1 package com.shellway.Reflection.impl;
     2 
     3 import com.shellway.Reflection.imp.Singing;
     4 
     5 public class Xietingfeng implements Singing {
     6 
     7     @Override
     8     public void singing() {
     9         System.out.println("谢霆锋演唱:因为爱所以爱...");
    10     }
    11 }
    歌手谢霆锋
     1 package com.shellway.Reflection.impl;
     2 
     3 import com.shellway.Reflection.imp.Dancing;
     4 
     5 public class Yangliping implements Dancing {
     6 
     7     @Override
     8     public void dancing() {
     9         System.out.println("杨丽萍表演孔雀舞...");
    10     }
    11 }
    舞者杨丽萍
     1 package com.shellway.Reflection.impl;
     2 
     3 import com.shellway.Reflection.imp.Dancing;
     4 
     5 public class Guofucheng implements Dancing {
     6 
     7     @Override
     8     public void dancing() {
     9         System.out.println("郭富城跳广场舞...");
    10     }
    11 }
    舞者郭富城
     1 package com.shellway.Reflection.impl;
     2 
     3 import com.shellway.Reflection.imp.Xiangsheng;
     4 
     5 public class Guodegang implements Xiangsheng {
     6 
     7     @Override
     8     public void show() {
     9         System.out.println("郭德纲表演相声...");
    10     }
    11 }
    相声演员郭德纲

     工厂类与配置文件结合实现反射

     1 package com.shellway.Reflection;
     2 
     3 import java.util.ResourceBundle;
     4 
     5 import com.shellway.Reflection.imp.Dancing;
     6 import com.shellway.Reflection.imp.Singing;
     7 import com.shellway.Reflection.imp.Xiangsheng;
     8 
     9 public class Factory {
    10 
    11     public static Singing getSinger() throws Exception{
    12         String pathName = ResourceBundle.getBundle("party").getString("Singing");
    13         Object obj = Class.forName(pathName).newInstance();
    14         return  (Singing) obj;
    15     }
    16     public static Dancing getDancer() throws Exception{
    17         String pathName = ResourceBundle.getBundle("party").getString("Dancing");
    18         Object obj = Class.forName(pathName).newInstance();
    19         return (Dancing) obj;
    20     }
    21 
    22     public static Xiangsheng getPerformer() throws Exception{
    23         String pathName = ResourceBundle.getBundle("party").getString("Xiangsheng");
    24         Object obj = Class.forName(pathName).newInstance();
    25         return (Xiangsheng) obj;
    26     }
    27         
    28 }
    Factory
  • 相关阅读:
    (爱加密系列教程十四)利用无效字节码指令引发逆向工具崩溃(一)
    (爱加密系列教程十三) 如何防止工具(八门神器)进行内存修改
    (爱加密系列教程十二) 如何防止jd-gui查看代码
    (爱加密系列教程十一) 关于某银行apk的安全分析
    超简教程:Xgboost在Window上的安装(免编译)
    自己动手实现机器学习算法:神经网络(附源代码)
    Spark 源码解析:TaskScheduler的任务提交和task最佳位置算法
    Spark 源码解析 : DAGScheduler中的DAG划分与提交
    16.Spark Streaming源码解读之数据清理机制解析
    14:Spark Streaming源码解读之State管理之updateStateByKey和mapWithState解密
  • 原文地址:https://www.cnblogs.com/shellway/p/3850086.html
Copyright © 2020-2023  润新知