java类库详细讲解

源代码在线查看: getsetprop.html

软件大小: 5593 K
上传用户: add505
关键词: java
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				
				
				Getting and Setting a Property of a Bean
				(Java Developers Almanac Example)
				
				
				
				
				
				
								    BODY CODE  {font-family: Courier, Monospace;				           font-size: 11pt}				    TABLE, BODY				          {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 10pt}				    PRE   {font-family: Courier, Monospace;				           font-size: 10pt}				    H3    {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 11pt}				    A.eglink {text-decoration: none}				    A:hover.eglink {text-decoration: underline}				    -->
				
				
				
				
				
				The Java Developers Almanac 1.4
				
				        Order this book from Amazon.
				    
				
				
				
				
				
				
				
				
				
				
				
				Home
				    >
				    List of Packages
				    >
				    java.beans
				         [12 examples]
				        
				
				  
				    e6.  
				    Getting and Setting a Property of a Bean
				
				This example demonstrates how to get and set the value of a property
				in a bean using Expression and Statement.  The example gets
				and sets three types of properties, an Object, a primitive type,
				and an array.  Both these classes use the name of the method that gets
				or sets the property.
				 
				
				
				    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();
				    
				        // 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) {
				    }
				    
				    public 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() {
				            return prop2;
				        }
				        public void setProp2(int i) {
				            prop2 = i;
				        }
				    
				        // Property prop3
				        byte[] prop3;
				        public byte[] getProp3() {
				            return prop3;
				        }
				        public void setProp3(byte[] bytes) {
				            prop3 = bytes;
				        }
				    }
				
				
				
				
				             Related Examples
				        
				
				
				
				
				e1. 
				    The Quintessential Bean
				
				
				
				e2. 
				    Implementing a Bound Property
				
				
				
				e3. 
				    Implementing a Constrained Property
				
				
				
				e4. 
				    Instantiating a Bean
				
				
				
				e5. 
				    Listing the Property Names of a Bean
				
				
				
				
				
				
				
				
				        See also: 
				
				    Events
				  
				
				
				    Serialization
				  
				
				
				
				
				
				
				
				
				
				© 2002 Addison-Wesley.
				
				
				
							

相关资源