首页 > 编程知识 正文

java对象的属性和方法的引用

时间:2023-05-06 20:04:41 阅读:241446 作者:4104

转载自:https://www.jianshu.com/p/9335c15c43cf

/** * @author hgl * @data 2019年1月10日 * @description 接口 */public interface TestInterface {public void innerMethod();} /** * @author hgl * @data 2019年1月10日 * @description */public class TestClass {public TestClass(){TestInterface ti = new TestInterface(){@Overridepublic void innerMethod() {classMethod();}};ti.innerMethod();Class clazz = ti.getClass();Field[] fields = clazz.getDeclaredFields();for(Field field : fields){System.out.println(field.getName());try {//返回指定对象上此 Field 表示的字段的值System.out.println(field.get(ti));} catch (IllegalArgumentException | IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Field declaredField;try { // this$0是上面打印出来的(field.getName)变量的名称,所以这里就直接使用了declaredField = clazz.getDeclaredField("this$0");System.out.println("反射this$0:"+declaredField.get(ti));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void classMethod(){System.out.println("匿名内部类调用了外部类方法");}}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。