当前位置:网站首页>Person.class.getInterfaces() 注意使用方法
Person.class.getInterfaces() 注意使用方法
2022-08-04 09:35:00 【魔道不误砍柴功】
Person.class.getInterfaces() 注意使用方法
如果此对象表示一个接口,则该数组包含表示该接口扩展的所有接口的对象。数组中接口对象顺序与此对象所表示的接口的声明的
extends子句中接口名顺序一致。如果此对象表示一个不实现任何接口的类或接口,则此方法返回一个长度为 0 的数组。
如果此对象表示一个基本类型或
void,则此方法返回一个长度为 0 的数组。
代码演示
package main.tcp.dubbo.test;
interface Person{
}
class Son implements Person{
}
public class TestDemo {
public static void main(String[] args) {
// 如果此对象表示一个不实现任何接口的类或接口,则此方法返回一个长度为 0 的数组。
System.out.println(Person.class.getInterfaces().length); // 输出结果是:0
System.out.println(Son.class.getInterfaces()[0]); // 输出结果是:interface main.tcp.dubbo.test.Person
ReflectInterface target = new ReflectInterfaceImpl();
System.out.println(target.getClass().getInterfaces()[0]);// 输出结果是:interface main.tcp.dubbo.test.ReflectInterface
System.out.println(new Class[]{
ReflectInterface.class}[0]);// 输出结果是:interface main.tcp.dubbo.test.ReflectInterface
System.out.println(ReflectInterface.class.getInterfaces().length); // 输出结果是:0
System.out.println(target.getClass());// 输出结果是:class main.tcp.dubbo.test.ReflectInterfaceImpl
System.out.println(ReflectInterface.class); // 输出结果是: interface main.tcp.dubbo.test.ReflectInterface
// 反射中第二个参数需要传入被代理类的接口数组,那么就有这几种写法了,如下:
// ReflectInterfaceImpl.class.getInterfaces()
// new Class<?>[]{ReflectInterface.class} // 推荐使用这种,灵活易用
// target.getClass().getInterfaces()
ReflectInterface service = (ReflectInterface)Proxy
.newProxyInstance(ReflectInterface.class.getClassLoader()
,new Class[]{
ReflectInterface.class}
,(obj, method, params) -> {
System.out.println("param="+ Arrays.asList(params));
String response = (String)method.invoke(target, params);
return response;
});
System.out.println(service.hello("我是入参"));
}
}
class ReflectInterfaceImpl implements ReflectInterface{
@Override
public String hello(String param) {
System.out.println("=================param="+param);
return "放屁";
}
}
运行结果:
0
interface main.tcp.dubbo.test.Person
interface main.tcp.dubbo.test.ReflectInterface
interface main.tcp.dubbo.test.ReflectInterface
0
class main.tcp.dubbo.test.ReflectInterfaceImpl
interface main.tcp.dubbo.test.ReflectInterface
param=[我是入参]
=================param=我是入参
放屁
Process finished with exit code 0
总结一句话,该类是否有父类接口,有的话就会有返回,否则数组长度为0,相当于没有元素返回。
边栏推荐
- 【C补充】指向指针或函数的指针
- 菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
- Ansible deployment scripts - pro available without pit
- IDEA 自动导入的配置(Auto import)
- How to restore the Youxuan database with only data files
- 94后字节P7晒出工资单:狠补了这个,真不错...
- TCP的四次挥手
- [Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1
- 各位大佬,请问mysql数据的cdc,能指定存量数据同步的zone为utc 吗
- MindSpore:Ascend运行出现问题
猜你喜欢

ps如何换背景颜色,自学ps软件photoshop2022,3种不同的方式笔记记录

蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路

路由/三层交换机DHCP下发地址详解【华为eNSP】

leetcode经典例题——49.字母异位词分组

leetcode二叉树系列(二)

MindSpore:Batchnorm only support nchw input!

cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】

去掉js代码文件所有注释

使用ClickHouse分析COS的清单和访问日志

Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
随机推荐
MindSpore:Batchnorm only support nchw input!
双指针方法
VRRP+MSTP配置详解【华为eNSP实验】
被Win11安全中心误删除的文件怎么恢复?
Anton Paar Anton Paar Density Meter Hydrometer Repair DMA35 Performance Parameters
SQL后计算的利器
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
《福格行为模型》:如何养成好习惯?
基于cRIO-904X搭建Simulink与Labview环境
[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
Shell编程的条件语句
【正点原子STM32连载】第四章 STM32初体验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
继承和static关键字
Win11怎么进行左右键对调?
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
MATLAB/Simulink快捷键
TCP的四次挥手
参数优化。
已解决No module named ‘flask_misaka‘【BUG解决】
Qt:小的任务管理器(task)