惠州Java反射方法是什么?_北大青鸟IT学校
作者:邓华发布时间:2021-04-21分类:Java技术浏览:952
Java反射方法是什么?这是大家在学习Java的过程都会遇到的问题,那么下面我们一起来看看惠州北大青鸟学术部老师是怎么讲解这道题的。
以下四个方法在Class类中返回有关类的方法的信息:
Method[] getMethods() Method[] getDeclaredMethods() Method getMethod(String name, Class... parameterTypes) Method getDeclaredMethod(String name, Class... parameterTypes)
getMethods()方法返回该类的所有可访问的公共方法无论从类中还是继承自超类。
getDeclaredMethods()方法返回该类所有只在声明中的方法(不包括从超类继承的方法)。
getMethod(String name,Class ... parameterTypes)和getDeclaredMethod(String name,Class ... parameterTypes)通过方法名和参数类型获取Method对象。
Method类中的getReturnType()方法返回包含有关返回类型信息的Class对象。
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
class MyClass<T> {
public MyClass(int i, int j, String s) {
}
public MyClass(T t) {
}
public int getInt(String a) {
return 0;
}
}
public class Main {
public static void main(String[] args) {
Class<MyClass> c = MyClass.class;
ArrayList<String> methodsDesciption = getDeclaredMethodsList(c);
System.out.println("Declared Methods for " + c.getName());
for (String desc : methodsDesciption) {
System.out.println(desc);
}
methodsDesciption = getMethodsList(c);
System.out.println("\nMethods for " + c.getName());
for (String desc : methodsDesciption) {
System.out.println(desc);
}
}
public static ArrayList<String> getMethodsList(Class c) {
Method[] methods = c.getMethods();
ArrayList<String> methodsList = getMethodsDesciption(methods);
return methodsList;
}
public static ArrayList<String> getDeclaredMethodsList(Class c) {
Method[] methods = c.getDeclaredMethods();
ArrayList<String> methodsList = getMethodsDesciption(methods);
return methodsList;
}
public static ArrayList<String> getMethodsDesciption(Method[] methods) {
ArrayList<String> methodList = new ArrayList<>();
for (Method m : methods) {
String modifiers = getModifiers(m);
Class returnType = m.getReturnType();
String returnTypeName = returnType.getSimpleName();
String methodName = m.getName();
String params = getParameters(m).toString();
String throwsClause = getExceptionList(m).toString();
methodList.add(modifiers + " " + returnTypeName + " " + methodName
+ "(" + params + ") " + throwsClause);
}
return methodList;
}
public static ArrayList<String> getParameters(Executable exec) {
Parameter[] parms = exec.getParameters();
ArrayList<String> parmList = new ArrayList<>();
for (int i = 0; i < parms.length; i++) {
int mod = parms[i].getModifiers() & Modifier.parameterModifiers();
String modifiers = Modifier.toString(mod);
String parmType = parms[i].getType().getSimpleName();
String parmName = parms[i].getName();
String temp = modifiers + " " + parmType + " " + parmName;
if (temp.trim().length() == 0) {
continue;
}
parmList.add(temp.trim());
}
return parmList;
}
public static ArrayList<String> getExceptionList(Executable exec) {
ArrayList<String> exceptionList = new ArrayList<>();
for (Class<?> c : exec.getExceptionTypes()) {
exceptionList.add(c.getSimpleName());
}
return exceptionList;
}
public static String getModifiers(Executable exec) {
int mod = exec.getModifiers();
if (exec instanceof Method) {
mod = mod & Modifier.methodModifiers();
} else if (exec instanceof Constructor) {
mod = mod & Modifier.constructorModifiers();
}
return Modifier.toString(mod);
}
}
上面的代码生成以下结果。
想了解更多关于Java的资讯,可以来惠州北大青鸟新方舟校区了解一下。
标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校
- Java技术排行
- 标签列表
-
- Java (3694)
- 北大青鸟 (3713)
- 软件开发 (3613)
- JAVA (3413)
- UI设计入门 (2093)
- 惠州北大青鸟 (4375)
- 惠州IT培训 (2558)
- UI设计培训 (2090)
- 惠州UI设计培训 (2095)
- 惠州UI设计培训学校 (2090)
- 惠州计算机软件培训 (6260)
- 惠州计算件软件开发 (6260)
- 惠州计算机软件基础 (6261)
- 惠州计算机JAVA培训 (3574)
- 惠州计算机Java软件开发 (3620)
- 惠州计算机JAVA软件开发 (4645)
- 惠州计算机JAVA软件开发学校 (3338)
- 惠州计算机Java软件开发培训 (3338)
- 北大青鸟IT计算机学校 (5048)
- 北大青鸟IT软件学校 (5062)
- 北大青鸟IT学校 (5059)
- 惠州计算机UI设计软件开发 (2088)
- UI设计基础教程 (2088)
- UI设计是什么 (2088)
- UI设计教程 (2088)
- 网站分类
-
- 计算机教程
- 计算机入门
- 职业学校
- 新闻动态
- 专业课程
- 热门技术
- SEO
- 培训教程
- windows
- linux教程
- 系统集成
- 网站开发
- Html5
- 办公软件
- 师资力量
- 热点问答
- 联系我们
- 计算机学校
- 惠州计算机学校
- 河源计算机学校
- 广州计算机学校
- 深圳计算机学校
- 湛江计算机学校
- 佛山计算机学校
- IT计算机培训信息
- 设计专业
- UI
- 影视特效
- 游戏动漫设计
- Photoshop
- AI设计
- 软件教程
- Java技术
- C语言/C++语言培训
- C#
- Python技术
- PHP
- 数据库
- SQL Server
- 网络教程
- 网络安全
- 网络营销
- 软件专业
- 大数据专业
- 前端开发专业
- 软件测试专业
- Python专业
- 软件实施
- 珠海计算机学校
- 初中生学什么好
- 计算机认证
- 文章归档
-
- 2025年2月 (26)
- 2024年12月 (15)
- 2024年11月 (45)
- 2024年10月 (32)
- 2024年9月 (29)
- 2024年8月 (68)
- 2024年7月 (59)
- 2024年6月 (43)
- 2024年5月 (48)
- 2024年4月 (80)
- 2024年3月 (65)
- 2024年2月 (54)
- 2024年1月 (25)
- 2023年12月 (12)
- 2023年11月 (73)
- 2023年10月 (134)
- 2023年9月 (34)
- 2023年8月 (3)
- 2023年7月 (3)
- 2023年6月 (12)
- 2023年5月 (30)
- 2023年4月 (72)
- 2023年3月 (11)
- 2023年2月 (34)
- 2023年1月 (37)
- 2022年12月 (78)
- 2022年11月 (359)
- 2022年6月 (1193)
- 2022年5月 (570)
- 2022年4月 (1567)
- 2022年3月 (982)
- 2022年2月 (54)
- 2022年1月 (182)
- 2021年9月 (308)
- 2021年8月 (1704)
- 2021年7月 (2423)
- 2021年6月 (1806)
- 2021年5月 (1569)
- 2021年4月 (1380)
- 2021年3月 (1255)
- 2021年2月 (709)
- 2021年1月 (1521)
- 2020年12月 (3626)
- 2020年11月 (1646)
- 2020年10月 (1046)
- 2020年9月 (592)
- 最近发表
-
- 清远信息:清远市社保局开展南粤春暖社保护航志愿服务活动|||北大青鸟计算机培训中心
- 惠州信息:惠州新春招聘会打响2025就业服务开门红|||广州计算机编程培训
- 湛江信息:全市人力资源和社会保障干部作风提升年行动部署会议召开|||北大青鸟计算机培训中心
- 佛山信息:佛山市农业农村局佛山市人力资源和社会保障局2024年度乡村工匠专业人才职称评审工作|||广州市北大青鸟计算机职业培训学校
- 中山信息:中山市社会保险基金管理局失业保险待遇退回决定催告书送达公告|||广州计算机编程培训
- 清远信息:南粤春暖共赴就业新征程清远市举办春季大型招聘活动|||计算机培训学校招生
- 江门信息:为什么要参加企业养老保险DeepSeek告诉你|||大学生计算机培训学校
- 东莞信息:香港劳工及福利局等代表团莅临东莞共促区域人才交流合作新发展|||大学生计算机培训学校
- 东莞信息:莞城街道2025年春风行动暨零工市场招聘活动|||计算机网络培训学校
- 汕头信息:招聘会开进高铁站汕头市南粤春暖专场招聘助力开门红|||计算机网络培训学校