Spaces:
Running
Running
File size: 2,982 Bytes
0a98d2b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
Of course, object methods do not have to be just straightforward methods
for setting and observing attributes.
However, in general, methods do make some use of attributes;
the purpose of public operations on objects is to view and modify the
data content of the object.
Consider the class 'Notebook'.
Since notes are a list, it would be more convenient than a
direct setting method to provide a method for the client to
'add a single note to the notebook':
class Notebook {
private String owner;
private ArrayList<String> notes;
// CONSTRUCTOR
public Notebook(String owner) {
this.owner= owner;
// not from parameter, but initialize an empty list
this.notes= new ArrayList<>();
}
public String getOwner() {
return this.owner;
}
public void addNote(String note) {
this.notes.add(note);
}
// Returns all notes in one string
public String allNotes() {
// join method connects all elements of the list with
// the given separator
// kind of like the opposite of 'split' method
String nb = String.join("\n", notes);
return nb;
}
}
Now it's easier for the customer to use the notebook.
The actual storage format is encapsulated (i.e. hidden from the client).
This means that it doesn't matter if the internal implementation of
the class changes (for example, from a list to a table or a hash table)
as long as the public operations are preserved.
public class Testclass {
public static void main(String[] args) {
Notebook book = new Notebook("Mike Memorizer");
book.addNote("Go to the store!");
book.addNote("Cram for test!");
book.addNote("Watch the news!");
System.out.println(book.allNotes());
}
}
Program outputs:
Go to the store!
Cram for test!
Watch the news!
As another example, consider the class 'Cube'.
In addition to methods for setting and getting the length of a page,
the class could have methods for calculating the area and volume, for example:
class Cube {
private int sidelength;
// constructor
public Cube(int sidelength) {
this.sidelength = sidelength;
}
public int getSidelength() {
return sidelength;
}
public void setSidelength(int sidelength) {
this.sidelength= sidelength;
}
public int Area() {
return sidelength* sidelength* 6;
}
public int Volume() {
return sidelength * sidelength* sidelength;
}
}
Example on using the class:
public class TestClass {
public static void main(String[] args) {
Cube smallCube = new Cube(3);
System.out.println(smallCube.Area());
System.out.println(smallCube.Volume());
Cube largeCube = new Cube(15);
System.out.println(largeCube.Area());
System.out.println(largeCube.Volume());
}
}
Program outputs:
54
27
1350
3375
|