Java Reference
In-Depth Information
1 class Person
2 {
3 public Person( String n, int ag, String ad, String p )
4 { name = n; age = ag; address = ad; phone = p; }
5
6 public String toString( )
7 { return getName( ) + " " + getAge( ) + " "
8 + getPhoneNumber( ); }
9
10 public String getName( )
11 { return name; }
12
13 public int getAge( )
14 { return age; }
15
16 public String getAddress( )
17 { return address; }
18
19 public String getPhoneNumber( )
20 { return phone; }
21
22 public void setAddress( String newAddress )
23 { address = newAddress; }
24
25 public void setPhoneNumber( String newPhone )
26 { phone = newPhone; }
27
28 private String name;
29 private int age;
30 private String address;
31 private String phone;
32 }
figure 4.1
The Person class
stores name, age,
address, and phone
number.
Copy-and-paste is a weak design option, fraught with significant liabili-
ties. First, there is the problem that if you copy garbage, you wind up with
more garbage. This makes it very hard to fix programming errors that are
detected, especially when they are detected late.
Second is the related issue of maintenance and versioning. Suppose we
decide in the second version that it is better to store names in last name, first
name format, rather than as a single field. Or perhaps it is better to store
addresses using a special Address class. In order to maintain consistency, these
should be done for all classes. Using copy-and-paste, these design changes
have to be done in numerous places.
Third, and more subtle, is the fact that using copy-and-paste, Person ,
Student , and Employee are three separate entities with zero relationship between
Search WWH ::




Custom Search