一:目标

Ø理解 Class 类
Ø理解 Java 的类加载机制
Ø学会使用 ClassLoader 进行类加载
Ø理解反射的机制
Ø掌握 Constructor、Method、Field 类的用法
Ø理解并掌握动态代理
 
1、Class类

–对象照镜子后可以得到的信息:某个类的数据成员名、方法和构造器、某个类到底实现了哪些接口。
对于每个类而言,JRE 都为其保留一个不变的 Class 类型的对象。
一个 Class 对象包含了特定某个类的有关信息。
–  Class 对象只能由系统建立对象。
–  一个类在 JVM 中只会有一个Class实例。
–  每个类的实例都会记得自己是由哪个 Class 实例所生成 。
获取 Class 对象的方式:
 
 
Class类的常用方法:
 
三种类加载器(ClassLoader):
 
练习代码:
 package com.shellway.test;

 public class Person {
String name;
String age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public Person(String name, String age) {
super();
this.name = name;
this.age = age;
System.out.println("有参数的构造器。。。。");
} public Person() {
System.out.println("无参数的构造器。。。。");
}
}

Person类

 package com.shellway.test;

 import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Field;
import org.junit.Test; public class ReflectionTest {
@Test
public void testClassLoader() throws ClassNotFoundException,
FileNotFoundException {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
System.out.println(classLoader);
classLoader = classLoader.getParent();
System.out.println(classLoader);
classLoader = classLoader.getParent();
System.out.println(classLoader); // 测试由哪个类加载器进行加载
classLoader = Class.forName("com.shellway.test.Person")
.getClassLoader();
System.out.println(classLoader); // 调用getResourceAsStream获取类路径下文件对应的输入流
InputStream in = null;
in = this.getClass().getClassLoader()
.getResourceAsStream("com/shellway/test/test.properties");
// new FileInputStream("com/shellway/test/test.properties");
System.out.println(in);
} @Test
public void testNewInstance() throws ClassNotFoundException,
InstantiationException, IllegalAccessException {
String className = "com.shellway.test.Person";
Class clazz = Class.forName(className);
Object obj = clazz.newInstance();// 实际上调用的是无参数的构造器创建的实例。
System.out.println(obj);
} @Test
public void test() throws ClassNotFoundException {
// 1.得到Class对象的三种方法
// 1.1直接通过类名.class的方式得到
Class clazz = null;
clazz = Person.class;
Field[] fields = clazz.getDeclaredFields();
System.out.println(clazz); // 2.1通过对象调用getClass()方法来获取
Person person = new Person();// Object obj =new Person();
clazz = person.getClass(); // clazz = obj.getClass(); // 3.1通过全类名的方式获取,用的最多的
String className = "com.shellway.test.Person";
clazz = Class.forName(className);
}
}

ReflectionTest类

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

 Singing=com.shellway.Reflection.impl.Liudehua
Dancing=com.shellway.Reflection.impl.Guofucheng
Xiangsheng=com.shellway.Reflection.impl.Guodegang

party.properties

 package com.shellway.Reflection;

 import com.shellway.Reflection.imp.Dancing;
import com.shellway.Reflection.imp.Singing;
import com.shellway.Reflection.imp.Xiangsheng; public class EveningParty {
public static void main(String[] args) throws Exception {
System.out.println("晚会开始!!!!!");
//唱歌
Singing sing = Factory.getSinger();
sing.singing();
//跳舞
Dancing dancing = Factory.getDancer();
dancing.dancing();
//相声
Xiangsheng xiangsheng = Factory.getPerformer();
xiangsheng.show();
System.out.println("晚会结束!!!!!");
}
}

EveningParty.java

 package com.shellway.Reflection.imp;

 public interface Singing {
public void singing();
}

Singing 接口

 package com.shellway.Reflection.imp;

 public interface Dancing {
void dancing();
}

Dancing 接口

 package com.shellway.Reflection.imp;

 public interface Xiangsheng {
void show();
}

