import com.fly.bean.Person; public class Demo1_Reflect { public static void main(String[] args) throws ClassNotFoundException { //反射 Class class1 = Class.forName("com.fly.bean.Person"); Class class2 = Person.class; Person person = new Person(); Class class3 = person.getClass(); System.out.println(class1==class2); System.out.println(class2==class3); } }
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; public class Demo2_Reflect { //Class.forName读取文件 public static void main(String[] args) throws Exception { X x = new X(); x.run(new A1()); BufferedReader br = new BufferedReader(new FileReader ("config.properties")); //com.fly.reflect.A1 Class class1 = Class.forName(br.readLine()); A a = (A) class1.newInstance(); x.run(a); } } interface A{ public void method(); } class A1 implements A{ @Override public void method() { System.out.println("A1"); } } class A2 implements A{ @Override public void method() { System.out.println("A2"); } } class X{ public void run(A a) { a.method(); } }
import java.lang.reflect.Constructor; import com.fly.bean.Person; public class Demo3_Constructor { /** * Class类的newInstance()方法是使用该类无参的构造函数创建对象, 如果一个类没有无参的 构造函数, * 就不能这样创建了,可以调用Class类的getConstructor * (String.class,int.class)方法获取一个指定的构造函数然后再调用Constructor类的 newInstance */ public static void main(String[] args) throws Exception { Class class1 = Class.forName("com.fly.bean.Person"); Constructor c = class1.getConstructor(String.class,int.class);//获取有参构 造 Person p = (Person) c.newInstance("zhang",23);//通过有参构造创建对象 System.out.println(p); } }
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import com.fly.bean.Person; public class Demo4_Field { /** * Class.getField(String)方法可以获取类中的指定字段(可见的), * 如果是私有的可以用getDeclaedField("name")方法获取,通过set(obj, "李四")方法可以设 置指定对象上该字段的值, * 如果是私有的需要先调用setAccessible(true)设置访问权限,用获取的指定的字段调用get (obj)可以获取指定对象中该字段的值 * @throws Exception */ public static void main(String[] args) throws Exception { Class class1 = Class.forName("com.fly.bean.Person"); Constructor c = class1.getConstructor(String.class,int.class);//获取有参构 造 Person person = (Person) c.newInstance("zhang",21);//通过有参构造创建对象 Field field = class1.getDeclaredField("name");//暴力反射获取字段 field.setAccessible(true);//去除私有权限 field.set(person, "zhao"); System.out.println(person); } }
import java.lang.reflect.Constructor; import java.lang.reflect.Method; import com.fly.bean.Person; public class Demo5_Method { /** * Class.getMethod(String, Class...) 和 Class.getDeclaredMethod(String, * Class...)方法可以获取类中的指定方法,调用invoke(Object, * Object...)可以调用该方法,Class.getMethod("eat") invoke(obj) * Class.getMethod("eat",int.class) invoke(obj,10) * @throws Exception */ public static void main(String[] args) throws Exception { Class class1 = Class.forName("com.fly.bean.Person"); Constructor c = class1.getConstructor(String.class,int.class);//获取有参构 造 Person person = (Person) c.newInstance("zhang",21);//通过有参构造创建对象 Method m = class1.getMethod("eat"); //获取eat方法 m.invoke(person); Method m2 = class1.getMethod("eat", int.class);//获取有参的eat方法 m2.invoke(person, 4); } }
import java.lang.reflect.Method; import java.util.ArrayList; public class Test1 { /** * @param args * ArrayList<Integer>的一个对象,在这个集合中添加一个字符串数据,如何实现呢? * 泛型只在编译期有效,在运行期会被擦除掉 * @throws Exception */ public static void main(String[] args) throws Exception { ArrayList<Integer> list = new ArrayList<>(); list.add(111); list.add(222); Class clazz = Class.forName("java.util.ArrayList"); //获取字节码对象 Method m = clazz.getMethod("add", Object.class); //获取add方法 m.invoke(list, "abc"); System.out.println(list); } }
import java.lang.reflect.Field; public class Tool { //此方法可将obj对象中名为propertyName的属性的值设置为value。 public void setProperty(Object obj, String propertyName, Object value) throws Exception { Class clazz = obj.getClass(); //获取字节 码对象 Field f = clazz.getDeclaredField(propertyName); //暴力反射获取字段 f.setAccessible(true); // 去除权限 f.set(obj, value); } }
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class Test1 { public static void main(String[] args) { //动态代理 MyStudent student = new MyStudent(); student.A(); student.B(); MyStudent student2 = new MyStudent(); MyInvocationHandler m = new MyInvocationHandler(student2); Student s = (Student) Proxy.newProxyInstance(student2.getClass ().getClassLoader(), student2.getClass().getInterfaces(), m); s.A(); s.B(); /* aaa bbb 校验 aaa 清理 校验 bbb 清理*/ } } interface Student{ public void A(); public void B(); } class MyStudent implements Student{ @Override public void A() { System.out.println("aaa"); } @Override public void B() { System.out.println("bbb"); } } /* * public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces, * InvocationHandler h) */ class MyInvocationHandler implements InvocationHandler{ private Object target; public MyInvocationHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("校验"); method.invoke(target, args);//执行被代理target对象的方法 System.out.println("清理"); return null; } }
模板设计模式:
示例: //计算程序运行的时间: abstract class GetTime { public final long getTime() { long start = System.currentTimeMillis(); code(); long end = System.currentTimeMillis(); return end - start; } public abstract void code(); }
枚举:
自定义枚举类:
方式1: public class Week { public static final Week MON = new Week(); public static final Week TUE = new Week(); public static final Week WED = new Week(); private Week(){} //私有构造,不让其他 类创建本类对象 } 方式2: public class Week2 { public static final Week2 MON = new Week2("星期一"); public static final Week2 TUE = new Week2("星期二"); public static final Week2 WED = new Week2("星期三"); private String name; private Week2(String name){ this.name = name; } //私有构造,不让其他类创建本类对象 public String getName() { return name; } } 方式3: public abstract class Week3 { public static final Week3 MON = new Week3("星期一") { public void show() { System.out.println("星期一"); } }; public static final Week3 TUE = new Week3("星期二"){ public void show() { System.out.println("星期二"); } }; public static final Week3 WED = new Week3("星期三"){ public void show() { System.out.println("星期三"); } }; private String name; private Week3(String name){ this.name = name; } //私有构造,不让其他类创建本类对象 public String getName() { return name; } public abstract void show(); }
使用enum:
方式1: public enum Week { MON,TUE,WED; } 方式2: public enum Week2 { MON("星期一"),TUE("星期二"),WED("星期三");//必须放在第一行 private String name; private Week2(String name) { this.name = name; } public String getName() { return name; } public String toString() { return name; } } 方式3: public enum Week3 { MON("星期一"){ public void show() { System.out.println("星期一"); } },TUE("星期二"){ public void show() { System.out.println("星期二"); } },WED("星期三"){ public void show() { System.out.println("星期三"); } }; private String name; private Week3(String name) { this.name = name; } public String getName() { return name; } public abstract void show(); }
//在switch上的应用: Week3 mon = Week3.TUE; switch (mon) { case MON: System.out.println("星期一"); break; case TUE: System.out.println("星期二"); break; }
public class Demo2_Enum { /** * int ordinal() * int compareTo(E o) * String name() * String toString() * <T> T valueOf(Class<T> type,String name) * values() * 此方法虽然在JDK文档中查找不到,但每个枚举类都具有该方法,它遍历枚举类的所有枚举值 非常方便 */ public static void main(String[] args) { //demo1(); // Week2 mon = Week2.valueOf(Week2.class, "MON"); //通过字节码对象获 取枚举项 // System.out.println(mon); Week2[] arr = Week2.values(); for (Week2 week2 : arr) { System.out.println(week2); } } public static void demo1() { Week2 mon = Week2.MON; Week2 tue = Week2.TUE; Week2 wed = Week2.WED; /*System.out.println(mon.ordinal()); //枚举项都 是有编号的 System.out.println(tue.ordinal()); System.out.println(wed.ordinal()); System.out.println(mon.compareTo(tue)); //比较的是编号 System.out.println(mon.compareTo(wed));*/ System.out.println(mon.name()); //获取实例 名称 System.out.println(mon.toString()); //调用toString方法 } }
新特性:
public class Demo1_JDK7 { /** * A:二进制字面量 * B:数字字面量可以出现下划线 * C:switch 语句可以用字符串 * D:泛型简化,菱形泛型 * E:异常的多个catch合并,每个异常用或| * F:try-with-resources 语句,1.7版标准的异常处理代码,关流 * * 100_000 */ public static void main(String[] args) { System.out.println(0b110); System.out.println(100_000);//100000 } }
public class Test2 { public static void main(String[] args) { //1.8新特性 //接口中可以定义有方法体的方法,如果是非静态的,必须用default修饰 Inner.method(); DA da = new DA(); da.name(); da.run(); } } interface Inner{ public default void name() { System.out.println("name"); } public static void method() { System.out.println("method"); } } class DA implements Inner{ public void run() { final int num = 1; //final可以省略 class In{ public void f() { // nun = 3; //直接报错 System.out.println(num); } } In in = new In(); in.f(); } }