数组
package com.demo; import java.util.Arrays; import org.junit.Test; public class TestArrayDemo { @Test public void testCreate() { // 10个人,有10个人名字,把10个人名字存储起来 String name1 = "AAA"; String name2 = "AAA"; String name3 = "AAA"; String name4 = "AAA"; String name5 = "AAA"; String name6 = "AAA"; String name7 = "AAA"; String name8 = "AAA"; String name9 = "AAA"; String name10 = "AAA"; // 有数组的概念: int length = 10; // 定义: 存储同一个类型的元素集合, 定义数组的时候必须定义容量。容量是不可变 // 1. String[] names1 = new String[length]; // 是引用类型, 会在堆里面分配空间 System.out.println(names1); // [Ljava.lang.String;@443b7951 // 2. String[] names2 = null; names2 = new String[2]; //3. String[] names3 = {}; System.out.println(names3.length); } @Test // 数组初始化 public void arrayInit() { //1. 静态初始化(创建数组的同时为其赋值) int[] numbers1 = {1,2,4,423,3,4,5,4}; int[] numbers2 = new int[]{2,3,5,3,56,43}; //2. 使用元素下标(0开始)为其赋值 byte[] buffer = new byte[10]; buffer[0] = 12; buffer[4] = 4; buffer[7] = 7; // System.out.println(Arrays.toString(buffer)); // 遍历数组中值 for (int i = 0; i < buffer.length; i++) { System.out.print(buffer[i]+" "); } System.out.println("\r\n"); // 增强for for (byte value : buffer) { System.out.println(value); } // 学生类的数组 Student[] students = new Student[4]; students[0] = new Student(); students[1] = new Student(); students[2] = new Student(); students[3] = new Student(); System.out.println(Arrays.toString(students)); } }
反射获取类信息
package com.demo; import java.util.Date; /** * 学生类 * * @author Administrator * */ public class Student { public Integer id; String name; protected String sex; private Integer age; public Date birth; public Student() { System.out.println("==================="); } public Student(int i) { System.out.println("==================="); } private Student(int i, String name) { System.out.println("==================="); } public String getName() { return name; } private void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } private Integer getAge() { return age; } private void setAge(Integer age) { this.age = age; } private void setAge(int age, String name) { this.age = age; this.name = name; } }
package com.demo; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; import org.hamcrest.core.IsNot; import org.junit.Test; public class TestStudent { @Test // 获取对应类的Class对象 public void testGetClass() throws Exception { System.out.println("-------获取对应类的Class对象--------"); // 1. 通过类本身class属性获取 System.out.println(Student.class); // 2. 通过类对应实例获取(自动补全CTRL+2,l) Student student = new Student(10); System.out.println(student.getClass()); // 3. 通过Class类中提供静态forName方法获取 Class clazz = Class.forName("com.demo.Student"); System.out.println(clazz); } @Test // 获取一个Class对象中的成员变量 public void testGetClassForField() throws Exception { System.out.println("------- 获取一个Class对象中的成员变量--------"); Class clazz = Student.class; // 获取所有的由public修饰的成员变量 Field[] fields = clazz.getFields(); for (Field field : fields) { System.out.println(field); } System.out.println("----------------------------"); // 变量: 局部变量定义方法里面, 成员变量定义类的里面方法的外面 // 获取当前Class对应类中所有定义的成员变量(定义类的里面,方法外面) fields = clazz.getDeclaredFields(); for (Field field : fields) { System.out.println(field); System.out.println("访问修饰符:" + Modifier.toString(field.getModifiers())); System.out.println("字段类型:" + field.getType().getTypeName()); System.out.println("字段类型简单名称:" + field.getType().getSimpleName()); System.out.println("字段名称:" + field.getName()); } // 通过Class对象创建类实例 Object instance = clazz.newInstance(); // Object stu = new Student(); // 单独获取一个字段 Field field = clazz.getDeclaredField("age"); // 开启任何字段访问权限 field.setAccessible(true); // 设置私有字段的值 field.set(instance, 25); // 通过反射获取字段的值 /* * Object value = field.get(instance); System.out.println(value); */ if (instance instanceof Student) { Student stu = (Student) instance; // System.out.println(stu.getAge()); } } @Test // 获取Class对象中所有方法及信息 public void testClassForMethod() throws Exception { System.out.println("------- // 获取Class对象中所有方法及信息--------"); Class clazz = Student.class; // 获取由public修饰的方法 Method[] methods = clazz.getMethods(); for (Method method : methods) { System.out.println(method); } System.out.println("----------------------------"); // 获取当前类中所有的方法 methods = clazz.getDeclaredMethods(); for (Method method : methods) { System.out.println(method); System.out.println("方法访问修饰符:" + Modifier.toString(method.getModifiers())); System.out.println("方法返回类型:" + method.getReturnType().getSimpleName()); System.out.println("方法名称:" + method.getName()); System.out.println("参数类型数组:" + Arrays.toString(method.getParameterTypes())); System.out.println("参数个数:" + method.getParameterCount()); System.out.println("参数数组:" + Arrays.toString(method.getParameters())); } // 创建一个Student对象 Student stu = new Student(); // 单独获取一个方法【方法名称必须和类中名完全匹配】 Method method = clazz.getDeclaredMethod("setAge", int.class, String.class); // 开启任何方法的访问权限 method.setAccessible(true); Object object = method.invoke(stu, 10, "张三"); System.out.println(stu.getName()); /* * Field field = clazz.getDeclaredField("age"); // 开启访问权限 * field.setAccessible(true); Object value = field.get(stu); * System.out.println(value); */ Method getMethod = clazz.getDeclaredMethod("getAge"); getMethod.setAccessible(true); Object value = getMethod.invoke(stu); System.out.println(value); } @Test // 通过Class对象调用对应类的构造函数创建对象的实例 public void testClassForConstructor() throws NoSuchMethodException, Exception { System.out.println("------- 通过Class对象调用对应类的构造函数创建对象的实例 --------"); Class clazz = Student.class; // 获取使用public修饰的构造器 Constructor[] constructors = clazz.getConstructors(); for (Constructor cl : constructors) { System.out.println(cl); } System.out.println("--------------------------"); // 获取当前类中所有的构造器 constructors = clazz.getDeclaredConstructors(); for (Constructor cl : constructors) { System.out.println(cl); System.out.println("访问修饰符:"+Modifier.toString(cl.getModifiers())); System.out.println("构造器名称:"+cl.getName()); System.out.println("参数类型数组:"+Arrays.toString(cl.getParameterTypes())); } // 获取一个构造器对象 Constructor conl = clazz.getDeclaredConstructor(int.class, String.class); // 开启任何构造函数的访问权限 conl.setAccessible(true); // 通过参数个数和类型创建对象的实例 Object object = conl.newInstance(100, "AAA"); System.out.println(object); } }