java-反射-getFields、getDeclaredFields

1.概述

1.1.getFields:获取类的全部公共属性,包括继承来的(但是私有和保护的成员不在内)。

1.2.getDeclaredFields:获取类的全部属性(包括私有和保护的属性),不包括继承来的。

2.代码

package test;

import java.lang.reflect.Field;

public class FieldTest {

    public static void main(String[] args) {
	// TODO Auto-generated method stub
	FieldTest fieldTest = new FieldTest();
	fieldTest.main();
    }

    public void main() {
	System.out.println("getFields:获取对象的全部公共属性,包括继承来的");
	showFieldName();
	System.out.println("getDeclaredFields:获取对象的全部属性,不包括继承的");
	showDeclaredField();
    }
    public void showFieldName(){
	D d = new D();
	Field[] field = d.getClass().getFields();
	for (Field f : field) {
	    System.out.println(f.getName());
	}
    } 
    public void showDeclaredField(){
	D d = new D();
	CreateClass.create(d);
	//showDeclaredField(d.getClass());
    }
    public void showDeclaredField(Class ca){
	Field[] field = ca.getDeclaredFields();
	for (Field f : field) {
	    System.out.println(f.getName());
	}
	if(ca.getSuperclass()!=null){
	    showDeclaredField(ca.getSuperclass());
	}
	
    }
   

    class A {
	public String ma;
    }

    class B extends A {
	protected String mb;
    }

    class C extends B {
	private String mc;
    }

    class D extends C {
	private String md2;
	public String md;
    }
}
class CreateClass{
    private Object obj;
    private CreateClass(Object o){
	this.obj = o;
    }
    static public void create(Object o){
	new CreateClass(o).showDeclaredField(o.getClass());
    }
    public void showDeclaredField(Class ca){
	Field[] field = ca.getDeclaredFields();
	for (Field f : field) {
	    if(!"this$0".equals(f.getName())){
		System.out.println(f.getName());
	    }
	}
	if(ca.getSuperclass()!=null){
	    showDeclaredField(ca.getSuperclass());
	}
    }
}

3.运行结果

getFields:获取对象的全部公共属性,包括继承来的
md
ma
getDeclaredFields:获取对象的全部属性,不包括继承的
md2
md
mc
mb
ma

4.补记录

4.1 第二版代码

package test;

import java.lang.reflect.Field;

public class FieldTest {

    public static void main(String[] args) {
	// TODO Auto-generated method stub
	FieldTest fieldTest = new FieldTest();
	fieldTest.main();
    }

    public void main() {
	System.out.println("getFields:获取对象的全部公共属性,包括继承来的");
	showFieldName();
	System.out.println("getDeclaredFields:获取对象的全部属性,不包括继承的");
	showDeclaredField();
    }
    public void showFieldName(){
	D d = new D();
	Field[] field = d.getClass().getFields();
	for (Field f : field) {
	    System.out.println(f.getName());
	}
    } 
    public void showDeclaredField(){
	D d = new D();
	CreateClass.create(d);
	//showDeclaredField(d.getClass());
    }
    public void showDeclaredField(Class ca){
	Field[] field = ca.getDeclaredFields();
	for (Field f : field) {
	    System.out.println(f.getName());
	}
	if(ca.getSuperclass()!=null){
	    showDeclaredField(ca.getSuperclass());
	}
	
    }
   

    class A {
	public String ma;
    }

    class B extends A {
	protected String mb;
    }

    class C extends B {
	private String mc;
    }

    class D extends C {
	private String md2;
	public String md;
    }
}
class CreateClass{
    private Object obj;
    private CreateClass(Object o){
	this.obj = o;
    }
    static public void create(Object o){
	new CreateClass(o).showDeclaredField(o.getClass());
    }
    public void showDeclaredField(Class ca){
	Field[] field = ca.getDeclaredFields();
	for (Field f : field) {
	    if(!"this$0".equals(f.getName())){
		System.out.println(f.getName());
		try{
		    f.setAccessible(true);
		    f.set(this.obj,f.getName());
		}catch(Exception e){
		    System.out.println(e.toString());
		}
		
	    }
	}
	if(ca.getSuperclass()!=null){
	    showDeclaredField(ca.getSuperclass());
	}
    }
}

 

你可能感兴趣的:(java-编码笔记)