Constructor (Java Platform SE 8 ) - Oracle Help Center

文章推薦指數: 80 %
投票人數:10人

public final class Constructor extends Executable. Constructor provides information about, ... Fields inherited from interface java.lang.reflect. Skipnavigationlinks Overview Package Class Use Tree Deprecated Index Help Java™ PlatformStandard Ed. 8 Prev Class Next Class Frames No Frames All Classes Summary:  Nested |  Field |  Constr |  Method Detail:  Field |  Constr |  Method java.lang.Object java.lang.reflect.AccessibleObject java.lang.reflect.Executable java.lang.reflect.Constructor TypeParameters: T-theclassinwhichtheconstructorisdeclared AllImplementedInterfaces: AnnotatedElement,GenericDeclaration,Member publicfinalclassConstructor extendsExecutable Constructorprovidesinformationabout,andaccessto,asingle constructorforaclass. Constructorpermitswideningconversionstooccurwhenmatchingthe actualparameterstonewInstance()withtheunderlying constructor'sformalparameters,butthrowsan IllegalArgumentExceptionifanarrowingconversionwouldoccur. SeeAlso: Member, Class, Class.getConstructors(), Class.getConstructor(Class[]), Class.getDeclaredConstructors() FieldSummary Fieldsinheritedfrominterface java.lang.reflect.Member DECLARED,PUBLIC MethodSummary AllMethods InstanceMethods ConcreteMethods  ModifierandType MethodandDescription boolean equals(Object obj) ComparesthisConstructoragainstthespecifiedobject. AnnotatedType getAnnotatedReceiverType() ReturnsanAnnotatedTypeobjectthatrepresentstheuseofa typetospecifythereceivertypeofthemethod/constructorrepresented bythisExecutableobject. AnnotatedType getAnnotatedReturnType() ReturnsanAnnotatedTypeobjectthatrepresentstheuseofatypeto specifythereturntypeofthemethod/constructorrepresentedbythis Executable. T getAnnotation(Class annotationClass) Returnsthiselement'sannotationforthespecifiedtypeif suchanannotationispresent,elsenull. Annotation[] getDeclaredAnnotations() Returnsannotationsthataredirectlypresentonthiselement. Class getDeclaringClass() ReturnstheClassobjectrepresentingtheclassorinterface thatdeclarestheexecutablerepresentedbythisobject. Class>[] getExceptionTypes() ReturnsanarrayofClassobjectsthatrepresentthe typesofexceptionsdeclaredtobethrownbytheunderlying executablerepresentedbythisobject. Type[] getGenericExceptionTypes() ReturnsanarrayofTypeobjectsthatrepresentthe exceptionsdeclaredtobethrownbythisexecutableobject. Type[] getGenericParameterTypes() ReturnsanarrayofTypeobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutablerepresentedby thisobject. int getModifiers() ReturnstheJavalanguagemodifiersfor theexecutablerepresentedbythisobject. String getName() Returnsthenameofthisconstructor,asastring. Annotation[][] getParameterAnnotations() ReturnsanarrayofarraysofAnnotationsthat representtheannotationsontheformalparameters,in declarationorder,oftheExecutablerepresentedby thisobject. int getParameterCount() Returnsthenumberofformalparameters(whetherexplicitly declaredorimplicitlydeclaredorneither)fortheexecutable representedbythisobject. Class>[] getParameterTypes() ReturnsanarrayofClassobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutable representedbythisobject. TypeVariable>[] getTypeParameters() ReturnsanarrayofTypeVariableobjectsthatrepresentthe typevariablesdeclaredbythegenericdeclarationrepresentedbythis GenericDeclarationobject,indeclarationorder. int hashCode() ReturnsahashcodeforthisConstructor. boolean isSynthetic() Returnstrueifthisexecutableisasynthetic construct;returnsfalseotherwise. boolean isVarArgs() Returnstrueifthisexecutablewasdeclaredtotakea variablenumberofarguments;returnsfalseotherwise. T newInstance(Object... initargs) UsestheconstructorrepresentedbythisConstructorobjectto createandinitializeanewinstanceoftheconstructor's declaringclass,withthespecifiedinitializationparameters. String toGenericString() ReturnsastringdescribingthisConstructor, includingtypeparameters. String toString() ReturnsastringdescribingthisConstructor. Methodsinheritedfromclass java.lang.reflect.Executable getAnnotatedExceptionTypes,getAnnotatedParameterTypes,getAnnotationsByType,getParameters Methodsinheritedfromclass java.lang.reflect.AccessibleObject getAnnotations,getDeclaredAnnotation,getDeclaredAnnotationsByType,isAccessible,isAnnotationPresent,setAccessible,setAccessible Methodsinheritedfromclass java.lang.Object clone,finalize,getClass,notify,notifyAll,wait,wait,wait Methodsinheritedfrominterface java.lang.reflect.AnnotatedElement getAnnotations,getDeclaredAnnotation,getDeclaredAnnotationsByType,isAnnotationPresent MethodDetail getDeclaringClass public Class getDeclaringClass() ReturnstheClassobjectrepresentingtheclassorinterface thatdeclarestheexecutablerepresentedbythisobject. Specifiedby: getDeclaringClass ininterface Member Specifiedby: getDeclaringClass inclass Executable Returns: anobjectrepresentingthedeclaringclassofthe underlyingmember getName public String getName() Returnsthenameofthisconstructor,asastring.Thisis thebinarynameoftheconstructor'sdeclaringclass. Specifiedby: getName ininterface Member Specifiedby: getName inclass Executable Returns: thesimplenameoftheunderlyingmember getModifiers public int getModifiers() ReturnstheJavalanguagemodifiersfor theexecutablerepresentedbythisobject. Specifiedby: getModifiers ininterface Member Specifiedby: getModifiers inclass Executable Returns: theJavalanguagemodifiersfortheunderlyingmember SeeAlso: Modifier getTypeParameters public TypeVariable>[] getTypeParameters() ReturnsanarrayofTypeVariableobjectsthatrepresentthe typevariablesdeclaredbythegenericdeclarationrepresentedbythis GenericDeclarationobject,indeclarationorder.Returnsan arrayoflength0iftheunderlyinggenericdeclarationdeclaresnotype variables. Specifiedby: getTypeParameters ininterface GenericDeclaration Specifiedby: getTypeParameters inclass Executable Returns: anarrayofTypeVariableobjectsthatrepresent thetypevariablesdeclaredbythisgenericdeclaration Throws: GenericSignatureFormatError-ifthegeneric signatureofthisgenericdeclarationdoesnotconformto theformatspecifiedin TheJava™VirtualMachineSpecification Since: 1.5 getParameterTypes public Class>[] getParameterTypes() ReturnsanarrayofClassobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutable representedbythisobject.Returnsanarrayoflength 0iftheunderlyingexecutabletakesnoparameters. Specifiedby: getParameterTypes inclass Executable Returns: theparametertypesfortheexecutablethisobject represents getParameterCount public int getParameterCount() Returnsthenumberofformalparameters(whetherexplicitly declaredorimplicitlydeclaredorneither)fortheexecutable representedbythisobject. Overrides: getParameterCount inclass Executable Returns: Thenumberofformalparametersfortheexecutablethis objectrepresents Since: 1.8 getGenericParameterTypes public Type[] getGenericParameterTypes() ReturnsanarrayofTypeobjectsthatrepresenttheformal parametertypes,indeclarationorder,oftheexecutablerepresentedby thisobject.Returnsanarrayoflength0ifthe underlyingexecutabletakesnoparameters. Ifaformalparametertypeisaparameterizedtype, theTypeobjectreturnedforitmustaccuratelyreflect theactualtypeparametersusedinthesourcecode. Ifaformalparametertypeisatypevariableoraparameterized type,itiscreated.Otherwise,itisresolved. Overrides: getGenericParameterTypes inclass Executable Returns: anarrayofTypesthatrepresenttheformal parametertypesoftheunderlyingexecutable,indeclarationorder Throws: GenericSignatureFormatError-ifthegenericmethodsignaturedoesnotconformtotheformat specifiedin TheJava™VirtualMachineSpecification TypeNotPresentException-ifanyoftheparameter typesoftheunderlyingexecutablereferstoanon-existenttype declaration MalformedParameterizedTypeException-ifanyof theunderlyingexecutable'sparametertypesrefertoaparameterized typethatcannotbeinstantiatedforanyreason Since: 1.5 getExceptionTypes public Class>[] getExceptionTypes() ReturnsanarrayofClassobjectsthatrepresentthe typesofexceptionsdeclaredtobethrownbytheunderlying executablerepresentedbythisobject.Returnsanarrayof length0iftheexecutabledeclaresnoexceptionsinitsthrowsclause. Specifiedby: getExceptionTypes inclass Executable Returns: theexceptiontypesdeclaredasbeingthrownbythe executablethisobjectrepresents getGenericExceptionTypes public Type[] getGenericExceptionTypes() ReturnsanarrayofTypeobjectsthatrepresentthe exceptionsdeclaredtobethrownbythisexecutableobject. Returnsanarrayoflength0iftheunderlyingexecutabledeclares noexceptionsinitsthrowsclause. Ifanexceptiontypeisatypevariableoraparameterized type,itiscreated.Otherwise,itisresolved. Overrides: getGenericExceptionTypes inclass Executable Returns: anarrayofTypesthatrepresenttheexceptiontypes thrownbytheunderlyingexecutable Throws: GenericSignatureFormatError-ifthegenericmethodsignaturedoesnotconformtotheformat specifiedin TheJava™VirtualMachineSpecification TypeNotPresentException-iftheunderlyingexecutable's throwsclausereferstoanon-existenttypedeclaration MalformedParameterizedTypeException-if theunderlyingexecutable'sthrowsclausereferstoa parameterizedtypethatcannotbeinstantiatedforanyreason Since: 1.5 equals public boolean equals(Object obj) ComparesthisConstructoragainstthespecifiedobject. Returnstrueiftheobjectsarethesame.TwoConstructorobjectsare thesameiftheyweredeclaredbythesameclassandhavethe sameformalparametertypes. Overrides: equals inclass Object Parameters: obj-thereferenceobjectwithwhichtocompare. Returns: trueifthisobjectisthesameastheobj argument;falseotherwise. SeeAlso: Object.hashCode(), HashMap hashCode public int hashCode() ReturnsahashcodeforthisConstructor.Thehashcodeis thesameasthehashcodefortheunderlyingconstructor's declaringclassname. Overrides: hashCode inclass Object Returns: ahashcodevalueforthisobject. SeeAlso: Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object) toString public String toString() ReturnsastringdescribingthisConstructor.Thestringis formattedastheconstructoraccessmodifiers,ifany, followedbythefully-qualifiednameofthedeclaringclass, followedbyaparenthesized,comma-separatedlistofthe constructor'sformalparametertypes.Forexample: publicjava.util.Hashtable(int,float) Theonlypossiblemodifiersforconstructorsaretheaccess modifierspublic,protectedor private.Onlyoneofthesemayappear,ornoneifthe constructorhasdefault(package)access. Overrides: toString inclass Object Returns: astringdescribingthisConstructor SeeTheJava™LanguageSpecification: 8.8.3.ConstructorModifiers toGenericString public String toGenericString() ReturnsastringdescribingthisConstructor, includingtypeparameters.Thestringisformattedasthe constructoraccessmodifiers,ifany,followedbyan angle-bracketedcommaseparatedlistoftheconstructor'stype parameters,ifany,followedbythefully-qualifiednameofthe declaringclass,followedbyaparenthesized,comma-separated listoftheconstructor'sgenericformalparametertypes. Ifthisconstructorwasdeclaredtotakeavariablenumberof arguments,insteadofdenotingthelastparameteras "Type[]",itisdenotedas "Type...". Aspaceisusedtoseparateaccessmodifiersfromoneanother andfromthetypeparametersorreturntype.Ifthereareno typeparameters,thetypeparameterlistiselided;ifthetype parameterlistispresent,aspaceseparatesthelistfromthe classname.Iftheconstructorisdeclaredtothrow exceptions,theparameterlistisfollowedbyaspace,followed bytheword"throws"followedbya comma-separatedlistofthethrownexceptiontypes. Theonlypossiblemodifiersforconstructorsaretheaccess modifierspublic,protectedor private.Onlyoneofthesemayappear,ornoneifthe constructorhasdefault(package)access. Specifiedby: toGenericString inclass Executable Returns: astringdescribingthisConstructor, includetypeparameters Since: 1.5 SeeTheJava™LanguageSpecification: 8.8.3.ConstructorModifiers newInstance public T newInstance(Object... initargs) throwsInstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException UsestheconstructorrepresentedbythisConstructorobjectto createandinitializeanewinstanceoftheconstructor's declaringclass,withthespecifiedinitializationparameters. Individualparametersareautomaticallyunwrappedtomatch primitiveformalparameters,andbothprimitiveandreference parametersaresubjecttomethodinvocationconversionsasnecessary. Ifthenumberofformalparametersrequiredbytheunderlyingconstructor is0,thesuppliedinitargsarraymaybeoflength0ornull. Iftheconstructor'sdeclaringclassisaninnerclassina non-staticcontext,thefirstargumenttotheconstructorneeds tobetheenclosinginstance;seesection15.9.3of TheJava™LanguageSpecification. Iftherequiredaccessandargumentcheckssucceedandthe instantiationwillproceed,theconstructor'sdeclaringclass isinitializedifithasnotalreadybeeninitialized. Iftheconstructorcompletesnormally,returnsthenewly createdandinitializedinstance. Parameters: initargs-arrayofobjectstobepassedasargumentsto theconstructorcall;valuesofprimitivetypesarewrappedin awrapperobjectoftheappropriatetype(e.g.afloat inaFloat) Returns: anewobjectcreatedbycallingtheconstructor thisobjectrepresents Throws: IllegalAccessException-ifthisConstructorobject isenforcingJavalanguageaccesscontrolandtheunderlying constructorisinaccessible. IllegalArgumentException-ifthenumberofactual andformalparametersdiffer;ifanunwrapping conversionforprimitiveargumentsfails;orif, afterpossibleunwrapping,aparametervalue cannotbeconvertedtothecorrespondingformal parametertypebyamethodinvocationconversion;if thisconstructorpertainstoanenumtype. InstantiationException-iftheclassthatdeclaresthe underlyingconstructorrepresentsanabstractclass. InvocationTargetException-iftheunderlyingconstructor throwsanexception. ExceptionInInitializerError-iftheinitializationprovoked bythismethodfails. isVarArgs public boolean isVarArgs() Returnstrueifthisexecutablewasdeclaredtotakea variablenumberofarguments;returnsfalseotherwise. Overrides: isVarArgs inclass Executable Returns: trueifanonlyifthisexecutablewasdeclared totakeavariablenumberofarguments. Since: 1.5 isSynthetic public boolean isSynthetic() Returnstrueifthisexecutableisasynthetic construct;returnsfalseotherwise. Specifiedby: isSynthetic ininterface Member Overrides: isSynthetic inclass Executable Returns: trueifandonlyifthisexecutableisasynthetic constructasdefinedby TheJava™LanguageSpecification. Since: 1.5 SeeTheJava™LanguageSpecification: 13.1TheFormofaBinary getAnnotation public  T getAnnotation(Class annotationClass) Returnsthiselement'sannotationforthespecifiedtypeif suchanannotationispresent,elsenull. Specifiedby: getAnnotation ininterface AnnotatedElement Overrides: getAnnotation inclass Executable TypeParameters: T-thetypeoftheannotationtoqueryforandreturnifpresent Parameters: annotationClass-theClassobjectcorrespondingtothe annotationtype Returns: thiselement'sannotationforthespecifiedannotationtypeif presentonthiselement,elsenull Throws: NullPointerException-ifthegivenannotationclassisnull Since: 1.5 getDeclaredAnnotations public Annotation[] getDeclaredAnnotations() Returnsannotationsthataredirectlypresentonthiselement. Thismethodignoresinheritedannotations. Iftherearenoannotationsdirectlypresentonthiselement, thereturnvalueisanarrayoflength0. Thecallerofthismethodisfreetomodifythereturnedarray;itwill havenoeffectonthearraysreturnedtoothercallers. Specifiedby: getDeclaredAnnotations ininterface AnnotatedElement Overrides: getDeclaredAnnotations inclass Executable Returns: annotationsdirectlypresentonthiselement Since: 1.5 getParameterAnnotations public Annotation[][] getParameterAnnotations() ReturnsanarrayofarraysofAnnotationsthat representtheannotationsontheformalparameters,in declarationorder,oftheExecutablerepresentedby thisobject.Syntheticandmandatedparameters(see explanationbelow),suchastheouter"this"parametertoan innerclassconstructorwillberepresentedinthereturned array.Iftheexecutablehasnoparameters(meaningnoformal, nosynthetic,andnomandatedparameters),azero-lengtharray willbereturned.IftheExecutablehasoneormore parameters,anestedarrayoflengthzeroisreturnedforeach parameterwithnoannotations.Theannotationobjectscontained inthereturnedarraysareserializable.Thecallerofthis methodisfreetomodifythereturnedarrays;itwillhaveno effectonthearraysreturnedtoothercallers. Acompilermayaddextraparametersthatareimplicitly declaredinsource("mandated"),aswellasparametersthat areneitherimplicitlynorexplicitlydeclaredinsource ("synthetic")totheparameterlistforamethod.SeeParameterformoreinformation. Specifiedby: getParameterAnnotations inclass Executable Returns: anarrayofarraysthatrepresenttheannotationson theformalandimplicitparameters,indeclarationorder,of theexecutablerepresentedbythisobject Since: 1.5 SeeAlso: Parameter, Parameter.getAnnotations() getAnnotatedReturnType public AnnotatedType getAnnotatedReturnType() ReturnsanAnnotatedTypeobjectthatrepresentstheuseofatypeto specifythereturntypeofthemethod/constructorrepresentedbythis Executable. IfthisExecutableobjectrepresentsaconstructor,theAnnotatedTypeobjectrepresentsthetypeoftheconstructedobject. IfthisExecutableobjectrepresentsamethod,theAnnotatedTypeobjectrepresentstheuseofatypetospecifythereturn typeofthemethod. Specifiedby: getAnnotatedReturnType inclass Executable Returns: anobjectrepresentingthereturntypeofthemethod orconstructorrepresentedbythisExecutable Since: 1.8 getAnnotatedReceiverType public AnnotatedType getAnnotatedReceiverType() ReturnsanAnnotatedTypeobjectthatrepresentstheuseofa typetospecifythereceivertypeofthemethod/constructorrepresented bythisExecutableobject.Thereceivertypeofamethod/constructoris availableonlyifthemethod/constructorhasareceiver parameter(JLS8.4.1). IfthisExecutableobjectrepresentsaconstructororinstance methodthatdoesnothaveareceiverparameter,orhasareceiver parameterwithnoannotationsonitstype,thenthereturnvalueisan AnnotatedTypeobjectrepresentinganelementwithno annotations. IfthisExecutableobjectrepresentsastaticmethod,thenthe returnvalueisnull. Overrides: getAnnotatedReceiverType inclass Executable Returns: anobjectrepresentingthereceivertypeofthemethodor constructorrepresentedbythisExecutable Since: 1.8 Skipnavigationlinks Overview Package Class Use Tree Deprecated Index Help Java™ PlatformStandard Ed. 8 Prev Class Next Class Frames No Frames All Classes Summary:  Nested |  Field |  Constr |  Method Detail:  Field |  Constr |  Method SubmitabugorfeatureForfurtherAPIreferenceanddeveloperdocumentation,seeJavaSEDocumentation.Thatdocumentationcontainsmoredetailed,developer-targeteddescriptions,withconceptualoverviews,definitionsofterms,workarounds,andworkingcodeexamples.Copyright©1993,2022,Oracleand/oritsaffiliates.Allrightsreserved.Useissubjecttolicenseterms.Alsoseethedocumentationredistributionpolicy.



請為這篇文章評分?