大家好,感谢邀请,今天来为大家分享一下constructor是什么意思、读音的问题,以及和Constructor类及相关类解读的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!
Constructor提供了对一个类单个构造器的信息和访问。
publicfinalclassConstructor<T>extendsExecutable{}
关联的Executable类在另一篇关于Method博客中有介绍,为便于查看,这里再次显示,定义(这里仅列出独有方法)如下:
publicabstractclassExecutableextendsAccessibleObject
implementsMember,GenericDeclaration{
/***其他方法省略,在其他博客中已介绍,使用类似*****/
publicParameter[]getParameters(){}
//返回方法的参数声明AnnotatedType数组
publicAnnotatedType[]getAnnotatedParameterTypes(){}
publicAnnotatedType[]getAnnotatedExceptionTypes(){}
importsun.reflect.CallerSensitive;
importsun.reflect.ConstructorAccessor;
importsun.reflect.annotation.TypeAnnotation;
importsun.reflect.annotation.TypeAnnotationParser;
importsun.reflect.generics.repository.ConstructorRepository;
importsun.reflect.generics.factory.CoreReflectionFactory;
importsun.reflect.generics.factory.GenericsFactory;
importsun.reflect.generics.scope.ConstructorScope;
importjava.lang.annotation.Annotation;
importjava.lang.annotation.AnnotationFormatError;
//Constructor提供了对一个类单个构造器的信息和访问
publicfinalclassConstructor<T>extendsExecutable{
publicClass<T>getDeclaringClass(){}
publicTypeVariable<Constructor<T>>[]getTypeParameters(){}
publicClass<?>[]getParameterTypes(){}
publicintgetParameterCount(){returnparameterTypes.length;}
publicType[]getGenericParameterTypes(){}
publicClass<?>[]getExceptionTypes(){}
publicType[]getGenericExceptionTypes(){}
//两个构造器是否相等,当两个构造器由形同的类声明,有相同的参数类型,则相等
publicbooleanequals(Objectobj){}
//返回构造器的字符串表示(包含类型变量)
publicStringtoGenericString(){}
publicTnewInstance(Object...initargs)
throwsInstantiationException,IllegalAccessException,
IllegalArgumentException,InvocationTargetException{}
public<TextendsAnnotation>TgetAnnotation(Class<T>annotationClass){}
publicAnnotation[]getDeclaredAnnotations(){}
//返回方法参数上的注解二维数组(多个参数的注解,每个参数可以有多个注解)
publicAnnotation[][]getParameterAnnotations(){
returnsharedGetParameterAnnotations(parameterTypes,parameterAnnotations);
//获取构造器返回的AnnotatedType
publicAnnotatedTypegetAnnotatedReturnType(){}
//获取构造器指定接收者的AnnotatedType
publicAnnotatedTypegetAnnotatedReceiverType(){}
publicstaticvoidmain(String[]args)throwsException{
Constructor<Stu>stuCons=Stu.class.getConstructor();
Constructor<Stu>stuOneCons=Stu.class.getConstructor(int.class,String.class);
Constructor<Stu>stuTwoCons=Stu.class.getDeclaredConstructor(List.class,Object[].class);
Constructor<Stu>stuThreeCons=Stu.class.getDeclaredConstructor(String[].class);
Constructor<Stu>stuFourCons=Stu.class.getDeclaredConstructor(int.class,String.class,String.class);
Class<Stu>clazz=stuCons.getDeclaringClass();
intmodifiers=stuCons.getModifiers();
Stringmodify=Modifier.toString(modifiers);
TypeVariable<Constructor<Stu>>[]typeParameters=stuTwoCons.getTypeParameters();
//返回参数类型数组,这里返回包含int和String的数组
Class<?>[]parameterTypes=stuOneCons.getParameterTypes();
intparameterCount=stuOneCons.getParameterCount();
Type[]genericParameterTypes=stuTwoCons.getGenericParameterTypes();
Class<?>[]exceptionTypes=stuTwoCons.getExceptionTypes();
Type[]genericExceptionTypes=stuTwoCons.getGenericExceptionTypes();
//两个构造器是否相等,当两个构造器由形同的类声明,有相同的参数类型,则相等
booleanequals=stuCons.equals(stuCons);//true
inthashCode=stuCons.hashCode();
StringtoString=stuCons.toString();
//返回构造器的字符串表示(包含类型变量)
StringtoGenericString=stuTwoCons.toGenericString();
booleanisVarArgs=stuThreeCons.isVarArgs();
booleanisSynthetic=stuThreeCons.isSynthetic();
Annotationannotation=stuFourCons.getAnnotation(ConstructorCacheAnno.class);
Annotation[]declaredAnnotations=stuFourCons.getDeclaredAnnotations();
//返回方法参数上的注解二维数组(多个参数的注解,每个参数可以有多个注解)
Annotation[][]parameterAnnotations=stuFourCons.getParameterAnnotations();
//获取构造器返回的AnnotatedType,这里为Stu
AnnotatedTypeannotatedReturnType=stuFourCons.getAnnotatedReturnType();
//获取构造器指定接收者的AnnotatedType,这里为ConstructorStudy
AnnotatedTypeannotatedReceiverType=stuFourCons.getAnnotatedReceiverType();
//获取构造器的参数AnnotatedType数组,这里结果包含int和String,(方法来自于继承Executable抽象类)
AnnotatedType[]annotatedParameterTypes=stuFourCons.getAnnotatedParameterTypes();
//获取构造器的参数AnnotatedType数组,这里结果包含int和String,(方法来自于继承Executable抽象类)
Parameter[]parameters=stuFourCons.getParameters();
//获取构造器返回的异常AnnotatedType,(方法来自于继承Executable抽象类)
AnnotatedType[]annotatedExceptionTypes=stuFourCons.getAnnotatedExceptionTypes();
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
@interfaceConstructorCacheAnno{
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@interfaceParameterCacheOneAnno{
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@interfaceParameterCacheTwoAnno{
StringcacheName()default"ocean";
publicStu(intid,StringstuName){
public<T,E>Stu(List<T>tList,E[]eArr)throwsIOException,NullPointerException{
@ConstructorCacheAnno(cacheTime=31)
publicStu(@ParameterCacheOneAnno(cacheTimeOne=11,cacheTimeTwo=12)@ParameterCacheTwoAnno(cacheName="water")intid,
@ParameterCacheTwoAnno(cacheName="water")StringstuName,
好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!