反射常用的一些方法
getName() 获取名称getSimpleName() 获取简称getModifiers() 获取访问修饰符getParameterTypes() 获取参数列表
反射获取Class类实例的三种方式:
对象.getClass()类名.clsssClass.forName(“全限定名”)
newInstance()
创建此 Class 对象所表示的类的一个新实例。
例如:Class<Goods> cls = Goods.class;
Goods = cls.newInstance();//调用的是无参构造
第三种和前面两种的区别:
前两种必须明确类型。后面是你只要提供这种类型的字符串就行,这种扩展更强,我不需要知道你的类,我只提供字符串,按照配置文件加载就可以了。
public class Goods {
private String name
;
private Double price
;
private Integer storage
;
....
}
public static void main(String
[] args
) throws ClassNotFoundException
{
Class
<?> cls
= Class
.forName("demo1.Goods");
Class
cls1 = Goods
.class;
Class
<? extends Goods> cls2
= new Goods().getClass();
}
获取运行时类实现的接口以及父类
(1)获取父类 getSuperclass() (2)获取实现接口 getInterfaces()
public interface Book {
}
public class Dog {
public Integer gid
;
public String gname
;
....
}
public class Goods extends Dog implements Book{
public Integer id
;
private String name
;
private Double price
;
private Integer storage
;
...
public static void main(String
[] args
) {
Class
cls = Goods
.class;
Class
[] interfaces
= cls
.getInterfaces();
for (Class
class1 : interfaces
) {
String name
= class1
.getName();
System
.out
.println(name
);
String simpleName
= class1
.getSimpleName();
System
.out
.println(simpleName
);
}
Class
cls2 = cls
.getSuperclass();
System
.out
.println(cls2
.getName());
System
.out
.println(cls2
.getSimpleName());
}
通过反射获取构造方法
getConstructor(Class<?>… parameterTypes) 获取指定公共构造
getConstructors() 获取本类所有的公共构造
getDeclaredConstructor(Class<?>… parameterTypes) 获取本类指定的构造(共有或私有)
getDeclaredConstructors() 获取本类所有的构造
创建对象
newInstance() con.newInstance(“zhangsan", 20);
加Declared和不加Declared区别?
加Declared可以获取本类所有的东西,不加Declared获取本类以及父类公共的东西 加s和不加s的区别?
加s获取是多个,返回数组。 不加s返回单个。
public static void main(String
[] args
) throws Exception
{
Class
<Goods> cls
= Goods
.class;
System
.out
.println("------------getConstructors()方法------------");
Constructor
[] constructors
= cls
.getConstructors();
for (Constructor constructor
: constructors
) {
int modifiers
= constructor
.getModifiers();
String string
= Modifier
.toString(modifiers
);
String name
= constructor
.getName();
Class
[] parameterTypes
= constructor
.getParameterTypes();
for (Class
class1 : parameterTypes
) {
String simpleName
= class1
.getSimpleName();
System
.out
.println(simpleName
);
}
System
.out
.println(string
+" "+name
);
}
System
.out
.println("------------getConstructor(Class<?>... parameterTypes)方法------------");
Constructor
<Goods> constructor
= cls
.getConstructor(String
.class,Double
.class,Integer
.class);
Goods goods
= constructor
.newInstance("张三",125.0,18);
System
.out
.println(goods
);
System
.out
.println("------------getDeclaredConstructors()方法------------");
Constructor
<?>[] declaredConstructors
= cls
.getDeclaredConstructors();
for (Constructor
<?> constructor2
: declaredConstructors
) {
int modifiers
= constructor2
.getModifiers();
String string
= Modifier
.toString(modifiers
);
String name
= constructor2
.getName();
Class
<?>[] parameterTypes
= constructor2
.getParameterTypes();
for (Class
<?> class1
: parameterTypes
) {
System
.out
.println(class1
.getSimpleName());
}
System
.out
.println(string
+" "+name
);
}
System
.out
.println("------------getDeclaredConstructor(Class<?>... parameterTypes)方法------------");
Constructor
<Goods> constructor2
= cls
.getDeclaredConstructor(String
.class,Integer
.class);
constructor2
.setAccessible(true);
Goods goods2
= constructor2
.newInstance("娃娃",289);
System
.out
.println(goods2
);
}
通过反射获取运行时类中成员变量
getFields() 获取本类以及父类所有的公共属性getField(String name) 获取本类以及父类中指定的属性(只能获取共有的)getDeclaredFields() 获取本类中所有的属性(共有、私有)getDeclaredField(String name) 获取本类中指定的属性(私有、共有都可以获取)
常用方法:
set(Object obj,Object value)
第一个参数:指定对象
第二个参数:需要设置的值
get(Object obj) 返回指定对象上此Field表示的字段的值
第一个参数:指定的对象
public static void main(String
[] args
) throws Exception
{
Goods g
= new Goods();
Class
<? extends Goods> cls
= g
.getClass();
Goods goods
= new Goods("洗发水", 12.0, 200);
System
.out
.println("--------------getDeclaredFields()------------");
Field
[] fields
= cls
.getDeclaredFields();
for (Field field
: fields
) {
field
.setAccessible(true);
int modifiers
= field
.getModifiers();
String string
= Modifier
.toString(modifiers
);
Class
<?> type
= field
.getType();
String simpleName
= type
.getSimpleName();
String name
= field
.getName();
Object value
= field
.get(goods
);
System
.out
.println(string
+" "+ simpleName
+" "+name
+" "+value
);
if("id".equals(name
)){
field
.set(goods
, 1);
}else if("name".equals(name
)){
field
.set(goods
, "洗面奶");
}else if("price".equals(name
)){
field
.set(goods
, 34.0);
}else if("storage".equals(name
)){
field
.set(goods
, 1000);
}
}
System
.out
.println(goods
);
System
.out
.println("--------------getDeclaredField(String name)------------");
Field field
= cls
.getDeclaredField("name");
field
.setAccessible(true);
int modifiers
= field
.getModifiers();
String string
= Modifier
.toString(modifiers
);
Class
<?> type
= field
.getType();
String simpleName
= type
.getSimpleName();
String name
= field
.getName();
field
.set(goods
, "洗洁精");
Object value
= field
.get(goods
);
System
.out
.println(string
+" "+ simpleName
+" "+name
+" "+value
);
System
.out
.println("--------------getFields() ------------");
Field
[] fields2
= cls
.getFields();
for (Field field2
: fields2
) {
int modifiers2
= field2
.getModifiers();
String string2
= Modifier
.toString(modifiers2
);
Class
<?> type2
= field2
.getType();
String simpleName2
= type2
.getSimpleName();
String name2
= field2
.getName();
System
.out
.println(string2
+" "+simpleName2
+ " "+name2
);
}
System
.out
.println("--------------getField(String name) ------------");
Field field2
= cls
.getField("gname");
int modifiers2
= field2
.getModifiers();
String string2
= Modifier
.toString(modifiers2
);
Class
<?> type2
= field2
.getType();
String simpleName2
= type2
.getSimpleName();
String name2
= field2
.getName();
field
.set(goods
, "旺财");
Object val
= field
.get(goods
);
System
.out
.println(string2
+" "+simpleName2
+ " "+name2
+" "+val
);
}
通过反射获取成员方法
getDeclaredMethods() 获取本类所有的成员方法(共有、私有)getDeclaredMethod(String name, Class<?>… parameterTypes) 获取本类指定的成员方法(共有、私有)getMethods() 获取本类以及父类所有的公共成员方法getMethod(String name, Class<?>… parameterTypes) 获取本类以及父类指定的公共方法
invoke(Object obj, Object... args)
第一个参数:调用那个对象的方法
第二个参数:需要设置的值
public static void main(String
[] args
) throws Exception
{
Class
<Goods> cls
= Goods
.class;
Goods goods
= cls
.newInstance();
System
.out
.println("-------------getDeclaredMethods()方法的演示-------------");
Method
[] declaredMethods
= cls
.getDeclaredMethods();
for (Method method
: declaredMethods
) {
method
.setAccessible(true);
int modifiers
= method
.getModifiers();
String string
= Modifier
.toString(modifiers
);
Class
<?> returnType
= method
.getReturnType();
String simpleName
= returnType
.getSimpleName();
String name
= method
.getName();
Class
<?>[] parameterTypes
= method
.getParameterTypes();
for (Class
<?> class1
: parameterTypes
) {
System
.out
.println(class1
.getSimpleName());
}
System
.out
.println(string
+" "+simpleName
+" "+name
);
if(name
.equals("setName")){
method
.invoke(goods
, "在坚持坚持就可以用膳了.");
}
}
System
.out
.println(goods
);
System
.out
.println("-------------getDeclaredMethod(String name, Class<?>... parameterTypes)方法-------------");
Method method
= cls
.getDeclaredMethod("toString");
int modifiers
= method
.getModifiers();
String string
= Modifier
.toString(modifiers
);
Class
<?> returnType
= method
.getReturnType();
String simpleName
= returnType
.getSimpleName();
String name
= method
.getName();
System
.out
.println(string
+" "+simpleName
+" "+name
);
Object value
= method
.invoke(goods
, null
);
System
.out
.println(value
);
System
.out
.println("-------------getMethods()方法-------------");
Method
[] methods
= cls
.getMethods();
for (Method method2
: methods
) {
int modifiers2
= method2
.getModifiers();
String string2
= Modifier
.toString(modifiers2
);
String name2
= method2
.getName();
Class
<?> returnType2
= method2
.getReturnType();
String simpleName2
= returnType2
.getSimpleName();
Class
<?>[] parameterTypes
= method2
.getParameterTypes();
for (Class
<?> class1
: parameterTypes
) {
System
.out
.println(class1
.getSimpleName());
}
System
.out
.println(string2
+" "+name2
+" "+simpleName2
);
}
System
.out
.println("-------------getMethod(String name, Class<?>... parameterTypes)方法-------------");
Method method2
= cls
.getMethod("show", String
.class);
int modifiers2
= method2
.getModifiers();
String string2
= Modifier
.toString(modifiers2
);
String name2
= method2
.getName();
Class
<?> returnType2
= method2
.getReturnType();
String simpleName2
= returnType2
.getSimpleName();
Class
<?>[] parameterTypes
= method2
.getParameterTypes();
for (Class
<?> class1
: parameterTypes
) {
System
.out
.println(class1
.getSimpleName());
}
System
.out
.println(string2
+" "+name2
+" "+simpleName2
);
method2
.invoke(goods
, "我真帅");
}