标签:his 环境 tar tac etag types tst set close
@Test
.Assert.assertEquals(期望的结果,运行的结果);
@Before
:修饰的方法会在测试方法之前自动执行.@After
:修饰的方法会在测试之后自动执行.package Junit_Demo;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class demo1 {
@Before
public void init()
{
System.out.println("init...");
}
@Test
public void test1()
{
int a=2;
int b=3;
int c=a+b;
System.out.println(c);
}
@Test
public void test2()
{
int a=2;
int b=3;
int c=a+b;
//断言:如果两个不相等,则失败.
Assert.assertEquals(3,c);
System.out.println(c);
}
@After
public void close()
{
System.out.println("after...");
}
}
Class.forName("全类名")
:将字节码文件加载进内存,返回Class对象.
类名.class
:通过类名的属性class获取.
对象.getClass()
:在Object类中定义.
结论:同一个字节码文件(*.class)在一次程序运行过程中,只会被加载一次,不论通过哪一种方式获取的Class对象都是同一个.
Field[] getFields()
:获取所有public修饰的成员变量Field getField(String name)
:获取指定名称的public修饰的成员变量.Field[] getDeclaredFields()
:获取所有的成员变量,不考虑修饰符.Field getDeclaredField(String name)
:获取指定名称的成员变量.Constructor<?>[] getConstructors()
Constructor<T> getConstructor(类<?>... parameterTypes)
Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
Constructor<?>[] getDeclaredConstructors()
Method[] getMethods()
Method getMethod(String name, 类<?>... parameterTypes)
Method[] getDeclaredMethods()
Method getDeclaredMethod(String name, 类<?>... parameterTypes)
String getName()
void set(Object obj,Object value)
:第一个参数是设置的对象,第二个参数是值.get(Object obj)
:参数表示获取的对象.setAccessible(true)
暴力反射.T newInstance(Object... initargs)
Object invoke(Object obj,Object... args)
:第一个参数是要执行方法的对象.后面的参数是方法的参数.String getName
:获取方法名.package Reflect_Demo;
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
‘}‘;
}
public void Sleep()
{
System.out.println("睡觉...");
}
}
package Reflect_Demo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class demo1 {
public static void main(String[] args) throws Exception {
Class<User> userClass = User.class;
Constructor<User> constructor = userClass.getConstructor(String.class, int.class);
User user = constructor.newInstance("zhangsan", 15);
Method sleep = userClass.getMethod("Sleep");
sleep.invoke(user);
Field name_Field = userClass.getDeclaredField("age");
name_Field.setAccessible(true);
System.out.println(user);
name_Field.set(user,25);
System.out.println(user);
}
}
static {
System.out.println("static_block");
}
程序加载入内存之后,只会被执行一次.
public static void print_static()
{
System.out.println("static_print");
}
可以使用类直接调用方法.
public class static_block {
static {
System.out.println("static_block");
}
public void print()
{
System.out.println("public_print");
}
public static void print_static()
{
System.out.println("static_print");
}
}
public static void main(String[] args) {
try {
Class.forName("Reflect_Demo.static_block");// 静态代码块
static_block.print_static();// 静态方法
static_block static_block = new static_block();
static_block.print();// 公共方法
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
标签:his 环境 tar tac etag types tst set close
原文地址:https://www.cnblogs.com/acknowledge/p/13282141.html