1、Lishoulin,PTC普联软件(中国)有限公司2011年度入职培训,济南燕山学院PANSOFTJULY,2011编码规范(JAVA版)JAVA之路JAVA开发人员编码规范Technology Team Progresscom.pansoft.financecom.pansoft.public class JRaster;class JImageSprite interface IRasterDelegate;interface IStoring; run();runFast();getBackground(); char c;int i;float myWidth; int mEmploy
2、eeId;String mName;Customer mCustomer; public void callMe(int pAge, String pName)static final int MIN_WIDTH = 4;static final int MAX_WIDTH = 999;static final int GET_THE_CPU = 1; /* * Classname * * Version information * * Date * * Copyright notice */ package java.awt; import java.awt.peer.CanvasPeer;
3、 /正常断行 someMethod(int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother) . /若断行后很靠右,则下面行可以缩进8个空格,看上去更美 观。 private static synchronized horkingLongMethodName(int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother) . /缩小过少,不易阅读1 if (condition1 & condition2)2 | (
4、condition3 & condition4)3 |!(condition5 & condition6) 4 doSomethingAboutIt(); /这一行不易阅读识别 /较多的缩进,便于阅读5 if (condition1 & condition2)6 | (condition3 & condition4)7 |!(condition5 & condition6) 8 doSomethingAboutIt(); 1 alpha = (aLongBooleanExpression) ? beta : gamma;2 alpha = (aLongBooleanExpression) ?
5、beta : gamma;3 alpha = (aLongBooleanExpression) ? beta : gamma; /* * Here is a block comment. */1 if (condition) 2 /* Handle the condition. */3 /code.4 1 if (a = 2) 2 return TRUE; /* special case */3 else 4 return isPrime(a); /* works only for odd a */5 1 if (foo 1) 2. / Do a double-flip.3 4 else 5.
6、 return false; / Explain why here.6 7. /if (bar 1) 8 / / Do a triple-flip.9 /10 /else 11 / return false;12 / /* * The Example class provides . */ public class Example .1 int level; / indentation level2 int size; / size of table /这样不好 3 int level, size; 4 int foo, fooarray; /错误写法错误写法1 void myMethod()
7、 2 int int1 = 0; / 好的声明3 if (condition) 4 int int2 = 0; / 不好的声时5 .6 7 1 class Sample extends Object 2 int ivar1;3 int ivar2;4 Sample(int i, int j) 5 ivar1 = i;6 ivar2 = j;7 8 int emptyMethod() 9 .10 1 argv+; / Correct 2 argc-; / Correct 3 argv+; argc-; / AVOID! 1 if ( this.isExist(“0001”) 2 argc-; /
8、 Correct 3 System.out.println(“HELLO”); 4 1. return; 2. return myDisk.size(); 3. return (size ? size : defaultSize); 1. if (condition) 2 statements; 3 4 if (condition) 5 statements; 6 else 7 statements; 8 9 If (condition) 10 statements;11 else if (condition) 12 statements; 13 else 14 statements; 15
9、if (condition) /非法的,应该以包括 statement; for (initialization; condition; update) statements; while (condition) statements; while (condition) ; do statements; while (condition);1 switch (condition) 2 case ABC:3 statements;4 /* falls through */5 case DEF:6 statements;7 break;8 case XYZ:9 statements;10 bre
10、ak;11 default:12 statements;13 break;14 try statements; catch (ExceptionClass e) statements; finally statements; 1 a += c + d;2 a = (a + b) / (c * d);3 while (d+ = s+) 4 n+;5 6 printSize(size is + foo + n); 7 for (expr1; expr2; expr3)8 myMethod(byte) aNum, (Object) x);9 myMethod(int) (cp + 5), (int)
11、 (i + 3) + 1); 1 ooBar.fChar = barFoo.lchar = c; / 禁止此种方法 2 if (c+ = d+) / AVOID! (Java disallows) . 3 /应该写成 4 if (c+ = d+) != 0) . 5 1 d = (a = b + c) + r; / AVOID /应该写成 2 a = b + c; 3 d = a + r;1 if (booleanExpression) 2 return true;3 else 4 return false;5 应该代之以如下方法:6 return booleanExpression;1 if
12、 (condition) 2 return x;3 4 return y; 应该写做:5 return (condition ? x : y);1 JResponseObject RO = new JResponseObject();/获得一个可用连接,这个连接可能获取不到。2 JConnection conn = 3 (JConnection) JActiveDComDM.AbstractDataActiveFramework.InvokeObjectMethod(4 DBManagerObject, GetDBConnection, Param);/因此,这里要判断一下。5 if ( co
13、nn = null ) /如果是无效连接,要返回错误信息。6 RO.ErrorCode = -1;7 RO.ErrorString = 无法获得数据库连接;8 return RO;9 1 try 2 DBOFormService.3 SQLQuery(conn,msgInput.asStringValue(SqlText,),IE.getResultMessage(); /根据需要,返回对象类型的结果。4 RO.ResponseObject = IE;5 catch (Exception E) /发生异常时,要把异常信息返回。6 RO.ErrorCode = -1;7 RO.ErrorStri
14、ng = E.getMessage();8 9 finally /finally里执行连接的关闭操作。10 conn.close();11 12 return RO;1 Statement pSession = null;2 ResultSet pRS = null;3 pSession = conn. createStatement();4 String pSql = “SELECT * FROM LSCONF WHERE 1 = 1 ”;5 try6 pRS = pSession. executeQuery (pSql);7 if ( pRS.next()8 /这里是逻辑代码。用来取数。9
15、 10 11 catch(Exception E)12 /这里是一个异常,需要把异常信息想法通知调用者。可以使用throw 抛出。13 1 finally 2 try 3 /清理操作。 4 if ( pRS != null ) 5 pRS.close(); 6 7 if ( pSession != null ) 8 pSession.close(); 9 10 11 catch(Exception EE) 12 /这里可能也有异常,不需要抛出。 13 14 1 IAction mActStop = TAction.Create(STOP, 停止,S,this,onStop,SYS_STOP);
16、 2 JButton btStop = new JButton(“”); 3 btStop.setAction(mActStop); 4 public boolean onStop(IAction pAct,ActionEvent pEvt) 5 return true; /这里即是处理代码。 6 7 JMenuItem pMM = new JMenuItem(); 8 pMM.setAction(mActStop);1 public static String F(String pStringFormat,String P1)2 public static String F(String p
17、StringFormat,String P1,String P2)3 public static String F(String pStringFormat,String P1,String P2,String P3)4 public static String F(String pStringFormat,String P1,String P2,String P3,String P4)5 public static String F(String pStringFormat,String P1,String P2,String P3,String P4,String P5)6 String pData = TString.F(科目%s不存在!;,”1001”);普联软件(中国)有限公司2011年度入职培训,济南燕山学院PANSOFTJULY,2011编码规范(JAVA版)本次课程结束!Technology Team Progress