我看JAVA 之 Class
我看JAVA 之 Class
注:基于jdk11
Class
类 Class 的实例代表运行时java应用程序中的类和接口。枚举类型是一种类,注解类是一种接口。数组是共享同一种类的。基 本类型(boolean、byte、char、short、int、long、float和double)和关键字void也表示为类对象。 Class 没有公开的构造方法,只能由JVM创建。 当类加载器调用defineClass方法并传递类文件字节给jvm,jvm会自动构建 Class 对象。
实现了如下接口
1. java.io.Serializable 2. GenericDeclaration GenericDeclaration 所有声明泛型变量的实体要实现此接口(注:后续会在“我看JAVA 之 泛型“阐述) 唯一的方法: public TypeVariable<?>[] getTypeParameters();//获取当前“实体”上声明的“泛型变量"。 3. Type Type是java语言中所有类型的公共高级接口,它们包括原始类型、参数化类型、数组类型、泛型变量和基本类型。 默认方法: default String getTypeName() { return toString(); } 4. AnnotatedElement java.lang.reflect.AnnotatedElement 接口定义了获取在当前JVM中运行程序的注解元素。这个接口允许通过反射的方式获取注解。通过当前接口方法获取到的注解都是不可变且序列化的。
几个重点方法
- forName()
public static Class<?> forName(String className) throws ClassNotFoundException { Class<?> caller = Reflection.getCallerClass(); return forName0(className, true, ClassLoader.getClassLoader(caller), caller); } @CallerSensitive public static Class<?> forName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException { Class<?> caller = null; SecurityManager sm = System.getSecurityManager(); if (sm != null) { // Reflective call to get caller class is only needed if a security manager // is present. Avoid the overhead of making this call otherwise. caller = Reflection.getCallerClass(); if (loader == null) { ClassLoader ccl = ClassLoader.getClassLoader(caller); if (ccl != null) { sm.checkPermission( SecurityConstants.GET_CLASSLOADER_PERMISSION); } } } return forName0(name, initialize, loader, caller); } /** Called after security check for system loader access checks have been made. */ private static native Class<?> forName0(String name, boolean initialize, ClassLoader loader, Class<?> caller) throws ClassNotFoundException;
几个获取 Class 对象引用方式
1. Integer.class 2. new Integer(1).getClass() 3. Class.forName("java.lang.Integer")
代码示例
package chapter03; import java.lang.annotation.ElementType; import java.lang.reflect.InvocationTargetException; public class TestClass { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { System.out.println("--- 3 mode get obj's Class ---"); System.out.println(Integer.class); System.out.println(Integer.valueOf(1).getClass()); System.out.println(Class.forName("java.lang.Integer")); System.out.println(int.class); System.out.println("int is privitive type or not ? " + int.class.isPrimitive()); // array System.out.println("--- array' Class ---"); System.out.println(new Integer[]{1, 3, 5}.getClass()); System.out.println(new Object[]{1, 3, 5}.getClass()); System.out.println(new Integer[][]{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}.getClass()); // void package System.out.println("--- void & package' Class ---"); System.out.println(Void.class); System.out.println(void.class); System.out.println(Package.class); // enum System.out.println("--- enum' Class ---"); System.out.println(ElementType.class); System.out.println("ElementType is enum or not ? " + ElementType.class.isEnum()); // annotation System.out.println("--- annotation' Class ---"); System.out.println(Deprecated.class); System.out.println("Deprecated is interface or not ? " + Deprecated.class.isInterface()); System.out.println("Deprecated is annotation or not ? " + Deprecated.class.isAnnotation()); // class' Class System.out.println("--- class' Class ---"); System.out.println(Class.forName("java.lang.Integer").getClass()); // module System.out.println(Class.forName("chapter03.Test").getModule()); //new instance System.out.println(Class.forName("chapter03.Test").getDeclaredConstructor()); Class.forName("chapter03.Test").getDeclaredConstructor().newInstance(); Class.forName("chapter03.Test").newInstance(); } } class Test { private Test() { } } 打印结果: --- 3 mode get obj's Class --- class java.lang.Integer class java.lang.Integer class java.lang.Integer int int is privitive type or not ? true --- array' Class --- class [Ljava.lang.Integer; class [Ljava.lang.Object; class [[Ljava.lang.Integer; --- void & package' Class --- class java.lang.Void void class java.lang.Package --- enum' Class --- class java.lang.annotation.ElementType ElementType is enum or not ? true --- annotation' Class --- interface java.lang.Deprecated Deprecated is interface or not ? true Deprecated is annotation or not ? true --- class' Class --- class java.lang.Class unnamed module @67b64c45 private chapter03.Test() Exception in thread "main" java.lang.IllegalAccessException: class chapter03.TestClass cannot access a member of class chapter03.Test with modifiers "private" at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361) at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) at chapter03.TestClass.main(TestClass.java:50)

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
VMware 完成 27 亿美元的 Pivotal 收购 | 云原生生态周报 Vol. 34
作者 | 汪萌海、王思宇、李鹏 业界要闻 VMware 完成 27 亿美元的Pivotal 收购 VMware 在 12 月 30 日宣布,已完成 27 亿美元的 Pivotal 收购,同一天 Pivotal 也已被纽约股市除名,成为 VMware 的子公司。 谷歌发布 BeyondCorp 的白皮书 BeyondCorp 是一个“零信任”安全框架,它将访问控制从外围转移到单个设备和用户,允许员工在任何位置安全地工作,而不需要传统的虚拟专用网络。使用 BeyondProd,谷歌实现了与连接机器、工作负载和服务类似的零信任原则。 PingCAP 正式开源混沌测试平台 Chaos Mesh 目前支持的错误注入主要有:pod-kill、pod-failure(模拟节点宕机不可用)、网络延迟、网络丢包、网络包重复、网络包损坏,网络分区、文件系统 I/
- 下一篇
ASP.NET Core Web程序托管到Windows 服务
前言在 .NET Core 3.1和WorkerServices构建Windows服务 我们也看到了,如何将workerservices构建成服务,那么本篇文章我们再来看看如何将web应用程序托管到我们的服务中.将WEB应用作为服务运行我们需要将我们的WEB应用程序编译成exe文件,在ASP.NETCore中其实这是一个很简单的过程,我们只需要修改.csproj即可。正如下面代码片段 <TargetFramework>netcoreapp3.1</TargetFramework> <OutputType>Exe</OutputType> 我们只需要添加OutputType即可.接着来我们需要安装Microsoft.Extensions.Hosting.WindowsServices到我们的WEB应用程序中,如下所示:Install-Package Microsoft.Extensions.Hosting.WindowsServices现在我们需要在Program.cs中的CreateDefaultBuilder方法中进行扩展UseWind...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Red5直播服务器,属于Java语言的直播服务器
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- 2048小游戏-低调大师作品
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8编译安装MySQL8.0.19
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS关闭SELinux安全模块