反射_单例模式_注解

获取类对象

		//1使用对象获取类对象
		Person zhangsan=new Person();
		Class<?> class1=zhangsan.getClass();
		System.out.println(class1.hashCode());
		//2使用类名.class属性
		Class<?> class2=Person.class;
		System.out.println(class2.hashCode());
		//3使用Class的静态方法[推荐使用]
		Class<?> class3=Class.forName("com.qf.chap17_1.Person");
		System.out.println(class3.hashCode());

使用反射获取类的名字、包名、父类、接口

		//(1)获取类对象 Person
		Class<?> class1=Class.forName("com.qf.chap17_1.Person");
		//getName();
		System.out.println(class1.getName());
		//getPackage();
		System.out.println(class1.getPackage().getName());
		//getSuperClass();
		System.out.println(class1.getSuperclass().getName());
		//getInterfaces();
		Class<?>[] classes=class1.getInterfaces();
		System.out.println(Arrays.toString(classes));
		System.out.println(class1.getSimpleName());
		System.out.println(class1.getTypeName());

使用反射获取类的构造方法,并创建对象

		//(1)获取类的类对象
		Class<?> class1=Class.forName("com.qf.chap17_1.Person");
		//(2)获取类的构造方法 Constructor
		Constructor<?>[] cons=class1.getConstructors();
		for (Constructor<?> con : cons) {
			System.out.println(con.toString());
		}
		//(3)获取类中无参构造
		Constructor<?> con=class1.getConstructor();
		Person zhangsan=(Person)con.newInstance();
		Person lisi=(Person)con.newInstance();
		System.out.println(zhangsan.toString());
		System.out.println(lisi.toString());
		//简便方法:类对象.newInstance();
		Person wangwu=(Person)class1.newInstance();
		System.out.println(wangwu.toString());
		//(4)获取类中带参构造方法
		Constructor<?> con2=class1.getConstructor(String.class,int.class);
		Person xiaoli=(Person)con2.newInstance("晓丽",20);
		System.out.println(xiaoli.toString());

使用反射获取类中的方法,并调用方法

		//(1)获取类对象
		Class<?> class1=Class.forName("com.qf.chap17_1.Person");
		//(2)获取方法  Method对象
		//2.1getMethods() 获取公开的方法,包括从父类继承的方法
//		Method[] methods=class1.getMethods();
		//2.2getDeclaredMethods() 获取类中的所有方法,包括私有、默认、保护的 、不包含继承的方法
		Method[] methods=class1.getDeclaredMethods();
		for (Method method : methods) {
			System.out.println(method.toString());
		}
		//(3)获取单个方法
		//3.1eat
		Method eatMethod=class1.getMethod("eat");
		//调用方法
		//正常调用方法  Person zhangsan=new Person();  zhangsan.eat();
		Person zhangsan=(Person)class1.newInstance();
		eatMethod.invoke(zhangsan);//zhangsan.eat();
		System.out.println("------------------");
		//3.2toString
		Method toStringMethod=class1.getMethod("toString");
		Object result=toStringMethod.invoke(zhangsan);
		System.out.println(result);
		System.out.println("-------------------");
		//3.3带参的eat
		Method eatMethod2=class1.getMethod("eat", String.class);
		eatMethod2.invoke(zhangsan, "鸡腿");
		//3.4获取私有方法
		Method privateMethod=class1.getDeclaredMethod("privateMethod");
		//设置访问权限无效
		privateMethod.setAccessible(true);
		privateMethod.invoke(zhangsan);
		//3.4获取静态方法
		Method staticMethod=class1.getMethod("staticMethod");
		//正常调用 Person.staticMethod
		staticMethod.invoke(null);
---------------------------------------------------------
	//4使用反射实现一个可以调用任何对象方法的通用方法
	public static Object invokeAny(Object obj,String methodName,Class<?>[] types,Object...args) throws Exception {
		//1获取类对象
		Class<?> class1=obj.getClass();
		//2获取方法
		Method method=class1.getMethod(methodName, types);
		//3调用
		return method.invoke(obj, args);
	}

使用反射获取类中的属性

//(1)获取类对象
		Class<?> class1=Class.forName("com.qf.chap17_1.Person");
		//(2)获取属性(字段) 公开的字段,父类继承的字段
//		Field[] fields=class1.getFields();
		//getDeclaredFields()获取所有的属性,包括私有,默认 ,包含,
		Field[] fields=class1.getDeclaredFields();
		System.out.println(fields.length);
		for (Field field : fields) {
			System.out.println(field.toString());
		}
		//(3)获取name属性
		Field namefield=class1.getDeclaredField("name");
		namefield.setAccessible(true);
		//(4)赋值  正常调用  Person zhangsan=new Person(); zhangsan.name="张三";
		Person zhangsan=(Person)class1.newInstance();
		namefield.set(zhangsan, "张三"); //zhangsan.name="张三";
		//(5) 获取值
		System.out.println(namefield.get(zhangsan));// zhangsan.name

工厂设计模式

单例模式

  • 饿汉式
    • 优点:线程安全,缺点:声明周期太长,浪费空间
/**
 * 饿汉式单例
 * (1)首先创建一个常量
 * (2)构造方法改成私有的,类外部不能创建对象
 * (3)通过一个公开的方法,返回这个对象
 * 优点:线程安全,缺点:声明周期太长,浪费空间
 */
public class SingleTon{
	private static final SingleTon instance=new SingleTon();
	private SingleTon(){}
	public static SingleTon getInstance(){
		return instance;
	}
}
  • 懒汉式
    • 优点:声明周期短,节省空间 缺点:有线程安全问题
public class SingleTon2{
	private static SingleTon2 instance = null;
	private SingleTon2(){}
	public static SingleTon2 getInstance(){
		if(instance == null){
			instance = new SingleTon2();			
		}
		return instance;
	}
}
------------------------------------------------
改进后的懒汉式,线程安全
public class SingleTon2 {
	// 创建对象
	private static SingleTon2 instance = null;
	// 私有化构造方法
	private SingleTon2() {
	}
	// 静态方法
	public static  SingleTon2 getInstance() {
		if(instance==null) {//提高执行效率
			synchronized (SingleTon2.class) {
				if (instance == null) {
					instance = new SingleTon2();
				}
			}
		}
		return instance;
	}
}
  • 静态内部类的写法
public class SingleTon3{
	private SingleTon3(){}
	private static class Holder{
		static SingleTon3 instance = new SingleTon3();
	}
	public staic SingleTon3 getInstance(){
		return Holder.instance;
	}
}

枚举

  • 枚举中必须要包含枚举常量,也可以包含属性、方法、私有构造方法
  • 枚举常量必须在前面,多个常量之间使用逗号隔开,最后分号可写可不写

注解Annotation

  • 定义注解使用@interface关键字,注解中只能包含属性
  • 注解属性类型
    • String类型
    • 基本数据类型
    • Class类型
    • 枚举类型
    • 注解类型
    • 以上类型的一维数组
  • 元注解:用来描述注解的注解
    • @Retention:用于指定注解可以保留的域。
      • RetentionPolicy.CLASS:注解记录在class文件中,运行java程序时,JVM不会保留,此为默认值。
      • RetentionPolicy.RUNTIME:注解记录在class文件中,运行java程序时,JVM会保留,程序可通过反射获取该注解
      • RetentionPolicy.SOURCE:编译时直接丢弃这种策略的注解
    • @Target:指定注解用于修饰类的哪个成员。
@Retention(value=RetentionPolicy.RUNTIME)
@Target(value= {ElementType.METHOD})
public @interface PersonInfo {
	String name();
	int age();
	String sex();
}
----------------------------------------------------------------------------
	@PersonInfo(name="小岳岳",age=30,sex="男")
	public void show(String name,int age,String sex) {
		System.out.println(name+"==="+age+"===="+sex);
	}
----------------------------------------------------------------------------
		//(1)获取类对象
		Class<?> class1=Class.forName("com.qf.chap17_5.Person");
		//(2)获取方法
		Method method=class1.getMethod("show", String.class,int.class,String.class);
		//(3)获取方法上面的注解信息 personInfo=null
		PersonInfo personInfo=method.getAnnotation(PersonInfo.class);
		//(4)打印注解信息
		System.out.println(personInfo.name());
		System.out.println(personInfo.age());
		System.out.println(personInfo.sex());
		//(5)调用方法
		Person yueyue=(Person)class1.newInstance();
		method.invoke(yueyue, personInfo.name(),personInfo.age(),personInfo.sex());	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值