第一个例子:我们把数据存储结构,也就是数组从程序的其他部分中分离出来,程序的其他部分将成为这个结构的用户。我们把程序分成两个class,:
一个class用来封装数组(LowArray),外界不能直接访问这个数组,但是该封装提供了供外界访问的方法setElem()和getElem(),该方法是外界应用和该数组之间通信的方式。
另一个class创建了一个LowArray的对象,用这个对象来操作数组中的数据。
container class - 像LowArray这样用来封装数据对象的类称为容器类。一般容器类不仅提供数据的存储,还提供访问该数据的方法,也许还会封装更复杂的操作。
class interface - 类的接口是用户访问该类的方法。由于class fields一般都是private的,所以我们说的接口一般指的都是类的方法。
abstrction - The process of seperating the how from the what - how an operation is performed inside a class, as oppose to what's visible to the class user.
第二个例子:我们进一步改进数组这个数据存储结构使其更好的面向对象(HighArray)。class用户不用再考虑数组的index, 数组的封装不再使用getElem()和setElem()这样的方法,取而代之的是insert(), find() 和delete(). 这使得class用户可以把注意力放在what而不是how, 也就是说用户只用考虑要插入/删除/查找什么,而不用担心怎么去插入/删除/查找。
class LowArray {
private long[] a;
// constructor
public LowArray(int size) {
a = new long[size];
}
// set value
public void setElem(int index, long value) {
a[index] = value;
}
//get value
public long getElem(int index) {
return a[index];
}
} // end class LowArray
class LowArrayApp {
public static void main(String[] args) {
LowArray arr;
arr = new LowArray(100);
int nElems = 0;
int j;
// insert 10 items
arr.setElem(0, 77);
arr.setElem(1, 99);
arr.setElem(2, 44);
arr.setElem(3, 55);
arr.setElem(4, 22);
arr.setElem(5, 88);
arr.setElem(6, 11);
arr.setElem(7, 00);
arr.setElem(8, 66);
arr.setElem(9, 33);
nElems = 10;
// display items
for(j=0; j<nElems;j++) {
System.out.print(arr.getElem(j) + " ");
}
System.out.println("");
// search for data item 26
int searchKey=26;
for(j=0; j<nElems; j++) {
if(arr.getElem(j) == searchKey) {
break;
}
}
if(j == nElems) {
System.out.println("Cannot find " + searchKey);
}
else {
System.out.println("Found " + searchKey);
}
// delete value 55
for(j=0; j<nElems;j++) {
if(arr.getElem(j) == 55) {
break;
}
}
for(int k=j; k<nElems-1; k++) {
arr.setElem(k, arr.getElem(k+1));
}
nElems--;
// display items
for(j=0; j<nElems; j++) {
System.out.print(arr.getElem(j) + " ");
}
System.out.println("");
} // end main()
} // end class LowArrayApp
==========================================================================================================================
class HighArray {
private long[] a;
private int nElems;
// constructor
public HighArray(int max) {
a = new long[max];
nElems = 0;
}
// find specified value
public boolean find(long searchKey) {
int j;
for(j=0; j<nElems; j++) {
if(a[j] == searchKey) {
break;
}
}
if(j == nElems) {
return false;
}
else {
return true;
}
} // end find()
// put element into array
public void insert(long value) {
a[nElems] = value;
nElems++;
}
// delete specified value
public boolean delete(long value) {
int j;
for(j=0; j<nElems; j++) { // look for it
if(value == a[j]) {
break;
}
}
if(j == nElems) {
return false; // can't find it
}
else {
for(int k=j; k<nElems; k++) {
a[k] = a[k+1]; // move higher ones down
}
nElems--;
return true;
}
} // end delete()
// display items
public void display() {
for(int j=0; j<nElems; j++) {
System.out.print(a[j] + " ");
}
System.out.println("");
} // end display()
} // end class HighArray
class HighArrayApp {
public static void main(String[] args) {
int maxSize = 100;
HighArray arr;
arr = new HighArray(maxSize);
// insert 10 items
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
// display items
arr.display();
// search for item 35
int searchKey = 35;
if(arr.find(searchKey)) {
System.out.println("Found " + searchKey);
}
else {
System.out.println("Can't find " + searchKey);
}
// delete 3 items
arr.delete(00);
arr.delete(55);
arr.delete(99);
// display items
arr.display();
} // end main()
} // end class HighArrayApp