JavaBean's properties

zhaozj2021-02-12  154

JavaBean's properties

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. 1.1 Simple Attribute A simple property represents a variable accompanying a pair of GET / SET methods (C language process or functions 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 aldenn1 () {// Alden1 () is Alden1 constructor, with C The meaning of the constructor is the same setBackground; setForeground (color.blue);} / * "set" attribute * / public void setstring (String newstring) {oorstring = news;} / * "get" attribute * / public String getString () {return oorstring;}} 1.2 Indexed property 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 an 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;} / * get the specified element value in the array * / public int getDataTaset (INT X) {Return Dataset [x] }} 1.3 Bound attributes A bound property is that other objects are to be notified when 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 to use some kind of The method must indicate which object of which object is to be used. In the following program, the method of ignition event is performed, and the method used in this operation is 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);} by the above code, development tools changes the call to other methods addPropertyChangeListener register JavaBean In the listener queue L of the opstring property, 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. 1.4 constrained attribute A JavaBean's constrained property is that when the value of this property changes, other Java objects that have established some Java objects that have a certain connection can reel with this property. The listener of the constrained property prevents the value of this attribute value by throwing PropertyveToException. The process is shown in Figure 3.2: The constrained attribute in the following program is PriceinCents.

转载请注明原文地址:https://www.9cbs.com/read-6449.html

New Post(0)