JavaBean的属性

来源:互联网 发布:网络语nl什么意思 编辑:程序博客网 时间:2024/05/02 02:09

 

 

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;}@font-face{font-family:"Arial Unicode MS";panose-1:2 11 6 4 2 2 2 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-134238209 -371195905 63 0 4129279 0;}@font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;mso-font-charset:0;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-1610611985 1073750139 0 0 159 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@Arial Unicode MS";panose-1:2 11 6 4 2 2 2 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-134238209 -371195905 63 0 4129279 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:15.6pt;}div.Section1{page:Section1;}-->

 

JavaBean的属性

3.1.1 Simple
属性

一个简单属性表示一个伴随有一对get/set方法(C语言的过程或函数在Java程序中称为方法)的变量。属性名与和该属性相关的get/set方法名对应。例如:如果有setXgetX方法,则暗指有一个名为X的属性。如果有一个方法名为isX,则通常暗指X是一个布尔属性(即X的值为true false)。例如在下面这个程序中:


public class alden1 extends Canvas {
string ourString= Hello; //
属性名为ourString,类型为字符串
public alden1(){
    //alden1()alden1的构造函数,与C++中构造函数的意义相同
setBackground(Color.red);
setForeground(Color.blue);
}
/* set
属性*/
public void setString(String newString) {
ourString=newString;
}
/* get
属性 */
public String getString() {
return ourString;
}
}

3.1.2 Indexed
属性

一个Indexed属性表示一个数组值。使用与该属性对应的set/get方法可取得数组中的数值。该属性也可一次设置或取得整个数组的值。例:


public class alden2 extends Canvas {
int[] dataSet={1,2,3,4,5,6}; // dataSet
是一个indexed属性
public alden2() {
setBackground(Color.red);
setForeground(Color.blue);
}
/*
设置整个数组 */
public void setDataSet(int[] x){
dataSet=x;
}
/*
设置数组中的单个元素值 */
public void setDataSet(int index, int x){
dataSet[index]=x;
}
/*
取得整个数组值 */
public int[] getDataSet(){
return dataSet;
}
/*
取得数组中的指定元素值 */
public int getDataSet(int x){
return dataSet[x];
}
}

3.1.3 Bound
属性

一个Bound属性是指当该种属性的值发生变化时,要通知其它的对象。每次属性值改变时,这种属性就点火一个PropertyChange事件( Java程序中,事件也是一个对象)。事件中封装了属性名、属性的原值、属性变化后的新值。这种事件是传递到其它的Bean,至于接收事件的Bean应做什么动作由其自己定义。


PushButtonbackground属性Dialogbackground属性bind时,若PushButtonbackground属性发生变化时,Dialogbackground属性也发生同样的变化。例:


public class alden3 extends Canvas{
String ourString= Hello; //ourString
是一个bound属性
private PropertyChangeSupport changes = new PropertyChangeSupport(this);
/**
注:Java是纯面向对象的语言,如果要使用某种方法则必须指明是要使用哪个对象的方法,在下面的程序中要进行点火事件的操作,这种操作所使用的方法是在 PropertyChangeSupport类中的。所以上面声明并实例化了一个changes对象,在下面将使用changes firePropertyChange方法来点火ourString的属性改变事件。*/

public void setString(string newString){
String oldString = ourString;
ourString = newString;
/* ourString
的属性值已发生变化,于是接着点火属性改变事件 */
changes.firePropertyChange(ourString,oldString,newString);
}
public String getString(){
return ourString;
}
/**
以下代码是为开发工具所使用的。我们不能预知alden3将与哪些其它的Bean组合成为一个应用,无法预知若alden3ourString属性发生变化时有哪些其它的组件与此变化有关,因而alden3这个Bean要预留出一些接口给开发工具,开发工具使用这些接口,把其它的JavaBean对象与 alden3挂接。*/

public void addPropertyChangeListener(PropertyChangeLisener l){
changes.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l){
changes.removePropertyChangeListener(l);
}

通过上面的代码,开发工具调用changesaddPropertyChangeListener方法把其它JavaBean注册入ourString 属性的监听者队列l中,l是一个Vector数组,可存储任何Java对象。开发工具也可使用changesremovePropertyChangeListener方法,从l中注销指定的对象,使alden3ourString属性的改变不再与这个对象有关。当然,当程序员手写代码编制程序时,也可直接调用这两个方法,把其它Java对象与alden3挂接。
3.1.4 Constrained
属性

一个JavaBeanconstrained属性,是指当这个属性的值要发生变化时,与这个属性已建立了某种连接的其它Java对象可否决属性值的改 变。constrained属性的监听者通过抛出PropertyVetoException来阻止该属性值的改变。过程如图3.2
以下没看

例:下面程序中的constrained属性是PriceInCents


public class JellyBean extends Canvas{
private PropertyChangeSupport changes=new PropertyChangeSupport(this);
private VetoableChangeSupport Vetos=new VetoableChangeSupport(this);
/*
与前述changes相同,可使用VetoableChangeSupport对象的实例Vetos中的方法,在特定条件下来阻止PriceInCents值的改变。*/


......
public void setPriceInCents(int newPriceInCents) throws PropertyVetoException {
/*
 方法名中throws PropertyVetoException的作用是当有其它Java对象否决PriceInCents的改变时,要抛出例外。*//* 先保存原来的属性值*/

int oldPriceInCents=ourPriceInCents;
/**
点火属性改变否决事件*/
vetos.fireVetoableChange(priceInCents,new Integer(OldPriceInCents), newInteger(newPriceInCents));

/**
若有其它对象否决priceInCents的改变,则程序抛出例外,不再继续执行下面的两条语句,方法结束。若无其它对象否决priceInCents的改变,则在下面的代码中把ourPriceIncents赋予新值,并点火属性改变事件*/

ourPriceInCents=newPriceInCents;
changes.firePropertyChange(priceInCents, new Integer(oldPriceInCents),newInteger(newPriceInCents));
}

/**
与前述changes相同,也要为PriceInCents属性预留接口,使其它对象可注册入PriceInCents否决改变监听者队列中,或把该对象从中注销

public void addVetoableChangeListener(VetoableChangeListener l)
{ vetos.addVetoableChangeListener(l);
}
public void removeVetoableChangeListener(VetoableChangeListener l){
vetos.removeVetoableChangeListener(l);
}
......
}
从上面的例子中可看到,一个constrained属性有两种监听者:属性变化监听者和否决属性改变的监听者。否决属性改变的监听者在自己的对象代码中有相应的控制语句,在监听到有constrained属性要发生变化时,在控制语句中判断是否应否决这个属性值的改变。
总之,某个Beanconstrained属性值可否改变取决于其它的Bean或者是Java对象是否允许这种改变。允许与否的条件由其它的BeanJava对象在自己的类中进行定义

原创粉丝点击