Java学习之路0804(线程、注解)

线程、工厂模式

线程Join

public class Join implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 20; i++) {
            System.out.println("卖了"+i+"张票");
        }
    }
}
public class JoinTest {
    public static void main(String[] args) {
        Join join=new Join();
        Thread t1=new Thread(join);
        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("票卖完了");
    }
}

工厂模式

/*
 * 工厂设计模式
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Factory {
    public static Person creatPerson(){
        Person person=null;
        Properties p=new Properties();
        try {
            p.load(new FileInputStream("config.properties"));
            String clazzName=p.getProperty("person");
            Class clazz=Class.forName(clazzName);
            person=(Person) clazz.newInstance();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return person;
    }
}
/*
 * 工厂设计模式
 */
public class FoctoryTest {
    public static void main(String[] args) {
        Person person=Factory.creatPerson();
        person.sleep();
    }
}
/*
 * 工厂设计模式
 */
public class Person {
    public void sleep(){
        System.out.println("人睡觉");
    }
}
* 注解Annotation
 */
import java.lang.reflect.Field;

public class Student extends Person{
    @Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("学生睡觉");
    }
    }

还需要一个file文件,名称为:config.properties
person=jie0804.Student
运行结果:学生睡觉

注解

* 注解Annotation
 */
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.lang.model.element.Element;

@Retention(RetentionPolicy.RUNTIME)//表示此注解的范围是可执行的
@Target(ElementType.FIELD)//表示此注解只能用到属性上
public @interface StudentAnnotation {
    int value();//表明此注解可以注解一个int值
}
* 注解Annotation
 */
import java.lang.reflect.Field;

public class Student extends Person{
    @Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("学生睡觉");
    }
    @StudentAnnotation(19)
    private int age;
    public Student() {
        // TODO Auto-generated constructor stub
        Class clazz=Student.class;//得到Student类的Class对象
        try {
            Field field=clazz.getDeclaredField("age");//得到age的属性
            StudentAnnotation sa=field.getAnnotation(StudentAnnotation.class);
            //得到age的属性的StudentAnnotation的注解,如果没有返回null
            if (sa!=null) {
                int i=sa.value();//得到属性的默认值
                field.setAccessible(true);//设置为不检查访问的修饰符
                field.set(this, i);//this指的是Student对象
                field.setAccessible(false);//设置为检查访问的修饰符
            }
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (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();
        }
    }
    private String name;
    public int num;
    public char sex;
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }

}
* 注解Annotation
 */
import java.lang.reflect.Field;

public class StudentTest {
    public static void main(String[] args) {
        Class clazz=Student.class;
        Field[] fields=clazz.getDeclaredFields();
        for (Field field:fields) {
            System.out.println(field.getType().getName());
        }
        Student zhangsan=new Student();
        System.out.println(zhangsan.getAge()+"注解的");
        try {
            Field age=clazz.getDeclaredField("age");
            age.setAccessible(true);
            age.set(zhangsan, 18);
            age.setAccessible(false);
            System.out.println(zhangsan.getAge());

            Field name=clazz.getDeclaredField("name");
            name.setAccessible(true);
            name.set(zhangsan, "张三");
            name.setAccessible(false);
            System.out.println(zhangsan.getName());

            Field sex=clazz.getDeclaredField("sex");
            sex.setAccessible(true);
            sex.set(zhangsan, 'f');
            sex.setAccessible(false);
            System.out.println(zhangsan.getSex());
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (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();
        }
    }
}

运行结果:
int
java.lang.String
int
char
19注解的
18
张三
f

/*
 * Annotation注解
 */
public class Teacher extends Person{
    private int age;
    private String name;

//  public Teacher(int age,String name) {       
//      // TODO Auto-generated constructor stub
//      this.age=age;
//      this.name=name;
//  }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}
* Annotation注解
 */
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TeacherAnnotation {
    int age() default 20;// 如果age在注解中没有声明就使用默认值Default
    String name();
}
* Annotation注解
 */
import java.lang.reflect.Field;

public class TeacherClazz {
    @TeacherAnnotation(age=19,name="张三")
    public Teacher javaTeacher;
    @TeacherAnnotation(name="李四")
    public Teacher enghishTeacher;
    @TeacherAnnotation(name="王五")
    public Teacher mathTeacher;
    public TeacherClazz() {
        Class clazz=TeacherClazz.class;
        Field[] fields=clazz.getDeclaredFields();
        //得到clazz的所有属性
        for (Field field:fields) {
            TeacherAnnotation ta=field.getAnnotation(TeacherAnnotation.class);
            //得到TeacherAnnotation注解,如果没有返回null
            if (ta==null) {
                continue;
            }else{
                int age=ta.age();//得到该注解的age值,如果没有则默认值
                String name=ta.name();//得到注解的name值
                Teacher teacher=new Teacher();
                teacher.setAge(age);
                teacher.setName(name);//根据注解的值创建新对象
                field.setAccessible(true);//设置为不检查属性的修饰符
                try {
                    field.set(this, teacher);//将此teacher对象赋值给此班级对象的属性
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                field.setAccessible(false);         
            }
        }
    }

}   
*
 * Annotation注解
 */
public class TeacherAnnotationTest {
    public static void main(String[] args) {
        TeacherClazz clazz=new TeacherClazz();
        System.out.println(clazz.javaTeacher.getName()+clazz.javaTeacher.getAge());
        System.out.println(clazz.mathTeacher.getName()+clazz.mathTeacher.getAge());
        System.out.println(clazz.enghishTeacher.getName()+clazz.enghishTeacher.getAge());
    }
}

运行结果:
张三19
王五20
李四20

你可能感兴趣的:(java学习)