JAva反射ノート2——ドメイン

4178 ワード

これはjavaクラスにおけるドメインの反射実装方式である.
package com.xxx;

import java.lang.reflect.Field;

import org.junit.Test;

public class Demo2 {
    
    //test1-3     public、private、                。
    //public  getField,private  getDeclaredField   setAccessible
    //        getDeclaredField    setAccessible
    @Test
    public void test1() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    {
        SwordBean sw=new SwordBean();
        Class c1=Class.forName("com.xxx.SwordBean");
        Field f=c1.getField("text");
        String t=(String)f.get(sw);
        System.out.print(t);
        
    }
    
    @Test
    public void test2() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    {
        SwordBean sw=new SwordBean();
        Class c1=Class.forName("com.xxx.SwordBean");
        Field f=c1.getDeclaredField("secret");
        f.setAccessible(true);
        String s=(String)f.get(sw);
        System.out.print(s);
        
    }    
    
    @Test
    public void test3() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    {
        SwordBean sw=new SwordBean();
        Class c1=Class.forName("com.xxx.SwordBean");
        Field f=c1.getDeclaredField("review");
        String r=(String)f.get(sw);
        System.out.print(r);
        
    }
    
    @Test
    public void test4() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    {
        SwordBean sw=new SwordBean();
        Class c1=Class.forName("com.xxx.SwordBean");
        Field f=c1.getDeclaredField("Attack");
        float a=(float)f.get(sw);
        System.out.print(a);
        
    }
    
    @Test
    public void test5() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    {
        SwordBean sw=new SwordBean(new PropertyBean());
        Class c1=Class.forName("com.xxx.SwordBean");
        Field f=c1.getDeclaredField("propertyOfSword");
        PropertyBean pb=(PropertyBean)f.get(sw);
        System.out.println(
                "file: "+pb.getFire()+"
"+                 "water: "+pb.getWater()+"
"+                 "wind: "+pb.getWind()+"
"+                 "rock: "+pb.getRock()+"
"+                 "thunder: "+pb.getThunder()+"
"                 );     }      }

beanは次のように定義されています.
package com.xxx;

public class SwordBean {

    String swordName;
    float price;
    float Attack;
    int sid;
    PropertyBean propertyOfSword;
    
    public String text="good sword!";
    private String secret="bad sword!";
    String review="soso sword!";

    public String getSwordName() {
        return swordName;
    }

    public void setSwordName(String swordName) {
        this.swordName = swordName;
    }
    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public float getAttack() {
        return Attack;
    }

    public void setAttack(float attack) {
        Attack = attack;
    }

    private int getSid() {
        return sid;
    }

    private void setSid(int sid) {
        this.sid = sid;
    }

    public PropertyBean getPropertyOfSword() {
        return propertyOfSword;
    }

    public void setPropertyOfSword(PropertyBean propertyOfSword) {
        this.propertyOfSword = propertyOfSword;
    }

    public SwordBean() {
        setAttack(1);
    }

    public SwordBean(float p) {
        setPrice(p);
    }

    public SwordBean(float p, float a) {
        setAttack(a);
        setPrice(p);
    }

    public SwordBean(PropertyBean pro) {
        setPropertyOfSword(pro);

    }
    
    private SwordBean(String Name)
    {
        setSwordName(Name);
    }

}