简体   繁体   English

java类类型 - 它们是什么意思?

[英]java class types - what do they mean?

What is the java class type used for? 什么是java class类型? I am confused about what it means and how it is different than declaring an object type: 我很困惑它的含义以及它与声明object类型的不同之处:

Class className;

Thanks 谢谢

There are several uses for a Class object. Class对象有多种用途。 For example, say I want to create an instance of a class based on some class name stored in a config file. 例如,假设我想基于存储在配置文件中的某个类名创建类的实例。

String className = config.myClass;
Class clazz = Class.forName(className);
Object myClassInstance = clazz.newInstance();

It represents the runtime type of the object. 它表示对象的运行时类型 The actual programmatic use of the Class type is often found in reflection and generics . Class类型的实际编程使用通常在反射泛型中找到。

For example, loading a JDBC driver abstractly with help of Class#forName() : 例如,在Class#forName()帮助下抽象地加载JDBC驱动程序:

String jdbcDriverClassName = getItFromSomeExternalConfigurationFile();
Class.forName(jdbcDriverClassName);

Or typecasting an generic Object to a beforeknown type: 或者将通用Object类型转换为以前的类型:

public static <T> T findAttribute(String key, Class<T> type) {
    return type.cast(attributeMap.get(key)); // It's a Map<String, Object>.
}

...which can be used as ......可以用作

SomeType instance = findAttribute("someKey", SomeType.class);

A more extended example can be found here in flavor of a "generic object converter". 这里可以在“通用对象转换器”的风格中找到更加扩展的示例。

Actually, reading the java.lang.Class javadoc, including all of the available methods, should give you an idea what it can be used for. 实际上,阅读java.lang.Class javadoc,包括所有可用的方法,应该可以让你知道它可以用于什么。

Class is a special type of Object , ie Class is a sub class of Object . Class是一种特殊类型的Object ,即ClassObject的子类。 Every class you define has its own Class object. 您定义的每个类都有自己的Class对象。 You can access this as MyObject.class or myInstance.getClass() . 您可以将其作为MyObject.classmyInstance.getClass()来访问。 In another word, any class you define has a Class attribute where as any class is an Object . 换句话说,您定义的任何类都具有Class属性,其中任何类都是Object I agree it is slightly confusing to a newbie. 我同意这对新手来说有点混乱。

javadoc says: javadoc说:

Instances of the class Class represent classes and interfaces in a running Java application. 类Class的实例表示正在运行的Java应用程序中的类和接口。 An enum is a kind of class and an annotation is a kind of interface. 枚举是一种类,注释是一种接口。 Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. 每个数组也属于一个类,它反映为一个Class对象,由具有相同元素类型和维数的所有数组共享。 The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. 原始Java类型(boolean,byte,char,short,int,long,float和double)和关键字void也表示为Class对象。

Class has no public constructor. 类没有公共构造函数。 Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader. 相反,类对象由Java虚拟机在加载类时自动构造,并通过调用类加载器中的defineClass方法来构造。

You can use it when checking the type of some variable or check for inheritance runtime. 您可以在检查某个变量的类型或检查继承运行时时使用它。 It's also used in reflection, to load dynamically types and execute methods on them. 它也用于反射,动态加载类型并在它们上执行方法。

From the book Thinking in Java : Thinking in Java

The Class object Class对象

To understand how Run Time Type Information (RTTI) works in Java, you must first know how type information is represented at run time. 要了解运行时类型信息(RTTI)如何在Java中工作,您必须首先了解在运行时如何表示类型信息。 This is accomplished through a special kind of object called the Class object, which contains information about the class. 这是通过一种称为Class对象的特殊对象来实现的,该对象包含有关该类的信息。 In fact, the Class object is used to create all of the 'regular' objects of your class. 实际上,Class对象用于创建类的所有“常规”对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM