Java programming specification

xiaoxiao2021-03-06  99

Defining the purpose of this specification is to let all documents in the project look like a person written, increase readability, and reduce the loss caused by the project group. (These norms are not necessarily abolished, but must make the program good readability)?

Down? Named?

The name of the package should be composed of a lowercase word. ?

Class? Named?

The name of the Class? Must be constructed from the word of uppercase letters and other letters lower-write words?

Class? Named of variables?

The name of the variable must be started with a lowercase letter. The latter words begin with uppercase letters. ?

Static? Final? Named of variables?

The name of the Static? Final® variable should be capitalized and point out the full meaning. ?

Named of parameters?

The name of the parameter must be consistent with the naming specification of the variable. ?

Array name?

The array should always be named in the following way:?

BYTE []? buffer;

Instead:?

BYTE? BUFFER [];

How the parameter of the method?

Name the meaningful parameter, if possible, use the name of the field to assign the value:?

SetCounter (int? Size) {

THIS.SIZE? =? size;

}

Java? File style?

All? Java (*. Java) • File must comply with the following style rules?

Copyright Information?

The copyright information must be in the beginning of the java? Such as:?

/ **

*? Copyright ??? 2000? Shanghai? Xxx? Co.? Ltd.

*? All? Right? Reserved.

* /

Others don't need to appear in? Javadoc® information can also be included here.

Package / Imports?

Package? Before you are in the import?, the standard package name in the Import® is before you have a local package name and arrange them in alphabetical order. If the IMPORT® line contains different subdirectory in the same package, should you be handled? *? ?

Package? hotlava.net.stats;

IMPORT? Java.io. *;

Import? java.util.observable;

Import? Hotlava.util.Application;

Here? Java.io. *? Use instead of InputStream? And? OutputStream?

Class?

Next, the annotation of the class is generally used to interpret the class. ?

/ **

*? A? Class? Representing? A? Set? Of? Packet? And? Byte? Counters

*? It? Is? Observable? To? Allow? It? To? Be? Watch,? But? Only

*? Reports? Changes? When? the? current? set? is? completion

* /

Next is a class definition, containing the extenss? And? Imports?

PUBLIC? CLAS? Counterset

EXTENDS? OBSERVABLE

IMPLEMENTS? CLONEABLE

Class? Fields?

Next is a member variable of class:?

/ **

*? Packet? Counters

* /

Protected? int []? packets;

The member variable of the public® must generate a document (javadoc). PROCETED, PRIVATE and PACKAGE? The defined member variable can be not commented if the name meaning is clear.

Access method?

Next is a method of access to class variables. It is just simple to get values ​​to assign values ​​to the class variable, you can simply write on one line. ?

/ **

*? Get? The? Counter

*?@return means?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 ;?}

Other methods don't write on a line

Constructor?

Next is a constructor, it should be written in an incremental manner (such as: writing more). ?

Access type? ("Public",? "Private"?, Etc.)?)? Method and parameters can be read more easily. ?

public

Counterset (int? Size) {

THIS.SIZE? =? size;

}

Cloning method?

If this class can be cloned, then the next step is? Clone? Method:?

public

Object? Clone ()? {

Try? {

Counterset? Obj? =? (Counterset) super.clone ();

Obj.packets? =? (int []) packets.clone ();

Obj.size? =? size;

Return? OBJ;

} Catch (ClonyNotsupportedException? E)? {

Throw? new? INTERNALROR ("UNEXPECTED? ClonyNotSupportedException:?"? ? E.getMessage ());

}

}

Class method?

The method of writing classes below:?

/ **

*? Set? The? Packet? Counters

*? (Such? as? when? restaurantoring? 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?! =? 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);

}

TOSTRING? Method?

In any case, every class should be defined? TOSTRING? Method:?

public

String? TOSTRING ()? {

String? RetVal? =? "" Counterset :? ";

FOR? (int? i? =? 0;? i?

RETVAL? =? Data.bytes.toString ();

RETVAL? =? Data.packets.tostring ();

}

RETURN? RETVAL;

}

}

Main? method?

If main (String [])? The method is already defined, then it should be written at the bottom of the class.?

Code write format?

Code style?

