JavaBean's properties and the properties referred to in the general Java program, or the properties of objects in all object-oriented programming languages are a concept, and the specific embodiment in the program is the variables in the class. In JavaBean design, subdivision is divided into four categories according to the different properties: Simple, INDEX, Bound and constrained properties.
3.1.1 Simple properties
A simple attribute represents a variable accompanying a pair of GET / SET methods (C language or functionality called "method" in the Java program). Attribute name and the GET / SET method name associated with this property. For example: If there is a setx and getx method, you have a property named "X". If there is a method name ISX, it usually fails to "X" is a Boolean property (ie the value of X is True or false). For example, in this program:
Public class aldenen1 extends can (string ourstring = "hello"; // Attribute name is ourstring, type is string public aldenen1 () {// Alden1 () is the constructor of Alden1, the meaning of constructor in C , the same setBackground Color.red; setForeground (color.blue);} / * "set" attribute * / public void setString (String newstring) {oorstring = news;} / * "get" attribute * / public string getString () {Return Ourstring }}
3.1.2 Indexed Properties
A indexed property represents an array value. Use the SET / GET method corresponding to this property to get the value in the array. This property can also set or achieve the value of the entire array. example:
Public class aldenen2 extends canvas {int [] DataSet = {1, 2, 3, 4, 5, 6}; // DataSet is a indexed property public aldenen2 () {setBackground (color.red); setForeground (color.blue) } / * Set the entire array * / public void setDataSet (int [] x) {dataset = x;} / * Set a single element value in the array * / public void setDataSet (int index, int x) {dataset [index] = X;} / * acquire the entire array value * / public int [] getDataSet () {return dataset;} / * acquired the specified element value in the array * / public int getDataSet (int x) {return dataset [x];} }
3.1.3 Bound attribute
A bound property is to refer to the value of the value of the property changes. When each attribute value changes, this property is ignited to a PropertyChange event (in the Java program, the event is also an object). The attribute name, the original value of the attribute, and the new value of the attribute change are encapsulated. Such an event is to other beans, as for the receiving event bean, should be defined by itself.
Figure 3.1 is a simple Bound property diagram showing that when the pushbutton's Background property is bind with the Dialog's Background property, if the PushButton's Background property changes, the Dialog's Background property also changes the same. example:
public class alden3 extends Canvas {String ourString = "Hello"; // ourString bound property is a private PropertyChangeSupport changes = new PropertyChangeSupport (this); / ** Note: Java is a pure object-oriented language, if you want some way then It must be indicated in which object is used, and the operation of the ignition event is performed in the following program, which is used in the PropertyChangeSupport class. So the above declaration and instantiate a Changes object, and will use the Changes's FirePropertyChange method to ignit the property change event. * / Public void setString (string newString) {String oldString = ourString; ourString = newString; / * ourString attribute value has changed, then followed by firing property change event * / changes.firePropertyChange ( "ourString", oldString, newString); } public string getString () {return ourstring;} / ** The following code is used for development tools. We cannot predict that Alden3 will become an application with which other bean combines, which can not predict which other components are related to this change when Alden3 is related, so Alden3 bean wants to reserve some interfaces to development tools. Tools use these interfaces to mount other JavaBean objects with Alden3. * /
public void addPropertyChangeListener (PropertyChangeLisener l) {changes.addPropertyChangeListener (l);} public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);}
Through the above code, the development tool calls Changes's AddPropertyChangeListener method to register other JavaBeans into the listener queue L of the Oudstring property, and L is a vector array that stores any Java objects. The development tool can also use the Changes's RemovePropertyChangeListener method to log out of the specified object from the L, so that the change of the Oudtring property of Alden3 is no longer related to this object. Of course, when the programmer is handwritten, these two methods can also be called directly to hill other Java objects with Alden3. 3.1.4 Constrained Attribute
A javabean constrained property means that when the value of this property is changed, other Java objects that have been established with this property have been established to reject the value of the value. The listener of the constrained property prevents the value of this attribute value by throwing PropertyveToException. The process is shown in Figure 3.2
Example: The constrained attribute in the following program is pricenetics.
public class JellyBean extends Canvas {private PropertyChangeSupport changes = new PropertyChangeSupport (this); private VetoableChangeSupport Vetos = new VetoableChangeSupport (this); / * aforementioned changes the same manner as in Example Vetos VetoableChangeSupport object may be used, to prevent the specific conditions down PriceInCents Value changes. * / ...... Public void setPriceinCents (int newpriceincents) throws propertyvetoException {/ * Method name The role of THROWS PropertyveToexception is to throw exceptions when there are other Java objects to veto the change of PriceNCents. * / / * Prevent the original attribute value * /
INT OLDPRICEINCENTS = OURPRICEINCENTS; / ** ignition attribute change veto * / Vetos.FireveToableChange ("PriceNCents", New Integer (OldPriceInCents), New Integer (NewPriceInCents);
/ ** If there is any other object veto of the change of PriceINCENTS, the program throws an exception, no longer continues to perform the following two statements, the method ends. If there is no other object to veto the change of the priceINCENTS, we give the out of our code to the new value in the following code, and ignite the attribute change event * /
OurpriceIncents = NewPriceInCents; Changes.FirePropertyChange ("PriceinCents", New Integer (OldPriceIncents)
/ ** is the same as the aforementioned Changes, and the interface is reserved for the PriceIncents property, so that other objects can be registered into the PriceINCENTS veto listener queue, or log out of the object
public void addVetoableChangeListener (VetoableChangeListener l) {vetos.addVetoableChangeListener (l);} public void removeVetoableChangeListener (VetoableChangeListener l) {vetos.removeVetoableChangeListener (l);} ......} can be seen from the above example, a constrained There are two listeners: attribute change listeners and veto listeners. The listener of the veto attribute changes to the corresponding control statement in its object code, and when the monitoring of the constrained property is changed, it is determined whether to veto this property value should be rejected in the control statement. In summary, whether a bean constrained property value can change depending on other beans or whether the Java object allows such changes. The allowable condition is defined in its own class by other beans or Java objects.