`
阅读更多

32.3 Bean Properties
    Properties are discrete, named attributes of a Java bean that can affect its appearance or behavior. They are often data fields of a bean. For example, the JButton component has a property named text that represents the text to be displayed on the button. Private data fields are
often used to hide specific implementations from the user and prevent the user from acciden-tally corrupting the properties. Accessor and mutator methods are provided instead to let the
user read and write the properties.

    属性是离散的,命名的Java bean特征能影响它的外表和行为。它们通常是bean的数据域。例如,JButton组件有一个属性名为text代表显示在button上的文本。私有数据域通常用于隐藏特定的来至用户的执行,和防止用户意外的乱用属性。提供访问器和设置器方法,而不是让用户直接读写属性。
32.3.1 Property-Naming Patterns

    The bean property-naming pattern is a convention of the JavaBeans component model that simplifies the bean developer’s task of presenting properties. A property can be a primitive data type
or an object type. The property type dictates the signature of the accessor and mutator methods.

    bean属性命名模式是JavaBean组件模型的约定,简化bean开发者的展现属性的任务。一属性可以是一原生数据类型或一个对象类型。属性类型指示了访问器和设置器的方法名。
    In general, the accessor method is named get<PropertyName>() , which takes no parameters and returns a primitive type value or an object of a type identical to the property type.

    一般而言,访问器命名为get<PropertyName>(),不接受参数且返回一个原生类型值或与属性类型一样的对象类型。
For example,

 public String getMessage()
 public int getXCoordinate()
 public int getYCoordinate()
 

For a property of boolean type, the accessor method should be named is<PropertyName>() ,
which returns a boolean value. For example,

对于一个boolean类型的属性,访问器方法应该命名为is<PropertyName>(),返回一个boolean值。

public boolean isCentered()

The mutator method should be named set<PropertyName>(dataType p) , which takes a
single parameter identical to the property type and returns void. For example,

设置器方法应该命名为set<PropertyName>(dataType p),接受单独的与属性类型相同的参数且返回void。

public void setMessage(String s)
public void setXCoordinate(int x)
public void setYCoordinate(int y)
public void setCentered(boolean centered)


Note

    You may have multiple get and set methods, but there must be one get or set method with a signature conforming to the naming patterns.

    你可以有多个get和set方法,但是必须有一个get和set方法的方法名遵从命名规范。

 

32.3.2 Properties and Data Fields
    Properties describe the state of the bean. Naturally, data fields are used to store properties.
However, a bean property is not necessarily a data field.
For example, in the MessagePanel

class in Listing 15.7, MessagePanel.java, you may create a new property named
messageLength that represents the number of characters in message. The get method for
the property may be defined as follows:

public int getMessageLength() {
      return message.length();
}

    属性描述bean的状态。自然的,数据域用于存储属性。然而,一个bean属性不一定是数据域。例如,在代码片段15.7中的MessagePanel类,MessagePanel.java, 有可以创建一个新的名为messageLength 的属性标示消息中的字符长度。属性的get方法可以如下定义:


Note
    A property may be read-only with a get method but no set method, or write-only with a set
method but no get method.

    一个属性可能只有只读的get方法但是没有set方法,或者只写的set方法但是没有get方法。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics