一.反射类中的属性
在Java中一切皆对象,那么类中任何一个属性都可以看做一个对象,一 个属性对应一个Field对象
1.先建一个普通的类,写上不同修饰类型的属性
public class Student {
public String name;
protected int age;
String sex;
private double money;
public static String phone;
public static final String email = "xiaoma@qq.com";
public Date brithday;
public Student() {
this.name = "tom";
System.out.println("无参构造方法调用...");
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name) {
this.name = name;
System.out.println("一参构造方法调用...");
}
private Student(double money) {
this.money = money;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public static String getPhone() {
return phone;
}
public static void setPhone(String phone) {
Student.phone = phone;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public static String getEmail() {
return email;
}
private void sleep() {
System.out.println("睡啊睡...");
}
public void a(int a, int b) {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
2,反射加载类并实例化
Class c = Class.forName("com.accp.entity.Student");
Student stu = (Student)c.newInstance();
3,获取指定名称的公有属性
Field f1 = c.getField("name")
System.out.println(f1)
stu.name = "james"
f1.set(stu, "james")
System.out.println(f1.get(stu))
4,获取公有(静态)属性
Field ff = c.getField("phone")
stu.name = "138888"
ff.set(stu, "138888")
System.out.println(ff.get(stu))
5,获取指定名称的非公有属性
Field f2 = c.getDeclaredField("money");
f2.setAccessible(true);
f2.set(stu, 8888.8);
System.out.println(f2.get(stu));
6,获取指定类的所有公有属性
Field [] fields = c.getFields()
for(Field ff:fields){
System.out.println(ff)
}
7,获取指定类的所有公有属性
Field [] fields = c.getFields()
System.out.println("访问修饰符\t属性类型\t属性的名字")
for(Field ff:fields){
System.out.println(soEasy(ff.getModifiers())+"\t"+
ff.getType().getSimpleName()+"\t"+
ff.getName())
}
public static String soEasy(int modifier){
return Modifier.toString(modifier)
}
8,获取所有的属性
Field [] fields = c.getDeclaredFields()
for(Field ff:fields){
//System.out.println(ff)
System.out.println(soEasy(ff.getModifiers())+"\t"+
ff.getType().getSimpleName()+"\t"+
ff.getName())
}
二.反射类中的方法
在Java中一切皆对象,那么类中任何一个方法都可以看做一个对象,一个方法对应一个Method对象
Class c = Class.forName("com.accp.entity.Student")
Student stu = (Student)c.newInstance()
1,获了指定名称的公有方法
Method m1 = c.getMethod("setName", String.class)
m1.invoke(stu, "小牛")
Method m2 = c.getMethod("getName")
System.out.println(m2.invoke(stu))
2,获取指定名称的非公有方法
Method m3 = c.getDeclaredMethod("sleep");
m3.setAccessible(true);
m3.invoke(stu);
3,获取全部的公有方法(包括父亲的);
Method [] ms1 = c.getMethods();
for(Method mm:ms1)
4,获取自己声明方法(公有、私有),就没有获取父亲的方法
Method [] ms1 = c.getMethods()
for(Method mm:ms1){
//System.out.println(mm)
System.out.println(soEasy(mm.getModifiers())+"\t"+
mm.getReturnType().getSimpleName()+"\t"+
parse(mm.getParameterTypes()))
}
public static String parse(Class [] cs){
StringBuffer sb = new StringBuffer()
sb.append("(")
for(int i=0
//获取class对象对应的名字,如:String.class-->String
sb.append(cs[i].getSimpleName())
//多个参数中间加一个逗号
//条件是最后一个参数后面不加逗号
if(i<cs.length-1){
sb.append(",")
}
}
sb.append(")")
return sb.toString()
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
三,反射类中的构造方法
所有的java文件都会编译成一个class文件,与Clas类型的对象相对应在这个Class类型的对象中,包含了这个java类的属性、方法、构造方法等所有的东西。通过Class类型的对象,获取其构造方法(Constructor):可以通过构造来new出对象.
1,获取反射类无参数构造方法
Class c = Class.forName("com.accp.entity.Student")
Student stu1 = (Student)c.newInstance()
System.out.println(stu1.getName())
Constructor ct1 = c.getConstructor()
Student stu1 = (Student)ct1.newInstance()
System.out.println(stu1.getName())
2,获取反射类一参数的构造方法
Constructor ct2 = c.getConstructor(String.class)
Student stu2 = (Student)ct2.newInstance("jack")
System.out.println(stu2.getName())
3,获取反射类两参数的构造方法
Constructor ct3 = c.getConstructor(String.class,int.class)
Student stu3 = (Student)ct3.newInstance("marry",23)
System.out.println(stu3.getName())
System.out.println(stu3.getAge())
注意:以上的获取构造方法的前提是公有的
4,获取非公有的构造方法时,需要获取是自己声明的。
Constructor ct4 = c.getDeclaredConstructor(double.class);
ct4.setAccessible(true);
Student stu4 = (Student)ct4.newInstance(2345.6);
System.out.println(stu4.getMoney());
5,获取所有的公有的构造方法的对象
Constructor [] cts1 = c.getConstructors()
for(Constructor cc:cts1){
System.out.println(cc)
}
System.out.println("==========================")
6,获取所有的构造方法的对象(包括私有的)
Constructor [] cts2 = c.getDeclaredConstructors();
for(Constructor cc:cts2)
补充: java中的可变参数
public static void test(int ... a){
int sum = 0;
for(int i=0;i<a.length;i++){
sum += a[i];
}
System.out.println(sum);
}
public static void main(String[] args){
test();
test(1);
test(1,2);
test(1,2,3);
}