反射的定义:
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
JAVA反射(放射)机制:“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言”。从这个观点看,Perl,Python,Ruby是动态语言,C++,Java,C#不是动态语言。但是JAVA有着一个非常突出的动态相关机制:Reflection,用在Java身上指的是我们可以在运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods。
.java文件的抽象
Field类中属性的抽象(访问修饰符、属性类型、属性名称)
method类中方法的抽象
Constructor类中构造器的抽象
//Student类中去掉setAge()方法
public class Student {
public String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public Student(String name,int age){
this.age=age;
this.name=name;
}
public Student() {
}
}
//******更改年龄***********
public static void main(String[] args) {
// Class<Student> stu=Student.class;
System.out.println("***********************************");
// Student zhangsan=new Student();
// Class<Student> stu= (Class<Student>) zhangsan.getClass();
System.out.println("************************************");
// Student zhangsan=new Student();
// try {
// Class<Student> clazz=(Class<Student>) Class.forName("com.lingzhuo.test.Student");
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println("*************************************");
Student zhangsan=new Student();
//反射
Class<Student> clazz=Student.class;
//得到student.Java的Class的抽象
try {
Field field=clazz.getDeclaredField("age");//得到某一个(age的)属性
field.setAccessible(true);//去掉访问修饰符
field.set(zhangsan, 31);//更改它的值
field.setAccessible(false);//在将访问修饰符改成false
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(zhangsan.getAge());
}
}
Annotation
//**************先生成一个注解****************
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface StudentAnnotation {
// 运行范围 一个类似注释不会编译 一个是会进行代码检查 一种是 运行时的注解
// 注解类型 给class注解一个给field注解 一个给method注解
// 一个给Constructor注解
// 可以传入值
String name();
//*****************在创建一个类************
public class Student {
// 给name加一个注解
@StudentAnnotation(name = "张三")
private String name;
private int age;
public Student() {
Class<Student> clazz = Student.class;
try {
Field fieldName = clazz.getDeclaredField("name");// 找到name属性
StudentAnnotation anno = fieldName.getAnnotation(StudentAnnotation.class);
String name = anno.name();
// fieldName.setAccessible(true);
fieldName.set(this, name);
// fieldName.setAccessible(false);
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public Student(String name, int age) {
this.age = age;
this.name = name;
}
}
//***********测试****************
public class Test {
public static void main(String[] args) {
Student stu = new Student();
System.out.println(stu.getName());
}
}
"生成一个TeacherAnnotation"
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TeacherAnnotation {
int age() default 19;
String name();
}
"一个Teacher类"
public class Teacher {
private String name;
private int 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;
}
}
"一个Clazz类"
public class Clazz {
@TeacherAnnotation(age = 20, name = "张三")
private Teacher javaTeacher;
@TeacherAnnotation(age = 21, name = "李四")
private Teacher englishTeacher;
@TeacherAnnotation(age = 22, name = "王五")
private Teacher mathTeacher;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Clazz() {
Class<Clazz> clazz = Clazz.class;
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
TeacherAnnotation ta = field.getAnnotation(TeacherAnnotation.class);
if (ta == null) {
continue;
} else {
int age = ta.age();
String name = ta.name();
Teacher teacher = new Teacher();
teacher.setName(name);
teacher.setAge(age);
// field.setAccessible(true);
try {
field.set(this, teacher);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// field.setAccessible(false);
}
}
}
public Teacher getJavaTeacher() {
return javaTeacher;
}
public void setJavaTeacher(Teacher javaTeacher) {
this.javaTeacher = javaTeacher;
}
public Teacher getEnglishTeacher() {
return englishTeacher;
}
public void setEnglishTeacher(Teacher englishTeacher) {
this.englishTeacher = englishTeacher;
}
public Teacher getMathTeacher() {
return mathTeacher;
}
public void setMathTeacher(Teacher mathTeacher) {
this.mathTeacher = mathTeacher;
}
}
"测试类"
public static void main(String[] args) {
Clazz clazz=new Clazz();
System.out.println( clazz.getEnglishTeacher().getName());
System.out.println(clazz.getMathTeacher().getAge());
}
线程实现的两种方式:
一种是继承Thread类,一种是实现Runnable接口,Runnable的优点是能实现数据共享。
注意:
必须重写run方法,必须调用start方法才是启动线程
线程的休眠 ( sleep) 休眠多少秒后唤醒
线程的优先级1-10
线程锁:同一时间只能有一个线程访问该同步代码块
public class MyRunnable implements Runnable {
private int cards = 1000;
private String lock = "abc";
@Override
public void run() {
while (cards > 0) {
synchronized (lock) {
if (cards > 0) {
System.out.println("我是线程" + Thread.currentThread().getName()+"我卖的票号"+cards);
cards--;
}
}
}
}
}
"***************测试类****************"
MyRunnable run=new MyRunnable();
Thread t1=new Thread(run);
Thread t2=new Thread(run);
Thread t3=new Thread(run);
t1.start();
t2.start();
t3.start();
同步方法
线程间通讯 wait释放此线程 notify唤醒等待的线程
线程生命周期
线程创建 线程的新生(调用start的方法)
休眠sleep 阻塞wait 唤醒notify
线程死亡
>
//*****************建立一个WanDa类***************
public class WanDa {
private int tickets = 1000;
public synchronized void sell() {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "我卖的票号" + tickets);
tickets--;
}
}
public void sellAll(){
while(tickets>0){
sell();
}
}
}
//*************建立一个MyRunnable类***************
public class MyRunnable implements Runnable{
private WanDa wd;
public MyRunnable(WanDa wd){
this.wd=wd;
}
@Override
public void run() {
wd.sellAll();
}
}
//***************测试类*****************
public static void main(String[] args) {
WanDa wd=new WanDa();
MyRunnable run=new MyRunnable(wd);
Thread t1=new Thread(run);
Thread t2=new Thread(run);
Thread t3=new Thread(run);
t1.start();
t2.start();
t3.start();
}
仓库 消费者 生产者 消费者查看仓库,如果有产品会消费产品,如果没有产品通知生产者生产者生产产品。生产者查看仓库,有就通知消费者消费产品,无则生产。
//****************消费者**************
public class Consumer extends Thread {
private Product pro;
public Consumer(Product pro){
this.pro=pro;
}
@Override
public void run() {
while(true){
synchronized (pro) {
if(pro.getNum()>0){
System.out.println("消费产品");
pro.setNum(0);
work(1000);
pro.notify();
}else{
System.out.println("等待生产者生产");
try {
pro.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
work(1000);
}
}
}
}
public void work(long time){
try {
sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//********************生产者**********************
public class Producer extends Thread{
private Product pro;
public Producer(Product pro){
this.pro=pro;
}
@Override
public void run() {
while(true){
synchronized (pro) {
if(pro.getNum()>0){
System.out.println("等待消费者消费");
work(1000);
try {
pro.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("生产者生产");
work(1000);
pro.setNum(1);
pro.notify();
}
}
}
}
public void work(long time){
try {
sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//******************仓库(产品)**********************
public class Product {
private int num=1;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
//***********************测试类*************
ublic static void main(String[] args) {
Product pro=new Product();
Producer producer=new Producer(pro);
Consumer consumer=new Consumer(pro);
producer.start();
consumer.start();
}