Java核心逻辑 反射PPT幻灯片

上传人:日度 文档编号:135062323 上传时间:2020-06-11 格式:PPT 页数:48 大小:630KB
返回 下载 相关 举报
Java核心逻辑 反射PPT幻灯片_第1页
第1页 / 共48页
Java核心逻辑 反射PPT幻灯片_第2页
第2页 / 共48页
Java核心逻辑 反射PPT幻灯片_第3页
第3页 / 共48页
Java核心逻辑 反射PPT幻灯片_第4页
第4页 / 共48页
Java核心逻辑 反射PPT幻灯片_第5页
第5页 / 共48页
点击查看更多>>
资源描述

《Java核心逻辑 反射PPT幻灯片》由会员分享,可在线阅读,更多相关《Java核心逻辑 反射PPT幻灯片(48页珍藏版)》请在金锄头文库上搜索。

1、第11章反射 回顾 网络应用的结构IP地址与端口号TCP编程UDP编程 本章目标 class对象获取class对象的方式反射包 java lang reflect 反射的作用 本章结构 反射 获取class对象 反射包java lang reflect 反射的作用 Class forName 方法 类名 class Method类 Field类 class对象概念 getClass 方法 Constructor类 思考 给定一个对象 要求输出这个对象所有的public方法的名称publicstaticvoidprintMethod Objectobject 给定一个字符串参数 这个参数表示一个

2、类的名字 根据类名 创建该类的一个对象并返回publicstaticObjectcreateObject StringclassName class对象 Customer类是客户的抽象 Customer对象 代表 某个客户 但不是某个客户 抽象一个Customer的过程 publicclassCustomer privateStringname privateintage privateStringaddress 使用字符串表示的地址无法正确的表示地址publicCustomer Stringname intage Stringaddress this name name this age a

3、ge this address address 省略set get方法 class对象 将address单独封装成一个类Address publicclassAddress privateStringprovince privateStringcity privateStringstreet privateintcode publicAddress Stringprovince Stringcity Stringstreet intcode this province province this city city this street street this code code 省略set

4、 get方法 class对象 将Customer的address属性的类型换成Address类型 publicclassCustomer privateStringname privateintage privateAddressaddress 将String类型转换成Address类型publicCustomer Stringname intage Addressaddress this name name this age age this address address 省略set get方法 class对象 如果想抽象一个Class的信息 那么我们如下来抽象 类中有许多构造函数 有许多

5、方法 有许多成员变量 publicclassClazz 构造方法privateString construtctors 字符串无法充分表示的表示构造方法的信息 方法privateString methods 字符串无法充分的表示方法 成员变量privateString fields 字符串无法充分的表示成员变量的信息publicClazz String construtctors String methods String fields super this construtctors construtctors this methods methods this fields fields

6、省略set get方法 class对象 1 字符串无法充分的表达各种成员 那么就各成员再次进行抽象 将Clazz中的各成员对象进行抽象classConstructor classMethod classField class对象 2 字符串无法充分的表达各种成员 那么就各成员再次进行抽象 publicclassClazz privateConstructor constructors 构造方法privateMethod methods 方法privateField fields 成员变量publicClazz Constructor constructors Method methods Fi

7、eld fields this constructors constructors this methods methods this fields fields 省略set get方法 class对象 Customer类是客户的抽象 Customer对象 代表 某个客户 但不是某个客户 Clazz类是其他类的抽象 Clazz对象代表其他的类 Java已经为我们提供了这样一个类 java lang Class 我们无需自己定义Clazz类 通过Class对象我们可以得到 类继承自哪个类实现了哪些接口有哪些属性有哪些方法有哪些构造方法 获取class对象的方式 类名 class Classcla

8、zStudent Student class ClassclazInt int class ClassclazIntArray int class 获取class对象的方式 getClass 方法 Studentstudent newStudent ClassclazStudent1 student getClass ClassclazStudent2 Student class 获取class对象的方式 Class forName 方法publicstaticClassforName StringclassName throwsClassNotFoundException Studentst

9、udent newStudent ClassclazStudent1 student getClass ClassclazStudent2 Student class try ClassclazStudent3 Class forName Student catch ClassNotFoundExceptione e printStackTrace class对象的作用 getName 获得类的名称 包括包名getSimpleName 获得类的名称 不包括包名getSuperClass 获得本类的父类的class对象getInterfaces 获得本类所实现的所有接口的class对象 clas

10、s对象的作用 Classc ArrayList class StringclassName c getName System out println 类名 className StringsimpleName c getSimpleName System out println 简单类名 simpleName ClasssuperClass c getSuperclass System out println 父类 superClass getName Class interfaces c getInterfaces System out println 接口 for inti 0 i int

11、erfaces length i System out println interfaces i getName class对象的作用 publicMethod getDeclaredMethods throwsSecurityException取得所有当前类声明的方法 包括public protected 默认 private四种访问权限的方法 但是不包括继承的方法publicMethod getMethods throwsSecurityException取得所有public的方法 包括继承的 接口中声明的和自己定义的 class对象的作用 publicclassStudent publi

12、cStringname privateintage staticfinalStringKIND human protectedStringschoolName publicStudent publicStudent Stringname intage this name name this age age publicStringgetName returnname publicvoidsetName Stringname this name name publicintgetAge returnage publicvoidsetAge intage this age age publicvo

13、idpublicStudy inth System out println 调用publicStudy学习时间是 h protectedvoidprotectedStudy inth System out println 调用protectedStudy学习时间是 h voidfriendlyStudy inth System out println 调用friendlyStudy学习时间是 h privatevoidprivateStudy inth System out println 调用privateStudy学习时间是 h class对象的作用 获得所有的公开方法 Classclaz

14、Student Student class Method publicMethods clazStudent getMethods System out println 所有public方法 for Methodmethod publicMethods System out println method getName class对象的作用 获得所有的本类中定义的方法 Classc Student class Method declaredMethods c getDeclaredMethods System out println 所有当前的类自己定义的方法 for Methodm decl

15、aredMethods System out println m getName class对象的作用 Field getDeclaredFields 取得所有当前类自己定义的属性 包括四种访问权限的Field getFields 取得所有public的属性 包括继承的 接口中声明的和自己定义的 class对象的作用 获得所有的公开属性 Classc Student class Field publicMethods c getFields System out println 所有public属性 for Fieldfield publicMethods System out println

16、 field getName publicclassStudent publicStringname privateintage 其他代码 class对象的作用 获得本类中定义的属性 ClassclazStudent Student class Field declaredFields clazStudent getDeclaredFields System out println 获取当前类自己定义的属性 for Fieldfield declaredFields System out println field getName publicclassStudent publicStringname privateintage staticfinalStringKIND human protectedStringschoolName 省略其他代码 使用class对象创建类的对象 ObjectnewInstance ClassclazStudent Student class Studentstudent try student clazStudent newInstance stud

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 计算机应用/办公自动化

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号