Java Annotations and reflections are annotated

Source: Internet
Author: User

Methods for class classes

F

Package Com.part1;/** * * @author PC **/ Public classStudent { Public intSID;  Public Charsex; PrivateString sname; PrivateString password; PrivateString Cardno; Private DoubleMoney ;  PublicStudent () {} PublicStudent (intsid, String sname, string password, string Cardno,DoubleMoney ) {         This. Sid =SID;  This. sname =sname;  This. Password =password;  This. Cardno =Cardno;  This. Money =Money ; }     Public intGetSID () {returnSID; }     Public voidSetsid (intSID) {         This. Sid =SID; }     PublicString Getsname () {returnsname; }     Public voidsetsname (String sname) { This. sname =sname; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicString Getcardno () {returnCardno; }     Public voidSetcardno (String cardno) { This. Cardno =Cardno; }     Public DoubleGetmoney () {returnMoney ; }     Public voidSetmoney (DoubleMoney ) {         This. Money =Money ; }         Public CharGetsex () {returnsex; }     Public voidSetsex (Charsex) {         This. Sex =sex; } @Override PublicString toString () {return "Student [cardno="+ Cardno +", money="+ Money +", password="+ Password +", sex="+ Sex +", sid="+ SID +", Sname="+ sname +"]"; }            }
Student entity class
Package com.part1;import Java.lang.reflect.field;import Java.lang.reflect.Method; Public classTest { Public Static voidMain (string[] args) throws ClassNotFoundException, SecurityException, Nosuchfieldexception, nosuchmethodexception {//1. Get the Class object//1.1 The first method of obtaining a Class object: Object name. GetClass ()Student stu=NewStudent (1,"Li Zhendong","123","3201121990",47.5); Class<Student> cla1= (class<student>) Stu.getclass (); System. out. println (CLA1); //1.2 The second way to get a class object: Class Name. class;Class<student> cla2=student.class; System. out. println (CLA2); //1.3 The third way to get a class object:Class<student> cla3= (class<student>) Class.forName ("com.part1.Student"); System. out. println (CLA3); //2. Get Properties//2.1 Get all public properties firstSystem. out. println ("--------2.1 Gets all the public properties first---------"); Field[] Fields1=Cla1.getfields ();  for(Field field:fields1) {System. out. println (field); }        //2.2 Getting a propertySystem. out. println ("---------2.2 Gets a property---------"); Field Field2=cla2.getfield ("Sex"); System. out. println (Field2); //3. Get the method//3.1 Get all the public methodsSystem. out. println ("------3.1 Get all the public methods------"); Method[] Methods=Cla1.getmethods ();  for(Method method:methods) {System. out. println (method); }                //3.2 Getting a public methodSystem. out. println ("---------3.2 Gets a public method---------"); Method method2=cla2.getmethod ("Setsname", String.class); System. out. println (METHOD2); }}
Java gets object properties and methods
Package com.part1;import Java.lang.reflect.field;import Java.lang.reflect.Method; Public classTest { Public Static voidMain (string[] args) throws ClassNotFoundException, SecurityException, Nosuchfieldexception, nosuchmethodexception {//1. Get the Class object//1.1 The first method of obtaining a Class object: Object name. GetClass ()Student stu=NewStudent (1,"Li Zhendong","123","3201121990",47.5); Class<Student> cla1= (class<student>) Stu.getclass (); System. out. println (CLA1); //1.2 The second way to get a class object: Class Name. class;Class<student> cla2=student.class; System. out. println (CLA2); //1.3 The third way to get a class object:Class<student> cla3= (class<student>) Class.forName ("com.part1.Student"); System. out. println (CLA3); //2. Get Properties//2.1 Get all public properties firstSystem. out. println ("--------2.1 Gets all the public properties first---------"); Field[] Fields1=Cla1.getfields ();  for(Field field:fields1) {System. out. println (field); }        //2.2 Getting a propertySystem. out. println ("---------2.2 Gets a property---------"); Field Field2=cla2.getfield ("Sex"); System. out. println (Field2); //3. Get the method//3.1 Get all the public methodsSystem. out. println ("------3.1 Get all the public methods------"); Method[] Methods=Cla1.getmethods ();  for(Method method:methods) {System. out. println (method); }                //3.2 Getting a public methodSystem. out. println ("---------3.2 Gets a public method---------"); Method method2=cla2.getmethod ("Setsname", String.class); System. out. println (METHOD2); }}
Java Dynamic Call Assignment

Java Annotations and reflections are annotated

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.