⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 14_1.htm

📁 翁剀JAVA语言那门课程的教案 很多人都看多他的视频教程可惜没有ppt的教案
💻 HTM
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Weng Kai">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
   <TITLE>What is a Bean?</TITLE>
</HEAD>
<BODY>

<H2>
14.1 What is a Bean?</H2>

<HR WIDTH="100%">
<BR>After the dust settles, then, a component is really just a block of
code, typically embodied in a class. The key issue is the ability for the
application builder tool to discover the properties and events for that
component. To create a VB component, the programmer had to write a fairly
complicated piece of code following certain conventions to expose the properties
and events. Delphi was a second-generation visual programming tool and
the language was actively designed around visual programming so it is much
easier to create a visual component. However, Java has brought the creation
of visual components to its most advanced state with Java Beans, because
a Bean is just a class. You don't have to write any extra code or use special
language extensions in order to make something a Bean. The only thing you
need to do, in fact, is slightly modify the way that you name your methods.
It is the method name that tells the application builder tool whether this
is a property, an event, or just an ordinary method.

<P>In the Java documentation, this naming convention is mistakenly termed
a "Design pattern." This is unfortunate since design patterns are challenging
enough without this sort of confusion. It's not a design pattern, it's
just a naming convention and it's fairly simple:
<OL>
<LI>
For a property named xxx, you typically create two methods: getXxx( ) and
setXxx( ). Note that the first letter after get or set is automatically
lowercased to produce the property name. The type produced by the "get"
method is the same as the type of the argument to the "set" method. The
name of the property and the type for the "get" and "set" are not related.</LI>

<LI>
For a boolean property, you can use the "get" and "set" approach above,
but you can also use "is" instead of "get."</LI>

<LI>
Ordinary methods of the Bean don't conform to the above naming convention,
but they're public.</LI>

<LI>
For events, you use the "listener" approach. It's exactly the same as you've
been seeing:&nbsp; addFooBarListener(FooBarListener) and removeFooBarListener(FooBarListener)
to handle a FooBarEvent. Most of the time the built-in events and listeners
will satisfy your needs, but you can also create your own events and listener
interfaces.</LI>
</OL>
Point 1 above answers a question about something you might have noticed
in the change from Java 1.0 to Java 1.1: a number of method names have
had small, apparently meaningless name changes. Now you can see that most
of those changes had to do with adapting to the "get" and "set" naming
conventions in order to make that particular component into a Bean.

<P>We can use these guidelines to create a simple Bean:

<P>Case Study: <A HREF="case/Frog.java">Frog.java</A>

<P>First, you can see that it's just a class. Usually, all your fields
will be private, and accessible only through methods. Following the naming
convention, the properties are jumps, color, spots, and jumper (notice
the change in case of the first letter in the property name). Although
the name of the internal identifier is the same as the name of the property
in the first three cases, in jumper you can see that the property name
does not force you to use any particular name for internal variables (or,
indeed, to even have any internal variable for that property).

<P>The events this Bean handles are ActionEvent and KeyEvent, based on
the naming of the "add" and "remove" methods for the associated listener.
Finally, you can see that the ordinary method croak( ) is still part of
the Bean simply because it's a public method, not because it conforms to
any naming scheme.
<BR>
<HR WIDTH="100%">
<DIV ALIGN=right><A HREF="14_2.htm">Next Page</A></DIV>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -