1.定义Worker类,通过三种方式找出Worker类对象
2.通过类对象找出所有的成员变量和成员方法
3.通过类对象获取构造对象并创建目标对象
4.给目标对象设置属性值
5.调用公共方法showWorkerInfo()
6.调用私有方法showSalary()
package test0411;
//定义Worker类
public class Worker {
//成员变量
private Integer id;
private String company;
private String name;
private Double salary;
//getter and setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
//无参构造
public Worker() {
}
//带参构造
public Worker(Integer id, String company, String name, Double salary) {
this.id = id;
this.company = company;
this.name = name;
this.salary = salary;
}
//toString方法
@Override
public String toString() {
return "Worker{" +
"id=" + id +
", company='" + company + '\'' +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
//公共成员方法showWorkInfo()
public void showWorkInfo(String name, String company, Integer id,Double salary){
System.out.println(name+"\t "+company+"" +
"\t"+id+"\t"+salary);
}
//私有成员方法showSalary()
private void showSalary(String name){
if (name.equals(this.name)){
System.out.println(salary);
}else {
System.out.println("保密");
}
}
import test0411.Worker;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//定义测试类
public class TestWorker {
public static void main(String[] args) {
//1.通过类名.class获取类对象
Class c = Worker.class;
System.out.println(c);
//2.通过类的实例对象的getClass()方法获取类对象
Worker worker = new Worker();
Class c1 = worker.getClass();
//3.通过Class类的forName()静态方法获取类对象,有异常抛出
try {
Class c2 = Class.forName("test0411.TestWorker");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
System.out.println("-----------------------------------------------");
//通过类对象找出所有的成员变量
Field[] declaredFields = c.getDeclaredFields();
for (Field f : declaredFields) {
System.out.println(f.getName());
}
System.out.println("------------------------------------------------");
//通过类对象找出所有的成员方法
Method[] declaredMethods = c.getDeclaredMethods();
for (Method m : declaredMethods) {
System.out.println(m.getName());
}
System.out.println("-----------------------------------------------------");
//通过获取到的构造方法给目标对象设置属性值
try {
Constructor constructor = c.getConstructor(Integer.class, String.class, String.class, Double.class);
Worker w =(Worker) constructor.newInstance(1,"强盛集团","lj",12000.0); //w是通过Constructor类的newInstance()方法创建的Worker类的对象
System.out.println(w);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
System.out.println("------------------------------------------------");
//调用公共方法showWorkInfo()
Worker worker1 = new Worker(2,"强盛集团","赵德彪",8000.0);
try {
Method m = c.getMethod("showWorkInfo",String.class, String.class, Integer.class, Double.class);
m.invoke(worker1, "赵德彪", "强盛集团", 2, 8000.0);
System.out.println(worker1);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
System.out.println("--------------------------------------------------");
//调用私有方法showSalary()
Worker worker2 = new Worker(); //创建实例设置属性name和salary
worker2.setName("hx");
worker2.setSalary(13000.0);
System.out.println(worker2);
System.out.println(worker2.toString()); //输出对象默认调用ToString方法
try {
Method m1 = c.getDeclaredMethod("showSalary", String.class);
m1.setAccessible(true); //调用私有方法,临时取消权限校验
m1.invoke(worker2,"hx");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
输出结果:
class test0411.Worker
-----------------------------------------------
id
company
name
salary
------------------------------------------------
toString
getName
getId
setName
showSalary
showWorkInfo
setSalary
setId
getSalary
getCompany
setCompany
-----------------------------------------------------
Worker{id=1, company='强盛集团', name='lj', salary=12000.0}
------------------------------------------------
赵德彪 强盛集团 2 8000.0
Worker{id=2, company='强盛集团', name='赵德彪', salary=8000.0}
--------------------------------------------------
Worker{id=null, company='null', name='hx', salary=13000.0}
Worker{id=null, company='null', name='hx', salary=13000.0}
13000.0
Process finished with exit code 0