The difference between the reflection Getdeclaredfield () method and the GetField () method of Dark horse programmer-java Learning

Source: Internet
Author: User

--------Android Training , Java training , look forward to communicating with you! --------

In the use of reflection, commonly used to a few methods are always silly to understand why, the function of the same Ah,
Like what:
Class.getfields (), Class.getfield (String), Class.getdeclaredfields (), Class.getdeclaredfield (String)
And
Class.getmethods (), Class.getmethod (String, class[]), Class.getdeclaredmethods (), Class.getdeclaredmethod (String, Class[])
The main thing is there's no difference between declared words,
After careful study of the declared word is obtained is a class of its own defined members, including public, private, protected, and so on fields or methods,
If there is no declared word, it is common to get all the exposed members owned by a class, including the exposed members of your own definition and inheritance, such as exposed fields and inherited public methods.
As shown in the following code:

Import java.lang.reflect.*; Public classtest2{ Public Static voidMain (string[] args) throws Exception {person P=NewPerson ();//instantiate a parent classfield[] PF1= P.getclass (). GetFields ();//The GetFields () method gets all accessible public fields for the class or interface that is represented. Includes accessible public fields that are inheritedShow (PF1); Field[] PF2= P.getclass (). Getdeclaredfields ();//The getdeclaredfields () method gets all the fields declared by the class or interface represented by the object. Includes public, protected, default (package) access, and private fields, but does not include inherited fields. Show (PF2); Student s=NewStudent ();//instantiate a subclassfield[] PF3= S.getclass (). GetFields ();//The GetFields () method gets all accessible public fields for the class or interface that is represented. Includes accessible public fields that are inheritedShow (PF3); Field[] Pf4= S.getclass (). Getdeclaredfields ();//The getdeclaredfields () method gets all the fields declared by the class or interface represented by the object. Includes public, protected, default (package) access, and private fields, but does not include inherited fields. Show (PF4); }    //a convenient way to print     Public Static voidShow (field[] fields) { for(Field f:fields) {System. out. println (f); } System. out. println ("----------------------"); }}//Parent Classclassperson{PrivateString name; PrivateString sex; PrivateString address;  PublicString School;  PublicString zip;}//sub-classclassStudent extends person{PrivateString Code; PrivateString classmate;  PublicString teacher;  PublicString Desk;}

In the above code, there are two classes, one is the parent class person, and the other is the subclass student have some public and private fields, the method is slightly. Where subclasses have two public fields that inherit from the parent class
In the test Code, GetFields () and Getdeclaredfields () are called separately, and the results are as follows:

Here is a test topic, but also used in the use of this field, not before the depth of the study of the time but tangled my long ah
Requirements are:
Write a method that sets the value of the property named PropertyName in the Obj object to values.

public void SetProperty (Object obj, String propertyname, Object value) {/* Some implementation code */}

The corresponding answer is:

 public  static  void  setProperty (Object ,string Propertyname,object value) throws Exception {Class  <?> c=object.getclass (); Field F  =c.getdeclaredfield (PropertyName); //  Get all the public private fields defined by this class itself, but do not include the inherited fields  f.setaccessible (true  ); F.  set         (object  ,value);  //  Here we use the Getdeclaredfield () method to find a field in which the name is the parameter "PropertyName" in the class itself, but does not include the inherited field, and then assigns the field a value.  

It's OK code or a lot of knocking to use AH.

The difference between the reflection Getdeclaredfield () method and the GetField () method of Dark horse programmer-java Learning

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.