一 unit单元测试
测试分类:
黑盒测试; 一般代码
白盒测试:需要写代码,junit单元测试
Junit 使用:
定义一个测试类
建议测试雷鸣被测试类名+TEST
包名,放到TEST
定义测试方法
Test测试的方法名 testAdd()
返回值void
参数列表:空参
给方法加注解
方法
Assertequal(正确答案,接受计算结果的答案)
联合用的注解
Before 修饰的方法在测试方法之前被执行
After 修饰的方法在测试之后被执行
反射
反射,框架设计的灵魂
框架:半成品软件。可以再框架的基础上进行软件开发,简化代码
反射概念;将类的组成部分组装为其他对象
Java•代码:
在计算机中经历的阶段 三个阶段
一.源代码阶段.
类的创建
Javac编译 形成class文件
运行class文件.
二,把字节码文件加载进内存 类加载器 class类,描述字节码文件的类
反射好处:
程序运行过程中,来操作对象
可以解耦,增加了可扩展性
Class对象
使用class对象
功能:1.获取功能
获取成员变量
Getfileds()//获取所有public修饰的变量
Getfiled(string name)//获取指定名称的public变量修饰的方法
操作方法:
Get获取值,需要先建立一个class对象,然后getfiled获取成员变量的值a,建立一个类对象,a.对象名 获取该对象中的这个变量的值
Set 类似于上(对象名,值)
Getdeclarefileds()
Getdeclaredfiled(string name)
公有私有都可以显示出来,使用其它权限变量,需要忽略安全检查;
d.setAccessible(true)
获取成员方法
Getmethods()
Getmethod(string name)
Getdeclaremethods()
Getdeclaredmethod(string name)
方法
Invoke()
获取构造方法
Getconstructors()
Getconstructor(string name)
Getdeclareconstructors()
Getdeclaredconstructor(string name)
Newinstance 创造对象 无参时可以直接调用类名的方法,有参需要先建一个构造器对象
package reflectTest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class ReflectTest {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InstantiationException {
// TODO Auto-generated method stub
//建立一个class对象
/*Class<Person> pc=Person.class;
//获取public变量
Field[] fields = pc.getFields();
Field eye = pc.getField("eyes");
for (Field field : fields) {
System.out.println(field);
}
Person p=new Person();
//get 方法 获取class对象中的变量
Object value = eye.get(p);
System.out.println(value);
//set(对象名,数值);给设置值
eye.set(p, 2);
System.out.println(p);*/
Class<Person> cfc=Person.class;
//.getDeclaredFields(); 获取所有的值无论公有私有
/*Field[] declaredFields = cfc.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println(field);
}*/
/*Field d = cfc.getDeclaredField("age");
// d.setAccessible(true);忽略权限
d.setAccessible(true);
System.out.println(d);
Person p = new Person("hcl", 22,2);
d.set(p, 100);
System.out.println(p);
*/
Class<Person> p1=Person.class;
//获取有参构造器
Constructor<Person> constructor = p1.getConstructor(String.class,int.class,int.class);
System.out.println(constructor);
/*Person newInstance = constructor.newInstance("张三",12,2);
System.out.println(newInstance);*/
//无参构造 可以直接用class对象调用
Person newInstance = p1.newInstance();
System.out.println(newInstance);
}
}