Xiangsheng 接口

接口实现类:

 package com.shellway.Reflection.impl;

 import com.shellway.Reflection.imp.Singing;

 public class Liudehua implements Singing {

     @Override
public void singing() {
System.out.println("刘德华演唱:中国人");
}
}

歌手刘德华

 package com.shellway.Reflection.impl;

 import com.shellway.Reflection.imp.Singing;

 public class Xietingfeng implements Singing {

     @Override
public void singing() {
System.out.println("谢霆锋演唱:因为爱所以爱...");
}
}

歌手谢霆锋

 package com.shellway.Reflection.impl;

 import com.shellway.Reflection.imp.Dancing;

 public class Yangliping implements Dancing {

     @Override
public void dancing() {
System.out.println("杨丽萍表演孔雀舞...");
}
}

舞者杨丽萍

 package com.shellway.Reflection.impl;

 import com.shellway.Reflection.imp.Dancing;

 public class Guofucheng implements Dancing {

     @Override
public void dancing() {
System.out.println("郭富城跳广场舞...");
}
}

舞者郭富城

 package com.shellway.Reflection.impl;

 import com.shellway.Reflection.imp.Xiangsheng;

 public class Guodegang implements Xiangsheng {

     @Override
public void show() {
System.out.println("郭德纲表演相声...");
}
}

相声演员郭德纲

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

 package com.shellway.Reflection;

 import java.util.ResourceBundle;

 import com.shellway.Reflection.imp.Dancing;
import com.shellway.Reflection.imp.Singing;
import com.shellway.Reflection.imp.Xiangsheng; public class Factory { public static Singing getSinger() throws Exception{
String pathName = ResourceBundle.getBundle("party").getString("Singing");
Object obj = Class.forName(pathName).newInstance();
return (Singing) obj;
}
public static Dancing getDancer() throws Exception{
String pathName = ResourceBundle.getBundle("party").getString("Dancing");
Object obj = Class.forName(pathName).newInstance();
return (Dancing) obj;
} public static Xiangsheng getPerformer() throws Exception{
String pathName = ResourceBundle.getBundle("party").getString("Xiangsheng");
Object obj = Class.forName(pathName).newInstance();
return (Xiangsheng) obj;
} }

Factory

最新文章

  1. 【金】nginx+uwsgi+django+python 应用架构部署
  2. c++作业:Circle
  3. Java—类的封装、继承与多态
  4. C语言实现单链表-01版
  5. CodeMap
  6. Velocity(1)——注释
  7. C#其他
  8. Spark RDD概念学习系列之RDD的依赖关系(宽依赖和窄依赖)(三)
  9. Python3 学习第三弹:异常情况如何处理?
  10. 图形化OpenGL调试器 BuGLe [转]
  11. 【转】ubuntu自动挂载硬盘方法
  12. hdu_5585_Less Time, More profit(二分+最大权闭合图)
  13. 【Machine Learning in Action --5】逻辑回归(LogisticRegression)
  14. arcgisserver成功发布服务后,浏览服务,无地图显示
  15. Zabbix(一) : 简介以及Server端安装
  16. CentOS LNMP环境搭建 各版本
  17. IDEA 无法运行Junit, 报错Class not found xxxx Empty test suite.
  18. (一)校园信息通微信小程序从前端到后台整和笔记
  19. JavaScript 作用域的误区
  20. MaC 修改MySQL密码

热门文章

  1. 爬虫系列(九) xpath的基本使用
  2. bupt summer training for 16 #7 ——搜索与DP
  3. JavaSE 学习笔记之package包(十一)
  4. 车展(vijos P1459)
  5. PatentTips - DMA address translation between peer-to-peer IO devices
  6. readl()和writel()
  7. 深入分析Linux自旋锁
  8. lzugis——Arcgis Server for JavaScript API之自己定义InfoWindow
  9. Facebook图搜索unicorn
  10. HDU 5347(MZL's chemistry-打表)