Java中的Type接口和Class类有什么区别
Type是Class的父接口。
Type 是 Java 编程语言中所有类型的公共高级接口。它们包括原始类型、参数化类型、数组类型、类型变量和基本类型。
这里举个当Type是参数化类型的例子:
public class B<T,K> {
}
package test;
public class Book extends B<String,Integer>{
}
import test.B;
import test.Book;
import java.lang.reflect.Type;
public class Main {
public static void main(String[] args) {
Book book=new Book();
Class cbook=book.getClass();
//获取Book类的直接超类的Type
Type type=cbook.getGenericSuperclass();
Class cb=new B().getClass();
System.out.println(type);
System.out.println(cb);
}
}
运行结果:
test.B<java.lang.String, java.lang.Integer>
class test.B
可以看出,Type可以表示出泛型的类型,而Class不能。
另外,如果想要获取泛型类型的数组,可以将Type转化为它的子接口ParameterizedType,通过getActualTypeArguments() 获取:
Type[] paramList=((ParameterizedType)type).getActualTypeArguments();
System.out.println(paramList[0]+" "+paramList[1]);
运行结果:
class java.lang.String class java.lang.Integer
Java中的Type接口和Class类有什么区别_零怀念的博客-CSDN博客_java class type
https://blog.csdn.net/linghuainian/article/details/86137810
==========================================================================================
==========================================================================================
==========================================================================================
Type定义与说明
源代码:
package java.lang.reflect;
/**
* Type is the common superinterface for all types in the Java
* programming language. These include raw types, parameterized types,
* array types, type variables and primitive types.
*
* @since 1.5
*/
public interface Type {
/**
* Returns a string describing this type, including information
* about any type parameters.
*
* @implSpec The default implementation calls {@code toString}.
*
* @return a string describing this type
* @since 1.8
*/
default String getTypeName() {
return toString();
}
}
总结来说:
Type是一个接口。
Type是Java中所有类型的父接口。
Type包括:raw type(原始类型,对应Class), parameterized types(参数化类型), array types(数组类型), type variables(类型变量) and primitive types(基本类型,对应Class).
Type是JDK1.5引入的,主要是为了泛型。
Class定义与说明
源代码:
/**
* Instances of the class {@code Class} represent classes and
* interfaces in a running Java application. 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 {@code Class} object
* that is shared by all arrays with the same element type and number
* of dimensions. The primitive Java types ({@code boolean},
* {@code byte}, {@code char}, {@code short},
* {@code int}, {@code long}, {@code float}, and
* {@code double}), and the keyword {@code void} are also
* represented as {@code Class} objects.
*
* <p> {@code Class} has no public constructor. Instead {@code Class}
* objects are constructed automatically by the Java Virtual Machine as classes
* are loaded and by calls to the {@code defineClass} method in the class
* loader.
*
* <p> The following example uses a {@code Class} object to print the
* class name of an object:
*
* <blockquote><pre>
* void printClassName(Object obj) {
* System.out.println("The class of " + obj +
* " is " + obj.getClass().getName());
* }
* </pre></blockquote>
*
* <p> It is also possible to get the {@code Class} object for a named
* type (or for void) using a class literal. See Section 15.8.2 of
* <cite>The Java™ Language Specification</cite>.
* For example:
*
* <blockquote>
* {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
* </blockquote>
*
* @param <T> the type of the class modeled by this {@code Class}
* object. For example, the type of {@code String.class} is {@code
* Class<String>}. Use {@code Class<?>} if the class being modeled is
* unknown.
*
* @author unascribed
* @see java.lang.ClassLoader#defineClass(byte[], int, int)
* @since JDK1.0
*/
public final class Class<T> implements java.io.Serializable,
GenericDeclaration,
Type,
AnnotatedElement {
}
总结来说:Class是一个类。
Class对象代表着正在运行的Java程序中的类型和接口:enum、annotation、array以及基本类型(8种)都可以用Class对象表示。
Class没有public构造器。
当类被加载或者在class loader中被方法调用时,JVM会自动构造Class对象。
Type接口与Class类的区别联系
Type是Class的父接口。
Class是Type的子类。
Java中Type接口与Class类的区别联系_韩超的博客 (hanchao5272)-CSDN博客_java type class
https://blog.csdn.net/hanchao5272/article/details/79401434