naming of the package
The package name should be made up of a lowercase word.
Name of Class
Class names must consist of words that begin with uppercase letters and other lowercase letters.
Name of Class variable
The name of the variable must begin with a lowercase letter. The following words start with capital letters.
Name of the Static Final variable
The name of the Static Final variable should be capitalized and indicate the full meaning.
Name of the parameter
The name of the parameter must be the same as the variable's naming convention.
- Naming the array
Arrays should always be named in the following way:
1 byte
Instead of:
1 byte buffer[];
- method
name with meaningful parameters, if possible, with the same names as the fields to be assigned:
1 Setcounter (int size) { 2 this . Size = size; 3 }
- Java file style
all Java (*.java) files must adhere to the following style rules
Copyright information must be at the beginning of the Java file, such as:
1 /** 2 * Copyright 0?3 Shanghai XXX Co., Ltd. 3 * all right reserved. 4 */
Other information that does not need to appear in Javadoc can also be included here.
- package/imports
package line to the import line, the standard package name in import is preceded by the local package name, and is sorted alphabetically.
If the import row contains different subdirectories in the same package, it should be handled with *.
1 package hotlava.net.stats; 2 import java.io.* 3 import java.util.Observable; 4 import hotlava.util.Application;
here java.io.* used instead of InputStream and OutputStream.
- Class
the next is the comment of the class, which is usually used to interpret the class.
1 /** 2 * A class representing a set of packet and byte counters 3 * It is observable-allow it to be watched 4 * Reports changes when the current set was complete 5 */
Next is the class definition, which contains the extends and implements of the different rows .
1 Public class CounterSet 2 extends Observable 3 Implements cloneable 4
Next is the member variable for the class:
1 /** 2 * Packet Counters 3 */ 4 protected int
the member variable for public must generate a document (JAVADOC). Member variables defined by the proceted, private, and package can have no comment if the name has a clear meaning.
- access Method
Next is the method of accessing the class variable. It is simply used to get the value of the class variable assignment, which can be simply written on one line.
1 /**2 * Get the counters3 *@returnAn array containing the statistical data. This array has been4 * Freshly allocated and can is modified by the caller.5 */6 Public int[] Getpackets () {returnCopyarray (packets, offset);}7 Public int[] GetBytes () {returncopyarray (bytes, offset);}8 Public int[] Getpackets () {returnpackets;}9 Public voidSetpackets (int[] packets) { This. packets = Packets; }
Other methods do not write on a line
- constructor function
Next is the constructor, which should be written in an incremental way (for example: many of the arguments are written in the back).
The type of access ("public", "private", etc.) and any "static", "final" or "synchronized" should be on one line, and the methods and parameters are written in another line, which makes the methods and parameters easier to read.
1 Public 2 counterset (int size) {3this. size = size; 4 }
- Cloning Method
if this class can be cloned, then the next step is the Clone method:
1 Public2 Object Clone () {3 Try {4CounterSet obj = (counterset)Super. Clone ();5Obj.packets = (int[]) Packets.clone ();6Obj.size =size;7 returnobj;8}Catch(clonenotsupportedexception e) {9 Throw NewInternalerror ("Unexpected clonenotsupportedexception:" +e.getmessage ());Ten } One}
- class Method
here's how to start writing classes:
1 /**2 * Set the packet counters3 * (such as when restoring from a database)4 */5 protected Final6 voidSetArray (int[] R1,int[] R2,int[] R3,int[] R4)7 throwsillegalargumentexception8 {9 //ensure the arrays www.gzlij.com is of equal sizeTen if(r1.length! = R2.length | | r1.length! = R3.LENGTH | | R1.length! =r4.length) One Throw Newillegalargumentexception ("Arrays must be of the the same size"); ASystem.arraycopy (R1, 0, R3, 0, r1.length); -System.arraycopy (R2, 0, R4, 0, r1.length); -}
- ToString Method
in any case, each class should define the ToString method:
1 Public2 String toString () {3String retval ="CounterSet:";4 for(inti = 0; I < data.length (); i++) {5retval + =data.bytes.toString ();6retval + =data.packets.toString ();7 }8 returnretval;9 }Ten}
- Main method
If the main (string[]) method is already defined, it should be written at the bottom of the class.