java程序员必备的基础知识注解和反射(五)
获取类对象的方法1 获取方式1、使用类 Class clazz = Dog.class; 2、使用全类名 Class aClass = Class.forName("com.ydl.Dog"); 3、使用对象 Dog dog = new Dog(); Class clazz = dog.getClass();2 对类对象操作//获取类名字 String name = clazz.getName(); //获取类加载器 ClassLoader classLoader = clazz.getClassLoader(); //获取资源 URL resource = clazz.getResource(""); //得到父类 Class superclass = clazz.getSuperclass(); //判断一个类是不是接口,数组等等boolean array = clazz.isArray(); boolean anInterface = clazz.isInterface(); //重点,使用class对象实例化一个对象 Object instance = clazz.newInstance();对成员变量的操作
在java中万物皆对象成员变量也是对象,他拥有操作一个对象的成 员变量的能力。 (1) 获取成员变量getFields只能获取被public修饰的成员变量,当然反射能牛,我们可以使用getDeclaredFields方法获取所有的成员变量。 //获取字段,只能获取公共的字段(public) Field name = clazz.getField("type"); Field[] fields = clazz.getFields(); //能获取所有的字段包括private Field color = clazz.getDeclaredField("color"); Field[] fields = clazz.getDeclaredFields(); System.out.println(color.getType());(2) 获取对象的属性Dog dog = new Dog(); dog.setColor("red"); Class clazz = Dog.class; Field color = clazz.getDeclaredField("color"); System.out.println(color.get(dog));
当然你要是明确类型你还能用以下方法, Int i = age.getInt(dog); xxx.getDouble(dog); xxx.getFloat(dog); xxx.getBoolean(dog); xxx.getChar(dog); //每一种基本类型都有对应方法(3) 设置对象的属性Dog dog = new Dog(); dog.setColor("red"); Class clazz = Dog.class; Field color = clazz.getDeclaredField("color"); color.set(dog,"blue"); System.out.println(dog.getColor());
当然如果你知道对应的类型,我们可以这样 xxx.setBoolean(dog,true); xxx.getDouble(dog,1.2); xxx.getFloat(dog,1.2F); xxx.getChar(dog,"A"); //每一种基本类型包装类都有对应方法 Field color = dogClass.getDeclaredField("color"); //暴力注入 color.setAccessible(true); color.set(dog,"red");3、对方法的操作(1) 获取方法
//根据名字和参数类型获取一个方法 Method method = clazz.getMethod("eat",String.class); Method[] methods = clazz.getMethods(); Method eat = clazz.getDeclaredMethod("eat", String.class); Method[] declaredMethods = clazz.getDeclaredMethods();(2) 对方法的操作Dog dog = new Dog(); dog.setColor("red"); Class clazz = Dog.class; //获取某个方法,名字,后边是参数类型 Method method = clazz.getMethod("eat",String.class); //拿到参数的个数 int parameterCount = method.getParameterCount(); //拿到方法的名字 String name = method.getName(); // 拿 到 参 数 的 类 型 数 组 Class<?>[] parameterTypes = method.getParameterTypes(); //拿到返回值类型 Class<?> returnType = method.getReturnType(); //重点。反射调用方法,传一个实例,和参数 method.invoke(dog,"热狗"); Class dogClass = Class.forName("com.xinzhi.Dog"); Object dog = dogClass.newInstance(); Method eat = dogClass.getMethod("eat"); eat.invoke(dog); Method eat2 = dogClass.getMethod("eat",String.class); eat2.invoke(dog,"meat"); Method eat3 = dogClass.getMethod("eat",String.class,int.class); eat3.invoke(dog,"meat",12);4、对构造器的操作(1)获取并构建对象Constructor[] constructors = clazz.getConstructors(); Constructor constructor = clazz.getConstructor(); Constructor[] declaredConstructors = clazz.getDeclaredConstructors(); Constructor declaredConstructor = clazz.getDeclaredConstructor(); Object obj = constructor.newInstance();5、对注解的操作(1)从方法、字段、类上获取注解//元注解 要加上runtime //类上 Annotation annotation = clazz.getAnnotation(Bean.class); Annotation[] annotations = clazz.getAnnotations(); //字段上 Annotation annotation = field.getAnnotation(Bean.class); Annotation[] annotations = field.getAnnotations(); //方法上 Annotation annotation = method.getAnnotation(Bean.class); Annotation[] annotations = method.getAnnotations();
续航焦虑不用愁,领克COPHEV家族目前,汽车行业正加速向电气化智能化网联化方向转变,各大车企也顺应时代趋势,在新能源领域积极布局。虽然新能源车型拥有免购置税免费上绿牌不限行等优势,但用户在考虑购买新能源车型时,仍然
椰壳活性炭可以做什么用聊一聊活性炭家族的它活性炭是一个大家族,我们都知道纯净水过滤会用活性炭,去味会用活性炭,除湿会用活性炭但是你知道吗?虽然都是活性炭,但是它们的名字并不一样。接下来大家就和国普活性炭一起来看看活性炭这个
2021年客厅怎么设计?选三恒系统,才是高级感的标配设计师说,选对三恒系统是营造高级感的第一步!毕竟客厅空间有限,想要不占用原有面积,还能够最大限度提升客厅的使用率和美观度,这些效果传统空调是达不到的,所以,装上家用三恒系统,高级感
目前评价最好的几款机型,哪一款是你心中最爱?1vivoNEX3SvivoNEX3S是vivo性能比较强悍的手机之一,配备了骁龙865处理器,在性能上顶级旗舰机的水平。在外观上,vivoNEX3S使用前置升降摄像头,6。89英
这边鹿晗关晓彤忙着秀恩爱,那边一鹿彤行商标就被抢注了关晓彤和鹿晗自从宣布恋爱后,就经常被传出分手。最近这段时间,他们分手的传闻尤其多。4月20号是鹿晗的生日,但是到了这一天,关晓彤却迟迟没有发生日祝福,于是很多网友和营销号都猜测鹿晗
中国沙县级西餐厅,人均35元消费,成功撬动2046亿市场在咱们国家的餐饮市场中,虽然说中餐仍占据强势主导地位,但随着我国人民生活水平的提高以及消费观念的变化,大家对多元餐饮文化的需求也越来越高,日本菜韩国菜等特色菜馆也如雨后春笋般冒出。
你看不见却始终在你身边在行业内一直流传三分产品七分安装的说法,安装是保证产品品质的重要环节,而安装材料是影响安装质量和后期使用效果的重要原因之一。装修时,业主最关心的一般都是家具电器等大件材料,要知道,
11年后,梁朝伟承认真做了,汤唯回应他本色出演没07年,色戒从上映以来一直受到颇多的争议,最让人浮想联翩的就是梁朝伟和汤唯床戏的部分。虽然我们在电影院看到的都是经过广电总局删减过后的影片,但那些床戏的部分还是让人血脉喷张,一些看
小米申请的小爱同学商标被驳回,二审判决来啦相信大家都了解,小爱同学是小米公司旗下的人工智能,喊小爱同学即可唤醒它,然后可以进行语音智能沟通,提供播放音乐讲故事翻译叫你起床等个性化服务。小米公司于2017年7月首次推出小爱同
小爱同学商标被抢,小米没辙了?小米在2017年正式推出了小爱同学语音助手,随后推出的智能手机智能音箱小米电视等众多小米生态链设备中也搭载了小爱同学,并且一直沿用至今。但值得注意的是,小米其实一直未能成功获得小爱