The code should be used in the format of the UNIX? Instead of? Windows? (Such as: Enter the carriage return Wrap)?

Documentation?

Do you have to generate a document for classes with? Javadoc?. Not only because it is standard, this is also a way to be approved by various? Java? Compiler. Use? @Author? The tag is not recommended because the code should not be owned by the individual. ?

indentation?

The indentation should be 2 spaces per line. Don't save the Tab character in the source file. • The Tab character will extend to different widths because the user settings are different when using different source code management tools.?

If you use? Ultredit? As your Java? Source code editor, you can ban the saving Tab characters by doing this? The method is set to the ULTREDIT? Tab? The length of the length of the length of 2 is used. Then use? Format | tabs? To? Spaces? Menu will be used to convert to spaces. ?

Page width?

Page width should be set to 80 characters. The source code will generally not exceed this width, which can not be complete display, but this setting can also be flexible.? In any case, the long statement should be in a comma Or after an operator (after a statement fold, should it be more than 2 characters more than the original statement.?

{}?Correct?

The statement in {} should be separate as a line.? For example, the first line below is wrong, the second line is correct:?

IF? (i> 0)? {? i? ?};? //? error,? {? and?}? Is it in the same line?

IF? (i> 0)? {?

I? ?

};? //? Correct,? {? Alone as a line?

}? The statement is always a line alone.?

If?}? Statement should be indented to the position corresponding to it? {? The row is relatively.

brackets?

There should be a space between the left bracket and the latter characters. Similarly, the right brackets and the previous characters should not have spaces. The following example shows the incorrect of parentheses and spaces and correct use:?

CallProc (? APARETER?);? //? Error?

CallProc (APARETER);? //? Correct?

Don't use the meaningless parentheses in the statement. The parentheses should only appear in the source code for a certain purpose. The following example illustrates errors and correct usage:?

IF? ((i)? =? 42)? {? //? error? -? Brand meaningless?

IF? (i? ==? 42)? OR? (j? ==? 42)? Then? //? correct? -? Indeed in parentheses?

Program writing specifications?

exit ()?

EXIT? In addition to? Main? can be called, other places should not be called. Because this does not give any code code opportunities to intercept exit. A similar background service ground program should not be exited because a library module determines to exit. ?

abnormal?

The declared error should throw a runtimeException or derived exception. ?

The top () function of the top layer should intercept all exceptions and print (or record it in the log) on ​​the screen. ?

Garbage collection?

Java uses mature background garbage collection techniques instead of reference count. But this will result in a problem: You must work after the instance of the object is used. For example, a PREL programmer may write this:?

...

{

FileOutputStream? FOS? =? New? Fileoutputstream (projectfile);

Project.save (FOS,? "IDE? PROJECT? FILE");?

}

...

Unless the output stream is turned off, it is not a reference to the program language, such as Java, is a clearing work of the variable. Must be written as below:?

FileOutputStream? FOS? =? New? Fileoutputstream (projectfile);

Project.save (FOS,? "IDE? PROJECT? FILE");?

fos.close (); clone?

Here is a useful way:?

IMPLEMENTS? Cloneable

public

Object? Clone ()

{

Try? {

THISCLASS? OBJ? =? (Thisclass) super.clone ();

Obj.field1? =? (int []) Field1.clone ();

Obj.field2? =? field2;

Return? OBJ;

}? Catch (ClonyNotsupportedException? e)? {

Throw? new? INTERNALROR ("UNEXPECTED? ClonyNotSupportedException:?"? ? E.getMessage ());

}

}

Final?

Never define class (unless the program frame requirements) because of performance reasons?

If a class is not ready to be inherited, it is best to indicate in class documents, not to define her as? Final? This is because no one can guarantee that it will not be inherited because of what reason. ?

Access the member variables of the class?

Most of the class member variables should be defined as? Protected to prevent inheritance classes from using them. ?

Note, use "int []? Packets" instead of "int? Packets []", the latter is never used. ?

PUBLIC? VOID? SETPACKETS (int []? packets)? {? this.packets? =? packets ;?}

Counterset (int? Size)

{

THIS.SIZE? =? size;

}

转载请注明原文地址:https://www.9cbs.com/read-123695.html

New Post(0)