黑马程序员-反射机制

反射故名思意,就是将一个对象映射出来。
类型能看清里面的架构。
就好比一个镜子里照映出自己的影子一样。

1、如何获取对象的类型名?

class Sample1{}
public class ClassReflection {
	public static void main(String[] args) {
		//对象o
		Object o = new Sample1();
		//打印对象o的类型名
		System.out.println(o.getClass().getName());
		//可以直接以类型来获取类型名
		System.out.println(Sample1.class.getName());
	}
}


2、如何获取对象的构造方法?
class Sample1{
	String str = "";
	public Sample1() {
		str="xxx";
	}
	public Sample1(String str) {
		this.str = str;
	}
	@Override
	public String toString() {
		return "Sample1 [str=" + str + "]";
	}
}
public class ClassReflection {
	public static void main(String[] args) {	
		try {
			//new 样品类构造方法
			Object o2 = Sample1.class.newInstance();
			System.out.println("o2 : " + o2);
			//获取全部构造方法
			Constructor[] con = o2.getClass().getConstructors();
			//实现带参数构造方法		
			Object o3 = con[1].newInstance("dfajkdf");
			System.out.println("o3 : " + o3);
			//数组1的构造方法的方法名
			System.out.println(con[1].toGenericString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2.1、用forName()获取类型
class Sample1{
	String str = "";
	public Sample1() {
		str="xxx";
	}
	public Sample1(String str) {
		this.str = str;
	}
	@Override
	public String toString() {
		return "Sample1 [str=" + str + "]";
	}
}
public class ClassReflection {
	public static void main(String[] args) {
		try {
			//对象o
			Object o = Class.forName("Sample1").newInstance();
			System.out.println(o);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


2.3、获取私有构造方法
class Sample1{
	String str = "";
	public Sample1() {
		str="xxx";
	}
	public Sample1(String str) {
		this.str = str;
	}
	@Override
	public String toString() {
		return "Sample1 [str=" + str + "]";
	}
}
public class ClassReflection {
	public static void main(String[] args) {
		try {
			Class c = Class.forName("Sample1");
			Constructor cons = c.getDeclaredConstructor();
			cons.setAccessible(true);
			Object o = cons.newInstance(null);
			System.out.print(o);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}



3、如何获取方法?
class Sample1{
	String str = "";
	public Sample1() {
		str="xxx";
	}
	public Sample1(String str) {
		this.str = str;
	}
	@Override
	public String toString() {
		return "Sample1 [str=" + str + "]";
	}
	public void say() {
		System.out.println("xx");
	}
}
public class ClassReflection {
	public static void main(String[] args) {
		try {
			Object o4 = Sample1.class.newInstance();
			//获取对象的类型的所有公有方法,不包括private和protected
			Method[] ms = o4.getClass().getMethods();
			//包括父类的方法
			System.out.println(ms.length);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


4、实现带参数方法
class Sample1{
	String str = "";
	public Sample1() {
		str="xxx";
	}
	public Sample1(String str) {
		this.str = str;
	}
	@Override
	public String toString() {
		return "Sample1 [str=" + str + "]";
	}
	public String say(String str) {
		return str;
	}
}
public class ClassReflection {
	public static void main(String[] args) {


		try {
			Object o4 = Sample1.class.newInstance();
			//getMethod() 第一个参数是方法名 第二个参数方法返回值类型
			Method m = o4.getClass().getMethod("say", String.class);
			//invoke() 第一个参数指定实现的对象是哪个,第二个参数是以后的参数是实现方法里面的参数
			//方法里面参数若多个 m.invoke(o4, "hehe",(Object),.....) 就往后加即可
			System.out.println(m.invoke(o4, "hehe"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


5、获取注解?
//WebService注解
@WebService(name="sample1")
class Sample1{
}
public class ClassReflection {
	public static void main(String[] args) {


		try {
			Object o5 = Sample1.class.newInstance();
			//获取在Sample1类型里面的@WebService注解
			WebService ws = o5.getClass().getAnnotation(WebService.class);
			//注解name参数
			System.out.println(ws.name());
			//注解portName参数
			System.out.println(ws.portName());
			//注解serviceName参数
			System.out.println(ws.serviceName());
			//注解targetNamespace参数
			System.out.println(ws.targetNamespace());
			//注解endpointInterface参数
			System.out.println(ws.endpointInterface());
			//注解wsdlLocation参数
			System.out.println(ws.wsdlLocation());
			//注解的类型
			System.out.println(ws.annotationType());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值