集册 Java实例教程 获取和设置Bean的属性

获取和设置Bean的属性

欢马劈雪     最近更新时间:2020-01-02 10:19:05

360
获取和设置Bean的属性

/* 
*来 自
 时 代 J     a    v  a - nowjava.com
*/

import java.beans.Expression;

import java.beans.Statement;


public class Main {

  public static void main(String[] args) throws Exception {

    Object o = new MyBean();

    try {

      // Get the value of prop1

      Expression expr = new Expression(o, "getProp1", new Object[0]);

      expr.execute();

      String s = (String) expr.getValue();


      // Set the value of prop1

      Statement stmt = new Statement(o, "setProp1",

          new Object[] { "new string" });

      stmt.execute();


      // Get the value of prop2

      expr = new Expression(o, "getProp2", new Object[0]);

      expr.execute();

      int i = ((Integer) expr.getValue()).intValue();/** 来 自 nowjava - 时  代  Java**/


      // Set the value of prop2

      stmt = new Statement(o, "setProp2", new Object[] { new Integer(123) });

      stmt.execute();


      // Get the value of prop1

      expr = new Expression(o, "getProp3", new Object[0]);

      expr.execute();

      byte[] bytes = (byte[]) expr.getValue();


      // Set the value of prop1

      stmt = new Statement(o, "setProp3", new Object[] { new byte[] { 0x12,

          0x23 } });

      stmt.execute();

    } catch (Exception e) {

    }

  }

}


class MyBean {

  // Property prop1

  String prop1;


  public String getProp1() {

    return prop1;

  }


  public void setProp1(String s) {

    prop1 = s;

  }


  // Property prop2

  int prop2;


  public int getProp2() {

    
展开阅读全文