Java Learning--reflection constructor operations

Source: Internet
Author: User

1 @Test2      Public voidtest1 () throws exception{3String ClassName ="Com.atguigu.java.Person";4Class Clazz =Class.forName (className);5         //creates an object for the corresponding run-time class. Using Newinstance () is actually the constructor that invokes the null parameter of the runtime class. 6         //To be able to create a success: ① requires the corresponding runtime class to have a constructor with null parameters. The permissions of the ② constructor are sufficient. 7Object obj =clazz.newinstance ();8Person p =(person) obj;9System. out. println (p);Ten}
1 @Test2      Public voidtest2 () throws classnotfoundexception{3String ClassName ="Com.atguigu.java.Person";4Class Clazz =Class.forName (className);5         6Constructor[] Cons =clazz.getdeclaredconstructors ();7          for(Constructor c:cons) {8System. out. println (c);9         }Ten}
1 //invokes the specified constructor to create an object of the run-time class2 @Test3      Public voidTest3 () throws exception{4String ClassName ="Com.atguigu.java.Person";5Class Clazz =Class.forName (className);6         7Constructor cons = Clazz.getdeclaredconstructor (String.class,int.class);8Cons.setaccessible (true);9Person P = (person) cons.newinstance ("Luo Wei", -);TenSystem. out. println (p); One}

1  classPerson extends creature<string>implements comparable,myinterface{2      PublicString name;3     Private intAge ;4     intID;5     //When you create a class, try to keep an empty parameter constructor. 6      PublicPerson () {7 super ();8 //System.out.println ("The weather is very sultry today");9     }Ten      PublicPerson (String name) { One super (); A          This. Name =name; -     } -     PrivatePerson (String name,intAge ) { the super (); -          This. Name =name; -          This. Age =Age ; -     } +      PublicString GetName () { -         returnname; +     } A      Public voidsetName (String name) { at          This. Name =name; -     } -      Public intGetage () { -         returnAge ; -     } -      Public voidSetage (intAge ) { in          This. Age =Age ; -     } to      +      Public intgetId () { -         returnID; the     } *      Public voidSetId (intID) { $          This. ID =ID;Panax Notoginseng     } -@MyAnnotation (value ="abc123") the      Public voidShow () { +System. out. println ("I am a person! "); A     } the      +     PrivateInteger Display (String Nation,integer i) throws exception{ -System. out. println ("My nationality is:"+nation); $         returni; $     } - @Override -      PublicString toString () { the         return "Person [Name="+ name +", age="+ Age +"]"; -     }Wuyi @Override the      Public intcompareTo (Object o) { -         //TODO auto-generated Method Stub Wu         return 0; -     } About      $      Public Static voidinfo () { -System. out. println ("The Chinese! "); -     } -      A     classbird{ +          the     } -      $}
1ImportStaticJava.lang.annotation.ElementType.CONSTRUCTOR;2ImportStaticJava.lang.annotation.ElementType.FIELD;3ImportStaticJava.lang.annotation.ElementType.LOCAL_VARIABLE;4ImportStaticJava.lang.annotation.ElementType.METHOD;5ImportStaticJava.lang.annotation.ElementType.PARAMETER;6ImportStaticJava.lang.annotation.ElementType.TYPE;7 8 import java.lang.annotation.Retention;9 import Java.lang.annotation.RetentionPolicy;Ten import Java.lang.annotation.Target; One  A @Target ({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, local_variable}) - @Retention (retentionpolicy.runtime) -  Public@interface myannotation { the String value (); -}
 1  public  class  Creature<t>{ public  double   weight;  3   Public  void   breath () { 5  System. out . println ( "  breathe!   );  6   7  }

1 import java.io.Serializable; 2 3 public interface MyInterface extends serializable{ 4 5 }

Java Learning--reflection constructor operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.