Class对象功能概述和Class对象功能获取Field
Constructor[] getConstructors()
Constructor getConstructor(类... parameterTypes)
Constructor getDeclareConstructor(类... paramterTypes)
Constructor[] getDeclaredConstructors()获取所有的成员变量不考虑修饰符
Constructor:构造方法
创建对象
T newUnstance(Object... initargs)
如果使用空参数构造方法创建对象,操作可以简化:Class对象的newInstance方法
package com.yang.reflect;
import com.yang.exercises.Person;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class ReflectStudy02 {
public static void main(String[] args) throws Exception {
/**
* 2.获取构造方法们
* Constructor[] getConstructors()
* Constructor getConstructor(类... parameterTypes)
*
* Constructor getDeclareConstructor(类... paramterTypes)
* Constructor[] getDeclaredConstructors()获取所有的成员变量不考虑修饰符
*/
Class<Person> personClass = Person.class;
//Constructor getConstructor(类... parameterTypes)
Constructor<Person> constructor = personClass.getConstructor(String.class, int.class);
System.out.println(constructor);
//创建对象
Person p = constructor.newInstance("张三", 18);
System.out.println(p);
Person person = personClass.newInstance();
System.out.println(person);
}
}
Class对象功能获取Field
package com.yang.reflect;
import com.yang.exercises.Person;
import java.lang.reflect.Method;
public class ReflectStudy03 {
public static void main(String[] args) throws Exception {
/**
3.获取成员方法们
Method[] getMethod()
Method getMethod(String name,类<?>... paramterTypes)
Method[] getDeclaredMethods()
Method getDeclaredMethod(String name,类<?>... paramterTypes)
*/
//获取Person的class对象
Class<Person> personClass = Person.class;
//获取指定名称的方法
Method eat = personClass.getMethod("eat");
Person p = new Person();
//执行方法
eat.invoke(p);
Method eat2 = personClass.getMethod("eat",String.class);
//执行方法
eat2.invoke(p,"饭");
//获取所有public修饰的方法
Method[] methods = personClass.getMethods();
for (Method method : methods) {
System.out.println(method);
String name = method.getName();
System.out.println(name);
}
//获取类名
String className = personClass.getName();
System.out.println(className);
}
}
Class对象功能概述和Class对象功能获取Field的更多相关文章
- Android(java)学习笔记102:Map集合功能概述
下面通过代码引入Map集合:如下 package cn.itcast_01; import java.util.HashMap; import java.util.Map; /* * 作为学生来说,是 ...
- Android(java)学习笔记41:Map集合功能概述
1. 下面通过代码引入Map集合: 如下 package cn.itcast_01; import java.util.HashMap; import java.util.Map; /* * 作为学生 ...
- 计算机网络之传输层(传输层提供的服务及功能概述、端口、套接字--Socket、无连接UDP和面向连接TCP服务)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105451022 学习课程:<2019王道考研计算机网络> 学习目的 ...
- 模拟Select-Options对象实现多项数据输入功能
模拟Select-Options对象实现多项数据输入功能 Select-Options对象可以同时输入多项值并将所输入数据存入内表以供程序使用,不过Select-Options的功能有一定的局限 ...
- 屏幕对象的F1/F4输入帮助功能
1.HELP-REQUST[FOR{LOW|HIGH}]字段的F1帮助 当选择SAP屏幕功能的制定字段按F1键时可以调关注用自定义的程序或者系统帮助文件,该功能通常称为F1帮助. TYPES:syst ...
- Struts功能详解——ActionMapping对象
Struts功能详解——ActionMapping对象 ActionMapping描述了struts中用户请求路径和Action的映射关系,在struts中每个ActionMapping都是通过pat ...
- Delphi2007新功能 -- 有限的栈对象
今天使用Delphi2007,一个误输入,无意中发现Delphi2007的record类型居然能够和TObject一样定义方法和属性,而且不需要调用类似TObject.Create方法就能生成一个re ...
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
C#构造方法(函数) 一.概括 1.通常创建一个对象的方法如图: 通过 Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...
- 前端(十七)—— jQuery基础:jQuery的基本使用、JQ功能概括、JS对象与JQ对象转换、Ajax简单应用、轮播图
jQuery的基本使用.JQ功能概括.JS对象与JQ对象转换.Ajax简单应用.轮播图 一.认识jQuery 1.什么是jQuery jQuery是对原生JavaScript二次封装的工具函数集合 j ...
随机推荐
- 中间件漏洞之Apache
中间件之Apache漏洞 我们常见的中间件有IIS.Apache.Nginx,其中Apache中间件有什么漏洞呢? Apache 解析漏洞: 漏洞原因:该解析漏洞属于用户配置问题(mime.types ...
- 超越OpenCV速度的MorphologyEx函数实现(特别是对于二值图,速度是CV的4倍左右)。
最近研究了一下opencv的 MorphologyEx这个函数的替代功能, 他主要的特点是支持任意形状的腐蚀膨胀,对于灰度图,速度基本和CV的一致,但是 CV没有针对二值图做特殊处理,因此,这个函数对 ...
- Thumbnails 图片处理
Thumbnails 是由谷歌提供的图片处理包,目前版本0.4.8. 可以简洁的实现图片的缩放.压缩.旋转.水印.格式转换等操作. 示例代码: package test;import net.coob ...
- 【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
问题描述 当本地环境中安装.NET 6.0后,用指令 dotnet new web 或 dotnet new console 生成的项目,使用的都是新模板生成的Program.cs文件.里面去掉了n ...
- 为什么 Redis 要有哨兵机制?
作者:小林coding 计算机八股文刷题网站:https://xiaolincoding.com 大家好,我是小林. 这次聊聊,Redis 的哨兵机制. 提纲 为什么要有哨兵机制? 在 Redis 的 ...
- 【工具-Nginx】从入门安装到高可用集群搭建
文章已收录至https://lichong.work,转载请注明原文链接. ps:欢迎关注公众号"Fun肆编程"或添加我的私人微信交流经验 一.Nginx安装配置及常用命令 1.环 ...
- 645. Set Mismatch - LeetCode
Question 645. Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数 ...
- 造个海洋球池来学习物理引擎【Three.js系列】
github地址:https://github.com/hua1995116/Fly-Three.js 大家好,我是秋风.继上一篇<Three.js系列: 游戏中的第一/三人称视角>今 ...
- 关于我学git这档子事(3)
对于如下报错: hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpa ...
- 第06组 Alpha冲刺 (2/6)
目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2. 方梓涵 3. 黄少丹 4. 董翔云 5.曾丽莉 6. 詹鑫冰 7.鲍凌函 8.杜筱 9.曹兰英 10. 吴沅静 1.3 冲刺成果展示 1 ...