package com.njust.test.kemu2;

import java.lang.reflect.Field;

/**
 * 1.public,protected的可以直接访问到
 * 2.private默认不能访问；但是可以设置setAccessible(true)之后访问
 */
class RTest
{
    public String field1 = "default1";
    
    String field2 = "default2";
    
    private String field3 = "default3";
}

/**
 * read1:test1
 * read2:test2
 * read3:default3
 * read3:test3
 */
public class ReflectionTest2
{
    public static void main(String[] args)
        throws Exception
    {
        RTest rTest = new RTest();
        
        Field field = RTest.class.getField("field1");
        field.set(rTest, "test1");
        System.out.println("read1:" + field.get(rTest));
        
        field = RTest.class.getDeclaredField("field2");
        field.set(rTest, "test2");
        System.out.println("read2:" + field.get(rTest));
        
        field = RTest.class.getDeclaredField("field3");
        //不可以直接访问
        //java.lang.IllegalAccessException
        //System.out.println(field.get(rTest));
        //java.lang.IllegalAccessException
        //field.set(rTest, "test3");
        
        //setAccessible后可以访问
        field.setAccessible(true);
        System.out.println("read3:" + field.get(rTest));
        field.set(rTest, "test3");
        System.out.println("read3:" + field.get(rTest));
    }
}

@@ -293,44 +293,51 @@
 %0A * 
-read1:
+console:%0A * %3Cpre%3E%0A * 
 test1
+11
 %0A * 
-read2:
 test2
+22
 %0A * 
-read3:
 defa
@@ -348,19 +348,25 @@
 %0A * 
-read3:test3
+test333%0A * %3C/pre%3E
 %0A */
@@ -530,24 +530,25 @@
  Field field
+1
  = RTest.cla
@@ -553,16 +553,24 @@
 lass.get
+Declared
 Field(%22f
@@ -583,32 +583,33 @@
 );%0A        field
+1
 .set(rTest, %22tes
@@ -610,16 +610,18 @@
 , %22test1
+11
 %22);%0A    
@@ -647,24 +647,14 @@
 tln(
-%22read1:%22 + 
 field
+1
 .get
@@ -672,37 +672,44 @@
        %0A        
+Field 
 field
+2
  = RTest.class.g
@@ -740,32 +740,33 @@
 );%0A        field
+2
 .set(rTest, %22tes
@@ -767,16 +767,18 @@
 , %22test2
+22
 %22);%0A    
@@ -804,24 +804,14 @@
 tln(
-%22read2:%22 + 
 field
+2
 .get
@@ -837,21 +837,28 @@
         
+Field 
 field
+3
  = RTest
@@ -1069,32 +1069,34 @@
 et(rTest, %22test3
+33
 %22);%0A        %0A   
@@ -1130,24 +1130,25 @@
        field
+3
 .setAccessib
@@ -1184,32 +1184,22 @@
 println(
-%22read3:%22 + 
 field
+3
 .get(rTe
@@ -1213,24 +1213,25 @@
        field
+3
 .set(rTest, 
@@ -1236,16 +1236,18 @@
 , %22test3
+33
 %22);%0A    
@@ -1273,24 +1273,14 @@
 tln(
-%22read3:%22 + 
 field
+3
 .get

Update ReflectionTest2.java