Java Reference
In-Depth Information
1 // Minimal Date class that illustrates some Java features
2 // No error checks or javadoc comments
3
4 public class Date
5 {
6 // Zero-parameter constructor
7 public Date( )
8 {
9 month = 1;
10 day = 1;
11 year = 2010;
12 }
13
14 // Three-parameter constructor
15 public Date( int theMonth, int theDay, int theYear )
16 {
17 month = theMonth;
18 day = theDay;
19 year = theYear;
20 }
21
22 // Return true if two equal values
23 public boolean equals( Object rhs )
24 {
25 if( ! ( rhs instanceof Date ) )
26 return false;
27 Date rhDate = ( Date ) rhs;
28 return rhDate.month == month && rhDate.day == day &&
29 rhDate.year == year;
30 }
31
32 // Conversion to String
33 public String toString( )
34 {
35 return month + "/" + day + "/" + year;
36 }
37
38 // Fields
39 private int month;
40 private int day;
41 private int year;
42 }
figure 3.6
A minimal Date class
that illustrates
constructors and the
equals and toString
methods
an accessor . A method that changes the state is a mutator (because it
mutates the state of the object).
Special cases of accessors and mutators examine only a single field.
These accessors typically have names beginning with get , such as getMonth ,
while these mutators typically have names beginning with set , such as
setMonth .
Search WWH ::




Custom Search