Coding specification for Java programs (3)

xiaoxiao2021-03-06  48

This article is selected from: IBM DW China ● Constructor Next is the constructor, it should be written in an incremental manner (such as: writing more written). Access Type ("Public", "Private", etc.) and any "static", "firm" or "synchronized" should be written in one line, and the methods and parameters can be written separately, which makes the method and parameters easier to read. Publiccounterset (int size) {this.size = size;} ● Cloning method If this class can be cloned, then the next step is Clone method: publicobject clone () {try {counter = (counterset) Super.clone () ; obj.packets = (int []) packets.clone (); obj.size = size; return obj;} catch (CloneNotSupportedException e) {throw new InternalError ( "Unexpected CloneNotSUpportedException:" e.getMessage ());} } ● Method: / ** * set the packet counter * (Such as When Restoring from a database) * / protected finalvoid setArray (int [] R1, int [] R2, int [] R3, INT [] R4) THROWS ILLEGALARGUMENTEXCEPTION {/// EnSure the arrays are of equal size //iff (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) } ●}} The method should define the Tostring method in each class: publicString toString () {string retval = "counterset:"; for (int i = 0; i

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

New Post(0)