JAVA学习—使用具有通用性的队列—2021-06-11
使用一个通用性得队列来写不同的类
那什么是类?
一直写的 public class 就是一个类。
类就是图纸,根据图纸来创造手机,根据类创造对象
类的定义
类包含两部分:成员变量和成员方法
1.成员变量,将变量位置直接定义在类中,在方法外,即为成员变量。
2.成员方法,将普通的方法去掉 static 关键字,即为成员方法。
列子
以学生为一个列子,那么学生的属性有年龄和姓名(只列举两项)。行为可以有吃饭和睡觉。对应起来
成员变量就是:String name;//姓名 int age;//年龄
成员方法就是:public void eat(){...} public void sleep(){...}
package a21;
/**
*
* @author hengyuzuo
*
*/
public class StudentDemo {
//成员变量
String name;
int age;
//成员方法
public void eat() {
System.out.println("干饭!");
}// Of eat
public void sleep() {
System.out.println("睡觉!");
}// Of sleep
}// Of StudentDome
类无法使用,要想使用,则根据类来创建一个对象
写法:类名称 对象名 = new 类名称();
package a21;
/**
*
* @author hengyuzuo
*
*/
public class StudentDemo1 {
public static void main(String[] args) {
StudentDemo stu1 = new StudentDemo();//创建了一个对象
StudentDemo stu2 = new StudentDemo();//创建了第二个对象
}// Of main
}// Of class StudentDemo1
如何使用对象
类包含成员变量和成员方法,类所创建的对象也有这两个部分。
如果对成员变量没有赋值,会有一个默认值
用法:对象名.成员变量名
对象名.成员方法名(参数);
参数看需要写
package a21;
/**
*
* @author hengyuzuo
*
*/
public class StudentDemo3 {
public static void mian(String[] args) {
// TODO Auto-generated method stub
StudentDemo stu = new StudentDemo();//创建一个学生对象
//改变成员变量的数值
stu.name = "金龙";
stu.age = 24;
System.out.println(stu.name);
System.out.println(stu.age);
//使用对象当中的成员方法
stu.eat();//调用吃饭的成员方法
stu.sleep();//调用睡觉的成员方法
}// Of main
}// Of class StudentDemo3
言归正传,对于昨天代码使用具有通用性的队列
昨天使用的队列有两种: 存储二叉树节点的队列; 存储整数的队列. 这样的话, 难道我们要为每种类型单独写一个队列? 这样显然没有充分利用代码的复用性. 实际上, 我们只需要一个存储对象的队列就够啦!
Java 里面, 所有的类均为 Object 类的 (直接或间接) 子类. 如果不写就默认为直接子类. 例如
public class CircleObjectQueue;
等价于
public class CircleObjectQueue extends Object;
存储对象的队列, 实际上是存储对象的地址 (引用、指针). 因此, 可以存储任何类的对象 (的引用).
可以通过强制类型转换将对象转成其本身的类别. 例如前面程序
tempTree = (BinaryCharTree) tempQueue.dequeue();
括号中的类型即表示强制类型转换.
Java 本身将 int, double, char 分别封装到 Integer, Double, Char 类.
今天的代码量非常少, 但涉及面向对象的思想不少.
让我们见识一下它的威力吧.
————————————————
learn form @minfanph 原文
/**
********************
* Convert the tree to data arrays, including a char array and an int array.
* The results are stored in two member variables.
*
* @see #valuesArray
* @see #indicesArray
*********************
*/
public void toDataArraysObjectQueue() {
// Initialize arrays.
int tempLength = getNumNodes();
valuesArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
// Traverse and convert at the same time.
CircleObjectQueue tempQueue = new CircleObjectQueue();
tempQueue.enqueue(this);
CircleObjectQueue tempIntQueue = new CircleObjectQueue();
Integer tempIndexInteger = new Integer(0);
tempIntQueue.enqueue(tempIndexInteger);
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
int tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
System.out.println("tempIndex = " + tempIndex);
while (tempTree != null) {
valuesArray[i] = tempTree.value;
indicesArray[i] = tempIndex;
i++;
if (tempTree.leftChild != null) {
tempQueue.enqueue(tempTree.leftChild);
tempIntQueue.enqueue(new Integer(tempIndex * 2 + 1));
} // Of if
if (tempTree.rightChild != null) {
tempQueue.enqueue(tempTree.rightChild);
tempIntQueue.enqueue(new Integer(tempIndex * 2 + 2));
} // Of if
tempTree = (BinaryCharTree) tempQueue.dequeue();
if (tempTree == null) {
break;
}//Of if
tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
} // Of while
}// Of toDataArraysObjectQueue
/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
public static void main(String args[]) {
BinaryCharTree tempTree = manualConstructTree();
System.out.println("\r\nPreorder visit:");
tempTree.preOrderVisit();
System.out.println("\r\nIn-order visit:");
tempTree.inOrderVisit();
System.out.println("\r\nPost-order visit:");
tempTree.postOrderVisit();
System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
System.out.println("The number of nodes is: " + tempTree.getNumNodes());
tempTree.toDataArrays();
System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
tempTree.toDataArraysObjectQueue();
System.out.println("Only object queue.");
System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
}// Of main