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方法。
分享到:
相关推荐
北大青鸟Y2 指导学习二 开发基于JSP Servlet JavaBean网上交易系 包括 指导学习课答案 提供给学员的素材 授课素材,PPT 功能全部实现,值得参考 是编程开发 学习参考的好资料
使用Oracle数据库+Eclipse+MyEclipse,JSP+Servlet+JavaBean的基本组合,适合刚刚学习JSP的朋友参考一下~
NULL 博文链接:https://yehao0716.iteye.com/blog/1542231
自己做的,部分代码参考网上的资料,一个纯的jsp+javaBean简单的新闻发布系统,比较适合学习jsp和javabean的初学者,界面做得简单了点,但是新闻系统的基本功能也都实现了
北大青鸟Y2 指导学习一 开发基于JSP Servlet JavaBean网上交易系 包括 指导学习课答案 提供给学员的素材 授课素材,PPT 功能全部实现,值得参考 是编程开发 学习参考的好资料
基于JSP+JavaBean+Servlet的课程管理系统的总体设计目标是为教师和学生提供一个课程管理平台。学生可以通过该系统快速地选择自己喜欢的课程,并且可以查看教师已经给出的某门课程的成绩,此外学生还可以更改自己的...
开发基于JSP Servlet JavaBean的网上交易系统(JSP Servlet JavaBean Web Service)分7部分上传,比较可以的学习资料,可以下载看看。 查找时请输入"accp5.0 jsp servlet javaBean"关键字查找,或者是"开发基于JSP/...
2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于...
2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于...
2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于JSP+...
2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于JSP+...
之前所发资源有ssh SSH2 servlet+jsp+javabean等框架的web开发供大家学习~eclipse.3.4,tomcat6.0,jdk1.6,数据库因人而异
2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于jsp+...
当你想学struts2或springmvc框架之前,可以先看看这个web工程,因为Servlet技术是web框架的基础,本工程简单易懂,相信对你的框架学习会有很好的帮助
2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)正在做课程设计、期末大作业和毕设项目的学生、或者相关技术学习者作为学习资料参考使用。 3、该资源包括全部源码,需要具备一定基础才能...
基于jsp+javabean的web在线考试系统,可以实现选择题、填空题、上机题(word、excel、ppt)的自动组卷和阅卷。 做毕业设计,课程设计或者正在学习相关技术知识的朋友可以下载资源学习。想继续学习相关知识的可以关注...
北大青鸟书后指导学习项目....“我听我购唱片网”
1:中小型物流信息管理系统 2:jsp实现,代码简单易读,适合初学者学习 3:系统可运行,内含数据库
二. 项目组件: Servlet+Jsp+JavaBean+ Tomcat+Java 三. 项目介绍: 1. 基于 Servlet+Jsp+JavaBean+Java实现的课程管理系统, 可用于毕业设计。代码结构清晰,可学习或者重新定制开发。 2. 项目具有详细的开发文档...
2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于...