复习java基础第七天(反射)
一:目标



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
复习java基础第七天(反射)的更多相关文章
- Java实习生常规技术面试题每日十题Java基础(七)
目录 1. Java设计模式有哪些? 2.GC是什么?为什么要有GC? 3. Java中是如何支持正则表达式. 4.比较一下Java和JavaSciprt. 5.Math.round(11.5) 等于 ...
- JAVA基础 (二)反射 深入解析反射机制
在谈论到反射这个问题时,你是否有例如以下疑问? 不管是在.NET还是Java中反射的原理和机制是一样的,理解了一种还有一种就能够迎刃而解,想要理解反射首先须要了解底层的一些概念和执行.理解了反射有助于 ...
- java基础篇3之反射
1.反射的基础 反射的基石---->Class类 java程序中的各个java类属于同一类事物,描述这类事物的java类名就是Class 获取字节码对应的实例对象(Class类型) class ...
- 【Java基础】RTTI与反射之Java
一.引言 很多时候我们的程序可能需要在运行时识别对象和类的信息,比如多态就是基于运行时环境进行动态判断实际引用的对象.在运行时识别对象和类的信息主要有两种方式:1.RTTI,具体是Class对象,它假 ...
- JAVA基础知识之JVM-——使用反射生成并操作对象
Class对象可以获取类里的方法,由Method对象表示,调用Method的invoke可以执行对应的方法:可以获取构造器,由Constructor对象表示,调用Constructor对象的newIn ...
- JAVA基础知识之JVM-——通过反射查看类信息
Class实例 当类被加载之后,JVM中就会生成一个Class实例,通过这个实例就可以访问JVM中的这个类.有三种方式可以获取Class对象 使用Class的静态方法forName(完整包名) 调用类 ...
- Java 基础【18】 反射与内省
1.概念定义 Java 反射机制(Reflect)容许程序在运行时加载.探知.使用编译期间完全未知的 class,核心类 java.lang.Class. 通过把指定类中各种元素映射成 java.la ...
- java基础强化——深入理解反射
目录 1.从Spring容器的核心谈起 2. 反射技术初探 2.1 什么是反射技术 2.2 类结构信息和java对象的映射 3 Class对象的获取及需要注意的地方 4. 运行时反射获取类的结构信息 ...
- Java基础(十三)反射
一.反射 1.反射概念 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的 ...
随机推荐
- Problem 21
Problem 21 https://projecteuler.net/problem=21 Let d(n) be defined as the sum of proper divisors of ...
- (蓝桥)2017C/C++A组第七题正则问题
#include<iostream> #include<memory.h> #include<stack> #include<string> using ...
- flask中的session cookie 测试 和 项目中的用户状态保持
# -*- coding:utf-8 -*- # Author: json_steve from flask import Flask, current_app, make_response, req ...
- kafka监控工具kafka-manager
1.几个kafka监控工具 Kafka Web Console:监控功能较为全面,可以预览消息,监控Offset.Lag等信息,但存在bug,不建议在生产环境中使用. Kafka Manager:偏向 ...
- [HDU3038]How Many Answers Are Wrong(并查集)
传送门 和某题类似,只不过奇偶换成了和. ——代码 #include <cstdio> #include <iostream> #define N 1000001 int n, ...
- hdu_1014_Uniform Generator_201310141958
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Axis2中使用wsdl2java.bat生成客户端代码
1 准备环境 (1)下载Axis2的zip包axis2-1.5.5-bin.zip,并解压. 官方网址:http://ws.apache.org/axis2/ (2)设置环境变量(我的电脑->属 ...
- F1: A Distributed SQL Database That Scales GOOGLE F1 论文
http://research.google.com/pubs/pub41344.html http://research.google.com/pubs/pub36726.html
- LinkedHashMap源代码阅读
LinkedHashMap LinkedHashMap内部採用了散列表和链表实现Map接口,并能够保证迭代的顺序,和HashMap不同,其内部维护一个指向全部元素的双向链表,其决定了遍历的顺序,一般是 ...
- ant+jmeter 报告优化
环境基础:ant+jmeter+java +jmeter脚本 1.将 JMeter的extras目录中ant-jmeter-1.1.1.jar包拷贝至ant安装目录下的lib目录中 2.修改JMete ...