JAVA中更好更快的掌握反射
JAVA反射学习
1. 尽量用这个方法去总结 可以更好的掌握java的思维逻辑
shan
由于反射知识点零碎,所以可以由以下代码快速理解反射应用和记住这些知识点
总结方法:
**
- 记忆方法一
**
Constructor构造方法类
构造方法的修饰符
getModifiers()
构造方法的名字 getName()
构造方法的参数类型
getParameterTypes()
获取构造方法
获取所有公有构造方法 class.getConstructors()
获取指定的公有构造方法class.getConstructors(Class<?>...ParameterTypes)
获取所有构造方法class.getDeclareConstructors()
获取指定的构造方法class.getDeclareConstructors(Class<?>...ParameterTypes)
Constructor 构造对象
public T
newInstance(Object… initargs) 返回值是一个是一个泛型
例子
Object obj1=(Object)
Constructor. newInstance();
Object obj2=(Object)
Constructor. newInstance(“参数1”);
Object obj3=(Object)Constructor. newInstance(“参数1”, “参数2”, “参数3”);
Object Parameters[]={“参数1”, “参数2”, “参数3” ,“参数4”, “参数5”, “参数6”};
Object obj4=(Object)Constructor. newInstance(parameters);
**
- 记忆方法二
**
以下是这些方法的操作
package class类与java反射;
public class Example implements MyInterface1, MYjiekou2 {
private int id;
String name;
int age;
public double price;
public String desc;
private Example() {
super();
}
private Example(int id) {
super();
this.id = id;
}
Example(String name) {
super();
this.name = name;
}
public Example(int id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setID(int id) {
this.id = id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getID() {
return id;
}
private void SiyouMethod() {
System.out.println("我是私有方法");
}
public void shuxie() {
System.out.println("现身吧");
}
private void shuchuxingming(String name) {
System.out.println();
}
public void interfaceInteger() {
}
public void jiekou2() {
}
public void dayin() {
System.out.println