Class类反射、多线程

来源:互联网 发布:淘宝怎么设置员工账号 编辑:程序博客网 时间:2024/06/14 14:50

利用Class类反射可以获得普通类的属性和方法。

关于类的加载器:ClassLoader

掌握
  // 方法一:
  // ClassLoader loader = this.getClass().getClassLoader();
  // InputStream is = loader.getResourceAsStream("src\\jdbc.properties");
  // 方法二:
  FileInputStream is = new FileInputStream(new File("jdbc.properties"));
  // Properties pros = new Properties();
  // pros.load(is);
  Properties pros = new Properties();
  pros.load(is);
  String user = pros.getProperty("user");
  System.out.println(user);
  String pwd = pros.getProperty("password");
  System.out.println(pwd);
 }
 // 如何获取Class实例(三种方法)
 @Test
 public void test4() throws ClassNotFoundException {
  // 1.调用运行时类本身的.class属性
  Class clazz1 = Person.class;
  System.out.println(clazz1.getName());
  Class clazz2 = String.class;
  System.out.println(clazz2.getName());
  // 2.通过运行时类的对象获取
  Person p = new Person();
  Class clazz3 = p.getClass();
  System.out.println(clazz3.getName());
  // 3.通过Class的静态方法获取
  String className = "src.Person";
  Class<?> clazz4 = Class.forName(className);
  // clazz4.newInstance();
  System.out.println(clazz4.getName());
  // 4.(了解)通过类的加载器
  ClassLoader classLoader = (ClassLoader) this.getClass().getClassLoader();
  Class clazz5 = classLoader.loadClass(className);
 }
 /*
  * java.lang.Class:是反射的源头。 我们创建了一个类,通过编译(javac.exe)生成对应的.class文件
  * 之后我们使用java.exe加载(JVM的类加载器完成的)此.class文件
  * 加载到内存后,就是一个运行时类,存放在缓存区,那么这个运行时类本身就是 一个Class的实例 1.每一个运行时类只加载一次
  * 2.有了Class的实例以后,我们才可以进行如下的操作: (1)创建对应的运行时类的对象 (2)获取对应的运行时类的完整结构
  * (属性、方法、构造器、内部类、父类、所在的包、异常、注解) (3)*调用对应的运行时类指定的结构(属性、方法、构造器) (4)反射的应用:动态代理
  */
@Test
 public void test3() {
  Person p = new Person();
  Class clazz = p.getClass();// 通过运行时类的对象,调用其getClass(),返回其运行时类。
  System.out.println(clazz);
 }
 // 有了反射,可以通过反射创建一个类的对象,并调用其中的结构
 public void test2() throws Exception {
  Class<Person> clazz = Person.class;
  Class<String> clazz2 = String.class;
  // 1.创建clazz对应的运行时类Person类的对象
  Person p = (Person) clazz.newInstance();
  System.out.println(p);
  Field f1 = clazz.getField("name");
  f1.set(p, "薛之谦");
  System.out.println(p);
  // 2.非public的属性
  Field f2 = clazz.getDeclaredField("age");
  f2.setAccessible(true);
  f2.set(p, 20);
  System.out.println(p);
  // 3.通过反射调用运行时类的指定的属性
  Method m1 = clazz.getMethod("show");
  m1.invoke(p);
  Method m2 = clazz.getMethod("display", String.class);
  m2.invoke(p, "CHN");
 }

/*
 * 创建一个子线程,完成1-100之间自然数输出,同时主线程也执行同样的操作
 *
 * 创建多线程的第一种方式,继承java.lang.Thread类
 */
/*
 *
 *
 * Thread类的常用方法 1.start():启动线程并执行相应的run()方法 2.run() : 3. 4. 5.
 * 6.yield():调用此方法的线程释放当前CPU的执行权 7.join():在A线程中调用B线程的join()方法,表示当执行到此方法,A
 * 线程停止执行,直至B线程执行完毕,A线程再接着join()之后的代码执行 8.isAlive():判断当前线程是否还存活 9.sleep(long
 * l):显示地让当前线程睡眠1毫秒 10.线程通信:wait()、notify()、notifyAll() 设置线程的优先级
 * setPriority():设置线程优先级
 *
 *
 */

public class TestThread {
 public static <SubThread2> void main(String[] args) {
  // 3.创建一个子类的对象
  SubThread1 st = new SubThread1();
  st.setName("子线程1");
  st.start();
  Thread.currentThread().setName("=========主线程");
  // SubThread2 st1 = new SubThread2();
  // st1.run();
  /**
   * 4.调用线程的start()方法,有两个作用 (1)启动此线程 (2)调用相应的run()方法
   *
   *
   */
  // st.start();
  // st2.start();
  // 一个线程只能够执行一次start()
  // 不能通过Thread实现类的对象直接调run()去启动一个线程
  // st.run();
  for (int i = 1; i <= 100; i++) {
   System.out.println(Thread.currentThread().getName() + ":" + i);
   if (i % 10 == 0) {
    Thread.currentThread().yield();
   }
   if (i == 20) {
    try {
     st.join();
    } catch (InterruptedException e) {
     // TODO: handle exception
     e.printStackTrace();
    }
   }
  }
  System.out.println(st.isAlive());
 }
}
// 1.创建一个继承于Thread的子类
class SubThread1 extends Thread {
 // 2.重写Thread类的run()方法,方法内实现此子线程要完成的功能
 public void run() {
  for (int i = 0; i <= 100; i++) {
   try {
    Thread.currentThread().sleep(100);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(Thread.currentThread().getName() + ':' + i);
  }
  class SubThread2 extends Thread {
   // 2.重写Thread类的run()方法,方法内实现此子线程要完成的功能
   public void run() {
    for (int i = 0; i <= 100; i++) {
     if (i % 2 == 0) {
      System.out.println("偶数" + i);
     } else {
      System.out.println("奇数" + i);
     }
     try {
      Thread.currentThread().sleep(100);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     System.out.println(Thread.currentThread().getName() + ':' + i);
    }
   }
  }
 }
}

原创粉丝点击