* y9 N0 h$ o8 y# E' q0 C- O& ^/ I' J# k+ J
这里 java.io.* 使用来代替InputStream and OutputStream 的。 ) [/ I1 ?( q& l: Q, w4 t4 `" t! W A! i5 O- M/ v! y* b9 B9 W q* T5 X) A2 a$ w$ ^. K' I
Class 9 ?2 g- V1 [/ p接下来的是类的注释,一般是用来解释类的。 ; Y3 T ^, ^1 V0 x. F. {
5 |: W2 t2 l, a' f7 N/ @
/** : T2 h6 y4 `( e3 I* A class representing a set of packet and byte counters ' Y+ W% ~* T* T8 \9 l% b5 m, |
* It is observable to allow it to be watched, but only : y7 c2 }- @- y7 ~' X, E& S) c* reports changes when the current set is complete ' k7 S. P7 K) {$ C5 c; W4 d
*/ ' ?4 j9 A7 r- ^2 x9 h5 R
# O# R8 y+ R0 `, F
& b4 g0 s" v: V接下来是类定义,包含了在不同的行的 extends 和 implements ! r& R- c+ h) w5 s x0 g2 b; c. h$ v ( w% |/ B8 O5 D; ~1 upublic class CounterSet / U2 ^. F% I6 Bextends Observable 0 j2 O( M; `8 z8 y% Gimplements Cloneable 1 V9 f. c M8 x0 G; K " ^7 ?- S3 w/ q! l+ y4 b. l& \( P( I& N' s& h
. J/ j" [6 S6 L& t+ t x8 x/ y+ @Class Fields 6 c1 m0 n: I5 h7 K* @0 P
接下来是类的成员变量: % L' P$ X1 _4 K) d- Q7 H: u 8 e f! R: e8 ?. Y/** 0 T, r: N) W* d" o: M; P, e* Packet counters 9 D( K2 E. q, v0 I*/ % F. @& X) e4 T Uprotected int[] packets; 6 F2 |, s+ O( W
/ } A6 ^6 p4 `. X % \* z- O( n! Spublic 的成员变量必须生成文档(JavaDoc)。proceted、private和 package 定义的成员变量如果名字含义明确的话,可以没有注释。 ! E# P* I4 J; X n( s- Z& J- p+ \
* f7 {' Q2 f. L* u" C0 N# H/ D
( u6 L3 z5 J5 G. F& t" z/ n
存取方法 - w- J; ]" O! C, E5 J7 ?
接下来是类变量的存取的方法。它只是简单的用来将类的变量赋值获取值的话,可以简单的写在一行上。 0 B1 f' x" ^/ l$ h2 L
; K/ D3 I0 N! R9 M
/** ) a( v( @+ ^$ y7 J; U% A/ Y, M d
* Get the counters 0 T" V! m2 L& E G/ ?: p: {! O5 n
* @return an array containing the statistical data. This array has been $ d4 R' ^ Z2 N' Q" m- X
* freshly allocated and can be modified by the caller. ; b8 b: u, n$ Y
*/ / F1 i$ U4 v1 X$ ]3 h2 Z
public int[] getPackets() { return copyArray(packets, offset); } 9 G! X! L+ g2 i( E5 d
public int[] getBytes() { return copyArray(bytes, offset); } ( s5 }4 O$ o3 J! z1 J
3 l6 k: V3 U' l" c! }2 h. J: y
public int[] getPackets() { return packets; } ) d: i# B$ ?! ~; X# |public void setPackets(int[] packets) { this.packets = packets; } A9 \) ~2 z/ ~& K8 e其它的方法不要写在一行上 / f6 v+ |$ ^0 ~' w+ h6 V构造函数 0 A+ f* Z" G0 P- D' p接下来是构造函数,它应该用递增的方式写(比如:参数多的写在后面)。 $ ]: \; E1 X$ S c0 h* k7 i! N
访问类型 ("public", "private" 等.) 和 任何 "static", "final" 或 "synchronized" 应该在一行中,并且方法和参数另写一行,这样可以使方法和参数更易读。 & b, S$ ^& J9 N' p6 H* ?4 c; d& J3 E0 Y8 t: _/ X8 c* N
public ( a* P7 h8 _8 c2 J: f) O6 C' WCounterSet(int size){ * p/ K1 n( u; F( n
this.size = size; ' L$ D4 S) D( `
} v+ V ]) E5 c- [; g9 Q$ k * `& `/ J* ^$ X( P: N4 V. c6 C' i" r& D, x, T
! r# x1 g, [8 g+ c8 ^
克隆方法 - P4 d# j4 O0 a' Q, b5 e
如果这个类是可以被克隆的,那么下一步就是 clone 方法: " _) ?# p! Q5 d% Q2 f h4 Y) O" f2 b" V( N) l% q
public 8 g9 j5 a4 D/ M9 Q0 xObject clone() { 0 e$ ^; t& C) l' w1 F& Wtry { " |: B# y3 S. t/ X1 W; X( E
CounterSet obj = (CounterSet)super.clone(); s, z" F O, v% W, G+ ~
obj.packets = (int[])packets.clone(); 8 f, l3 Q9 r& w* d% V8 `
obj.size = size; ) e9 H* `3 L- t8 V" l: N @
return obj; . N6 G; f U5 B& b}catch(CloneNotSupportedException e) { 8 }: l9 M$ }* w8 U0 b
throw new InternalError("Unexpected CloneNotSUpportedException: " + e.getMessage()); ! `* i8 B; |, z. A
} % t4 i# I& l- M6 r
} : H. _. z) z6 q: K0 i
/ k, F( e3 q" D+ e: `$ l
; g/ k* O# |. [- ?% A- w
- a- r( s( E# \, D& C* H类方法 ! S" B) y, F4 b# i) u+ z下面开始写类的方法: ! q- w" X. ^8 j$ Q, J5 T, F: \1 y
% M8 p P/ g+ f$ B! ~; |6 ~( A6 D% W$ h/** ! S+ Z. Q2 }% M* i b3 v* f. ?* Set the packet counters L5 i+ D3 G! w" h4 E* (such as when restoring from a database) 2 S9 `8 o% S( g& ~. O# }
*/ ) I4 S; q# _5 \: \0 ~+ X
protected final , @9 t% ~4 F2 {/ n+ ]% rvoid setArray(int[] r1, int[] r2, int[] r3, int[] r4) 3 c2 d0 B# W. `, X, {throws IllegalArgumentException : U- q( i( T3 m9 z" {+ k" ]{ . x" W* U7 f8 Q7 s// ( C& v" F( s! @+ n2 _! b- q6 o5 t
// Ensure the arrays are of equal size 1 k+ I& b4 y+ Z9 P7 X
// + F: C4 y7 q! g& Fif (r1.length != r2.length || r1.length != r3.length || r1.length != r4.length) & L0 C6 g& f8 M5 F+ Q4 Rthrow new IllegalArgumentException("Arrays must be of the same size"); 6 |8 `, w4 X3 q9 A7 QSystem.arraycopy(r1, 0, r3, 0, r1.length); ! W2 s, b% X, \
System.arraycopy(r2, 0, r4, 0, r1.length); ) j$ {' a) _" ^} 5 A7 \" H' z& x9 }
|! K8 ]- h/ V. H( @