??xml version="1.0" encoding="utf-8" standalone="yes"?>
1、build.xml文g
<?xml version="1.0" encoding="gb2312"?>
<project name="message" basedir="." default="help">
<property name="webapp.name" value="message"/>
<import file="../message/common.xml"/>
<!-- "test" d用于执行junit的单元测?-->
<target name="test" description="Run junit test.">
<mkdir dir="${build.dir}/test"/>
<mkdir dir="${build.dir}/report"/>
<junit printsummary="yes">
<formatter type="xml"/>
<test name="cn.hxex.message.test.AllTests"/>
<classpath refid="classpath"/>
</junit>
<junitreport todir="${build.dir}/test">
<fileset dir=".">
<include name="TEST-*.xml"/>
</fileset>
<report format="noframes" todir="${build.dir}/report"/>
</junitreport>
</target>
<!-- "run"d用于q行指定E序 -->
<target name="run">
<java classname="cn.hxex.basic.exercise.LifeCycle">
<classpath refid="classpath"/>
</java>
</target>
</project>
2、common.xml文g
<?xml version="1.0" encoding="gb2312"?>
<project>
<!-- 直接讄目文g夹变量,也可以写一个配|文Ӟ用file属性引?-->
<!--
<property file="webapp.properties"/>
-->
<property name="src.dir" value="src"/>
<property name="build.dir" value="WebRoot"/>
<property name="webpage.dir" value="${build.dir}/webpage"/>
<property name="webinf.dir" value="${build.dir}/webinf"/>
<property name="lib.dir" value="${webinf.dir}/lib"/>
<property name="classes.dir" value="${webinf.dir}/classes"/>
<!-- 取得环境变量TOMCAT_HOME的?-->
<property environment="env"/>
<property name="tomcat.home" value="${env.TOMCAT_HOME}"/>
<property name="ant.home" value="${env.ANT_HOME}"/>
<!-- 引入有关tomcat的变量配|文Ӟ也可以象上面一P直接在这里设|?-->
<property file="tomcat.properties"/>
<!--
<property name="tomcat.server" value="localhost"/>
<property name="tomcat.manager.url" value="http://${tomcat.server}/manager"/>
<property name="tomcat.username" value="admin"/>
<property name="tomcat.password" value=""/>
-->
<!-- 讄d要执行的cL?-->
<!-- 可引入设|tomcatd的配|文?-->
<taskdef file="tomcatTasks.properties">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<!-- 也可逐一讄d
<taskdef name="deployc" classname="org.apache.catalina.ant.DeployTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="remove" classname="org.apache.catalina.ant.RemoveTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="roles" classname="org.apache.catalina.ant.RolesTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
-->
<!--
<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath>
<pathelement path="${ant.home}/lib/ant-junit.jar"/>
</classpath>
</taskdef>-->
<!-- 讄c\?-->
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<fileset dir="${tomcat.home}/common/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${ant.home}/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${classes.dir}"/>
</path>
<!-- "help"d用于昄帮助信息 -->
<target name="help">
<echo message=""/>
<echo message="${webapp.name} build file"/>
<echo message="----------------------------------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="help --> Print this screen."/>
<echo message="start --> Start Tomcat application."/>
<echo message="stop --> Stop Tomcat application."/>
<echo message="list --> List Tomcat applications."/>
<echo message="compile --> Compile all Java files."/>
<echo message="deploy --> Deploy the project to the tomcat."/>
<echo message="clean --> Clean output directories."/>
<echo message="new --> Creates a new project with the specified name."/>
<echo message="test --> Run junit test."/>
</target>
<!-- "start"d用于启动Tomcat -->
<target name="start" description="Start Tomcat application">
<start url="${tomcat.manager.url}"
username="${tomcat.username}"
password="${tomcat.password}"
path="/${webapp.name}"/>
</target>
<!-- "stop"d用于停止Tomcat -->
<target name="stop" description="Stop Tomcat application">
<stop url="${tomcat.manager.url}"
username="${tomcat.username}"
password="${tomcat.password}"
path="/${webapp.name}"/>
</target>
<!-- "list"d用于昄Tomcat中应用名 -->
<target name="list" description="List Tomcat applications">
<list url="${tomcat.manager.url}"
username="${tomcat.username}"
password="${tomcat.password}"/>
</target>
<!-- "clean"d用于清除所有文件及文g?-->
<target name="clean" description="Clean output directories">
<delete dir="${classes.dir}"/>
<delete dir="${tomcat.home}/webapps/${webapp.name}"/>
</target>
<!-- "compile"d用于~译源代码和复制Hibernate配置文g到指定类文g?-->
<target name="compile" description="compile the java source and copy the configuration file">
<mkdir dir="${classes.dir}"/>
<javac destdir="${classes.dir}"
target="1.5" source="1.5" debug="true"
deprecation="true" optimize="false" failonerror="true">
<src path="${src.dir}"/>
<classpath refid="classpath"/>
</javac>
<!-- 复制hibernate映射文g到项目下的classes文g?-->
<copy todir="${classes.dir}">
<fileset dir="${src.dir}" includes="**/*.xml"/>
<fileset dir="${src.dir}" includes="**/*.properties"/>
</copy>
<!-- 复制配置文g到项目下的classes文g?-->
<copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF/classes">
<fileset dir="${classes.dir}" includes="**/*.xml"/>
<fileset dir="${classes.dir}" includes="**/*.properties">
<exclude name="**/*_zh.properties"/>
</fileset>
</copy>
<!-- 对中文配|文件进行编码编?-->
<native2ascii dest="${tomcat.home}/webapps/${webapp.name}/WEB-INF/classes"
encoding="UTF-8"
src="${classes.dir}" includes="**/*_zh.properties"/>
</target>
<!-- "deploy"d用于部v目和复刉|文件到tomcat容器 -->
<target name="deploy" depends="compile" description="deploy the project to the tomcat!">
<!-- 复制web|页文g -->
<copy todir="${tomcat.home}/webapps/${webapp.name}" preservelastmodified="true">
<fileset dir="${webpage.dir}">
<include name="**/*.*"/>
<exclude name="**/readme.txt"/>
</fileset>
</copy>
<!-- 复制cL?-->
<copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF/classes"
preservelastmodified="true">
<fileset dir="${classes.dir}"/>
</copy>
<!-- 复制库文?-->
<copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF/lib"
preservelastmodified="true">
<fileset dir="${lib.dir}">
<exclude name="**/readme.txt"/>
</fileset>
</copy>
<!-- 复制配置文g -->
<copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF"
preservelastmodified="true">
<fileset dir="${webinf.dir}">
<exclude name="**/readme.txt"/>
</fileset>
<fileset dir="${build.dir}">
<include name="*.xml"/>
<include name="*..properties"/>
</fileset>
</copy>
</target>
<!-- "new"d用于创徏一个新的项目,复制原项目,q改为新目?-->
<target name="new" description="creates a new project with the specified name">
<echo level="info">
+----------------------------------------------------------+
| -- Welcome to the AppDemo New Application Wizard! -- |
| |
| To create a new application, please answer the following |
| questions. |
+----------------------------------------------------------+
</echo>
<echo/>
<!-- 提示用户输入新徏的项目名 -->
<input message="What would you like to name your application [myapp]?"
addproperty="app.name" defaultvalue="myapp"/>
<echo level="info">
Creating new application named '${app.name}'...
</echo>
<copy todir="../${app.name}">
<fileset dir="${basedir}">
<exclude name="${lib.dir}/*.txt"/>
</fileset>
</copy>
<!-- 替换应用?-->
<replaceregexp flags="g">
<regexp pattern="message"/>
<substitution expression="${app.name}"/>
<fileset dir="../${app.name}">
<include name="**"/>
<exclude name="**/*.jar"/>
</fileset>
</replaceregexp>
</target>
</project>
3、webapp.properties文g
src.dir=src
build.dir=WebRoot
webpage.dir=${build.dir}/webpage
webinf.dir=${build.dir}/webinf
lib.dir=${webinf.dir}/lib
classes.dir=${webinf.dir}/classes
4、tomcatTasks.properties文g
deploy=org.apache.catalina.ant.DeployTask
install=org.apache.catalina.ant.InstallTask
list=org.apache.catalina.ant.ListTask
reload=org.apache.catalina.ant.ReloadTask
remove=org.apache.catalina.ant.RemoveTask
resources=org.apache.catalina.ant.ResourcesTask
roles=org.apache.catalina.ant.RolesTask
start=org.apache.catalina.ant.StartTask
stop=org.apache.catalina.ant.StopTask
undeploy=org.apache.catalina.ant.UndeployTask
5、tomcat.properties文g
tomcat.server=localhost
tomcat.manager.url=http://${tomcat.server}/manager
tomcat.username=admin
tomcat.password=
private static BigInt fillZero(BigInt arr, int int0){
BigInt temp = arr;
for(int i=0; i<int0; i++){
temp = new BigInt("0" + temp.getArr());
}
return temp;
}
/** 对两个BigInt型对象做加法q算Q返回结果ؓBigInt?*/
public static BigInt add(BigInt one, BigInt two) {
BigInt arrSum = null;
BigInt arr = null;
if(one.intArr.length > two.intArr.length) {
arrSum = one;
arr = fillZero(two, one.intArr.length - two.intArr.length);
} else if(one.intArr.length == two.intArr.length) {
arrSum = two;
arr = one;
} else {
arr = two;
arrSum = fillZero(one, two.intArr.length - one.intArr.length);
}
int i=arr.intArr.length-1;
for(; i>=1; i--){
int intTemp = arrSum.intArr[i] + arr.intArr[i];
arrSum.intArr[i] = intTemp%10;
if(intTemp>9){
arrSum.intArr[i-1] = arrSum.intArr[i-1] + intTemp/10;
}
}
int intTemp = arrSum.intArr[0] + arr.intArr[0];
arrSum.intArr[i] = intTemp%10;
if(intTemp>9){
return new BigInt("1" + arrSum.getArr());
} else {
return arrSum;
}
}
/** E序入口Q格式化输出 */
public static void main(String[] args) {
try {
BigInt one = new BigInt(args[0]);
System.out.println(" " + one.getArr());
BigInt two = new BigInt(args[1]);
System.out.println("+ " + two.getArr());
System.out.println("----------------------------");
BigInt sum = BigInt.add(one, two);
System.out.println(" " + sum.getArr());
} catch(java.lang.ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标界");
}
}
}
1、版权信息,必须?java 文g的开_比如Q?
/* * Copyright ? 2000 Shanghai XXX Co. Ltd. * All right reserved. */
其他不需要出现在 javadoc 的信息也可以包含在这里?BR>
2、Package/Imports
package 行要?import 行之前,import 中标准的包名要在本地的包名之前,而且按照字母序排列。如?import 行中包含了同一个包中的不同子目录,则应该用 * 来处理?
package hotlava.net.stats;
import java.io.*;
import java.util.Observable;
import hotlava.util.Application
q里 java.io.* 使用来代替InputStream and OutputStream 的?BR> Q关于这点,有点疑问Q我知道详细的写出引用类的\径及cdQ可以较快的搜烦到要引用的类Q可是一个包中要引用的类q多的话Q是不是?来处理,比全部写出来好哩Q全部写出来会很长,不方侉K读,不过现在的IDE大都有收~功能,问题也不大吧Q)
3、对cȝ注释Q解释类的功能)Q采用文档注释,写在cd明之前。例如:
/**
* A class representing a set of packet and byte counters
* It is observable to allow it to be watched, but only
* reports changes when the current set is complete
*/
接下来是cd义的格式Q包含了在不同的行的 extends ?implements
public class CounterSet extends Observable implements Cloneable
Q这样写呀Q第一ơ看刎ͼ我想我不太喜Ƣ这U写法,q也是规范吗Q)
4、类的成员变量,在声明之前也要用文档注释说明其意义。例如:
/**
* Packet counters
*/
protected int[] packets;
public 的成员变量必ȝ成文档(JavaDocQ。proceted、private?package 定义的成员变量如果名字含义明的话,可以没有注释?BR> Q因为public的成员变量是cȝ接口Q是供用L序员使用的,所以必ȝ成文档,所以必ȝ文档注释标注Q?BR> 5、类变量的存取的Ҏ(gu)Q在其声明之前也要用文档注释说明其意义?BR> 若它只是单的用来类的变量赋D取值的话,可以单的写在一行上?例如Q?/P>
/** * Get the counters * @return an array containing the statistical data. This array has been * freshly allocated and can be modified by the caller. */ public int[] getPackets() { return copyArray(packets, offset); } public int[] getBytes() { return copyArray(bytes, offset); } public int[] getPackets() { return packets; } public void setPackets(int[] packets) { this.packets = packets; }
其它的方法不要写在一行上?BR>
6、构造函敎ͼ应该用递增的方式写Q比如:参数多的写在后面Q?BR> 讉Kcd ("public", "private" {?) ?M "static", "final" ?"synchronized" 应该在一行中Qƈ
且方法和参数另写一行,q样可以使方法和参数更易诅R?BR>
public CounterSet(int size){ this.size = size; }
Q这么写Q不习惯Q保留意见) 7?FONT size=3>克隆Ҏ(gu)Q如果一个类是可以被克隆的,那么在定义类时应该重写其l承?clone Ҏ(gu)Q?BR>public Object clone() { try { CounterSet obj = (CounterSet)super.clone(); obj.packets = (int[])packets.clone(); obj.size = size; return obj; }catch(CloneNotSupportedException e) { throw new InternalError("Unexpected CloneNotSUpportedException: "
+ e.getMessage()); } }
8、类Ҏ(gu)Q定义ؓfinal或static的,c而非其实例对象所拥有的方法)Q例如:
/** * Set the packet counters * (such as when restoring from a database) */ protected final void setArray(int[] r1, int[] r2, int[] r3, int[] r4) throws IllegalArgumentException { // // Ensure the arrays are of equal size // if (r1.length != r2.length || r1.length != r3.length || r1.length != r4.length) throw new IllegalArgumentException("Arrays must be of the same size"); System.arraycopy(r1, 0, r3, 0, r1.length); System.arraycopy(r2, 0, r4, 0, r1.length); }
9?FONT size=3>toString Ҏ(gu)Q每一个类都应该对l承自Objectcȝ toString Ҏ(gu)q行重写Q?BR>public String toString() { String retval = "CounterSet: "; for (int i = 0; i < data.length(); i++) { retval += data.bytes.toString(); retval += data.packets.toString(); } return retval; }
10、main Ҏ(gu)
如果main(String[]) Ҏ(gu)已经定义? 那么它应该写在类的底?
Q这样子的?Z么把mainQ)Ҏ(gu)写在cȝ一开始不好哩Q)
1、关键词和操作符之间加适当的空根{(q个我知道,比如“i+j=x;”应该写成“i + j = x;”,q样看v来是不是漂亮多了Q?
2、相对独立的E序块与块之间加I。(嗯,q样便于阅读Q方法与Ҏ(gu)之间IQ方法内部也应该Ҏ(gu)语句内容自然IQ?nbsp;
3、较长的语句、表辑ּ{要分成多行书写Q对呀Q屏q显C有限)。划分出的新行要q行适应的羃q,使排版整齐,语句可读?nbsp;
4、长表达式要?FONT color=#ff0000>低优先操作W处划分新行Q操作符攑֜新行之首。(有道理,在低优先U操作符处断行,不会q于影响代码的阅ȝ解,操作符攑֜新行之首也是q个道理Q?nbsp;
5、@环、判断等语句中若有较长的表达式或语句Q则要进行适应的划分。(q个Q?FONT color=#ff0000>怎么划分哩?Q?nbsp;
6、若函数或过E中的参数较长,则要q行适当的分行。(是的Q分行时应该参数的cd攑֜新行之首Q且要有适当的羃q,比如可以与上一行的参数寚wQ这样阅L晰、方便)
7、不允许把多个短语句写在一行中Q即一行只写一条语句。(嗯,对于“int iQjQxQ”这L语句是不是分成三行,一行一句更好些Q尤其在它做为类的成员变量时Q?nbsp;
8、函数或q程的开始、结构的定义及@环、判断等语句中的代码都要采用~进风格?nbsp;
9、C/C++语言是用大括号‘{’和‘}’界定一D늨序块的,~写E序块时‘{’和 ‘}’应各独占一行ƈ且位于同一列,同时与引用它们的语句左对齐。在函数体的开始、类的定义、结构的定义、枚丄定义以及if、for、do、while、switch、case语句中的E序都要采用如上的羃q方式?BR> Q呵呵,q里怎么说的是C和C++语言Q不是在讲java 的编码规范吗Q不q,q个问题一直困扰着我,C和C++语言中这U打括号的Ş式,我觉得很好,W合Z的阅M惯,哪个“{”和哪个“}”是一对儿的,一目了Ӟ使代码的层次非常清晰。然而我在绝大多数的java 语言代码Q包括sun公司所l的JFC源代码中看到的都是下列格式:
public void myMethodQ){
int i = 2;
......
}
q是Z么哩Qjava的“始作蛹者”(好象的贬义词Q开个玩W)当初是出于什么考虑哩?甚至有的老师把它做ؓ“地道的java”程序的标志。虽然我不喜Ƣ这U书写Ş式,可是既然大家都这么写Q?FONT color=#ff0000>是不是我也应该遵循这U“约定俗成”的格式?/FONT>Q?/P>
public class DiaryEditToolBarPane extends JPanel implements ActionListener
{
JTextPane jTextPane;
DiaryEditToolBar diaryEditToolBar;
JPanel jPanelSouth = new JPanel();
JButton jButtonUp = new JButton(ConstValue.IMG_UP);
//JButton jButtonDn = new JButton(ConstValue.IMG_DN);
public DiaryEditToolBarPane(JTextPane jTextPane)
{
this.jTextPane = jTextPane;
diaryEditToolBar = new DiaryEditToolBar(this.jTextPane);
jButtonUp.setMargin(new Insets(0,0,0,0));
//jButtonDn.setMargin(new Insets(0,0,0,0));
jButtonUp.setActionCommand("up");
//jButtonDn.setActionCommand("down");
//jPanelSouth.setLayout(new GridLayout(2,1));
jPanelSouth.add(jButtonUp);
//jPanelSouth.add(jButtonDn);
this.setLayout(new BorderLayout());
this.add(diaryEditToolBar,BorderLayout.CENTER);
this.add(jPanelSouth, BorderLayout.SOUTH);
jButtonUp.addActionListener(this);
//jButtonDn.addActionListener(this);
}
public void setTextPane(JTextPane jTextPane)
{
this.jTextPane = jTextPane;
diaryEditToolBar.setTextPane(this.jTextPane);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("up"))
{
diaryEditToolBar.moveUp();
}
/*
else if(e.getActionCommand().equals("down"))
{
diaryEditToolBar.moveDown();
}*/
}
}
/**定义一个具体实现工hlg布局与滚动的cDiaryEditToolBar */
public class DiaryEditToolBar extends JToolBar
{
/**用于保存工具按钮的对象数l?/
private BrimlessButton[] toolBarButton;
/**工具按钮增、删索引?/
private static int index = 0;
private void init()
{
this.setFloatable(false);
BrimlessButton[] temp =
{
toolBarButtonSave,toolBarButtonSave,toolBarButtonCut,
toolBarButtonCopy,toolBarButtonPaste,toolBarButtonFontStyle,
toolBarButtonFontSize,toolBarButtonFontForeground,toolBarButtonBackground,
toolBarButtonBold,toolBarButtonItalic,toolBarButtonUnderline,
toolBarButtonFlusLeft,toolBarButtonCenter,toolBarButtonFlushRight,
toolBarButtonUndo,toolBarButtonRedo,toolBarButtonIndentLeft,
toolBarButtonIndentRight,toolBarButtonURL,toolBarButtonImage,
toolBarButtonDateTime
};
toolBarButton = temp;
for(int i=0; i<22; i++)
{
this.addElement(toolBarButton[i]);
}
}
private void addElement(JButton jButton)
{
this.add(jButton);
}
private void delElement(JButton jButton)
{
this.remove(jButton);
}
/**删除工具栏开头的工具按钮Qƈ其d到工h末尾Q以实现工具栏向上滚动的效果*/
public void moveUp()
{
delElement(toolBarButton[index]);
this.revalidate();
addElement(toolBarButton[index]);
index = (++index)%22;
}
/**增加工具栏开头的工具按钮Q以实现工具栏向下滚动的效果*/
public void moveDown()
{
//addElement(toolBarButton[index]);
//this.revalidate();
//repaint();
}
}