Java枚举与反射

枚举类

1.有限的对象。

2.两个静态方法,一个静态代码块,一个私有默认构造器。

3.定义的变量都为常量(static final),类由 final 修饰。

4.枚举类型都会默认继承一个父类型: java.lang.Enum ,这还是一个抽象的泛型类

5.可在类中定义属性,方法,构造器(private)。类可继承接口

public enum Color {	//定义枚举
	RED,PINK	//编号从 0 开始
}
获取枚举对象的三种方式{
    Color c = Color.PINK;
    
    String name = "RED";
    Color c2 = Color.valueOf(name);
    
    Class class = Class.forName("com.briup.day.Color");
    String name2 = "PINK";
    Enum c3 = Enum.valueOf(class,name2);
}
反射

​ 反射是java中提供的一种机制,它允许我们在程序运行的时候,动态获取一个类中的基本信息,

​ 并且可以调用类中的属性、方法、构造器。

//获取Student类的三种方式
public void method() throws ClassNotFoundException {
		Class c = Student.class;
		Class c2 = Class.forName("com.briup.day11.Student");
		Student s = new Student();
		Class s3 = s.getClass();
}

//得到属性的修饰符,属性名,类型。
public void method3() throws ClassNotFoundException {
		Class c = Class.forName("com.briup.day11.Student");
		//c.getFields();	得到用public修饰的属性以及继承父类中的属性
		Field[] field = c.getDeclaredFields();//得到Student中的所有属性
		for (Field field2 : field) {
			System.out.println(Modifier.toString(field2.getModifiers()));
			System.out.println(field2.getName());
			System.out.println(field2.getType().getName());
			System.out.println("-----------------------");
		}
}

//得到Sudent类中的方法的 修饰符,方法名,返回值,参数列表
public void getMethod() throws ClassNotFoundException {
		Class c = Class.forName("com.briup.day11.Student");
		Method[] method = c.getMethods();
		for (Method method2 : method) {
			System.out.println(Modifier.toString(method2.getModifiers()));
			System.out.println(method2.getName());
			System.out.println(method2.getReturnType().getSimpleName());
			System.out.println(method2.getParameterCount());
			Class[] returnType = method2.getParameterTypes();
			System.out.println(Arrays.toString(returnType));
			System.out.println("-----------");
		}
}

//得到Student类的构造器名字与参数列表
public void getCons() throws ClassNotFoundException {
		Class c = Class.forName("com.briup.day11.Student");
		Constructor[] cons = c.getConstructors();
		for (Constructor cons2 : cons) {
			System.out.println(cons2.getName());
			Parameter[] ps = cons2.getParameters();
			System.out.println(Arrays.toString(ps));
			System.out.println("--------------");
		}
}

//修改引用s的属性值		setAccessible(true),设置属性可被访问
public void modifyClass() throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
		Student s =  new Student();
		Class c = Class.forName("com.briup.day11.Student");
		Field[] df = c.getDeclaredFields();
		System.out.println(Arrays.toString(df));
		Field f2 = c.getDeclaredField("add");
		System.out.println(f2.isAccessible());
		f2.setAccessible(true);
		f2.set(s, "China");
		
		Field f3 = c.getDeclaredField("id");
		f3.setAccessible(true);
		f3.set(s, 123);
		
		Field f4 = c.getDeclaredField("name");
		f4.setAccessible(true);
		f4.set(s, "Jack");
		
		Field f5 = c.getDeclaredField("score");
		f5.setAccessible(true);
		f5.set(s, 99);
		
		System.out.println(s);
}

//调用Student类的方法
public void userMethod() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		Student s =  new Student();
		Class c = s.getClass();
		
		Method[] m = c.getDeclaredMethods();
		System.out.println(Arrays.toString(m));
		
		Method m2 = c.getMethod("toString", null);
		Object rs = m2.invoke(s, null);
		System.out.println(rs);

		Method m3 = c.getMethod("setId", int.class);
		Object rs2 = m3.invoke(s, 1);
		
		Method m4 = c.getMethod("setName", String.class);
		Object rs3 = m4.invoke(s, "Tom");
		System.out.println(rs3);
		
		System.out.println(s);
}

//调用Student类的构造器创建对象
public void userCons() throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		Class c = Student.class;
		//相当于	Object o1 = new Student();
		Object o1 = c.newInstance();
		System.out.println(o1);
		
		Constructor cons = c.getConstructor(int.class, String.class, int.class);
		Object o2 = cons.newInstance(2,"Bob",68);
		System.out.println(o2);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值