复习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反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的 ...
随机推荐
- 我理解的数据结构(一)—— 数组(Array)
我理解的数据结构(一)-- 数组(Array) 首先,我是一个phper,但是毕竟php是一个脚本语言,如果使用脚本语言去理解数据结构具有一定的局限性.因为脚本语言是不需要编译的,如果你的语法写的不错 ...
- Python模块 os.walk
Os.walk os.walk(top,topdown=True,onerror=None,followlinks=False) os.walk()是python中内置(built-in)的目录树生成 ...
- 小白神器 - Django - 起步
小白神器 - Django - 起步 一. Django下载 1. 命令行 pip install django==1.11.16 pip install django==1.11.16 -i ht ...
- Ubuntu_linux系统与网络服务管理
1.远程telnet联机:telnetd: 2.远程加密ssh联机:openssh: 3.webmin远程联机:webmin: 3.VNC远程图像界面控制:vino: 4.NAT网关服务器:iptab ...
- PAT 1114 Family Property
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
- const关键字作用
const int a; int const a; const int *a; int * const a; int const * a const; /******/ 前两个的作用是一样,a是一个常 ...
- hdu_2046_骨牌铺方格_201311251403
骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- N天学习一个linux命令之rsync
用途 主要用于本地和远程主机同步文件 特性 1 使用增量传输算法(delta-transfer algorithm) 2 支持ssh,rsync协议 3 可以用于本地同步文件 4 本地和远程主机都需要 ...
- [Algorithms] The Bayes Rule
Prior odd: The idea is to take the odds for something happening (against it not happening), which we ...
- linux下apache+openssl配置记录
软件环境 Apache Httpd 2.2.29 (http://httpd.apache.org ) OpenSSL 1.0.1h (http://www.openssl.org/source ) ...