Recent research framework underlying code of the process feel less solid foundation, then wrote that case, in case you ever forget
Interfaces: Animals
1 public interface Animals { 2 3 public void eat(); 4 }
package cn.chenc.study.entity; public interface InterfaceFactory { public String show(int i); }
Entity classes: Person
package cn.chenc.study.entity; import java.lang.reflect.Proxy; public class Person implements Animals { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = 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; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public void show(String name,int age){ System.out.println("name="+name+",age="+age); } public void eat(){ System.out.println ( "People eating utensils" ); } public void RUN (S String, int I) { System.out.println ( "human" + i + "only" + s + "walking" ); } }
Test categories:
package cn.chenc.study; import cn.chenc.study.entity.Animals; import cn.chenc.study.entity.InterfaceFactory; import cn.chenc.study.entity.Person; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class Demo1 { public static void main(String[] args) throws Exception{ //调用toString Class clazz=Class.forName("cn.chenc.study.entity.Person"); Method method=clazz.getMethod("toString"); Constructor constructor=clazz.getConstructor(String.class,int.class); Object object=constructor.newInstance("secret",21); System.out.println(method.invoke(object,null)); //调用show method=clazz.getMethod("show",String.class,int.class); method.invoke(object,"chen",19); //接口测试,person实现animals Class[] interfaces= clazz.getInterfaces(); Class childClass=interfaces.getClass(); Class inter=interfaces[0]; method=inter.getMethod("eat"); method.invoke(object); //代理测试 Animals proxy = (Animals) Proxy.newProxyInstance(clazz.getClassLoader(), interfaces, (proxy1, method1, args_temp) -> { Object result = method1.invoke(object, args_temp); return result; }); proxy.run("脚", 2); //lanmbda 函数式接口 lambdaTest((a) -> { // return new Person(); return String.valueOf(a); }); } public static void lambdaTest(InterfaceFactory interfaceFactory){ System.out.println(interfaceFactory.show(1)); } }
to sum up:
Function interface:
InterfaceFactory interfaceFactory=
(A) -> {
// new new return the Person ();
return String.valueOf (A);
};
In fact here is the creation of an anonymous subclass object, and implements methods show, show method is a parameter of type int The return value of type String. Function interface has only one abstract method, there are many non-abstract methods, such as static methods. In the interface,
the variable default puublic static final
method is the default public abstract
The first three are calling achieved by specifying a method and className method, if the mass participation also need getMethod, the feeling is not very flexible.
If you use a dynamic proxy, then you can directly use the interface to call, and also to achieve enhancement method.
Proxy class is used to create a proxy object's class, our most commonly used method is newProxyInstance.
InvocationHandler is also a very important dynamic agent interface, which has a invoke method, I have here is the use of lambda expressions to implement this interface.