最近在学Java,本来C++中的面向对象技术就没掌握的很好,于是非常认真的准备学,想补补OO。想下面是我近期的基于类与对象的小程序。在此罗列出来,供交流学习,望各位指正。
在Java中创建对象都得用关键字new,然后调用构造函数。Java中的“=”连接对象是申明前者是后者的引用,即共用地址(实质是同一对象的不同名称而已)。
class Date {
int day, month, year;
Date(int D, int M, int Y) {
day = D;
month = M;
year = Y;
}
void Copy(Date date) {
this.day = date.day;
this.day = date.month;
this.day = date.year;
}
}
class Product {
Date When;
String Name;
double Price;
Product(Date date, String name, double price) {
this.When = date;//引用
this.Name = name;
this.Price = price;
}
}
public class Object1 {
static Product[] ArrayProduct= new Product[10];
static boolean IsInList(String TmpName) {
for (int i = 0; i < ArrayProduct.length; i++) {
if (0 == TmpName.compareTo(ArrayProduct[i].Name))
return true;
}
return false;
}
static int GetNumEterval(double low, double high) {
int count = 0;
for (int i = 0; i < ArrayProduct.length; i++) {
if (ArrayProduct[i].Price >= low && ArrayProduct[i].Price <= high)
count++;
}
return count;
}
public static void main(String[] str) {
String[] Name = new String[10];
Name[0] = "Apple";
Name[1] = "Pear";
Name[2] = "Banana";
Name[3] = "oringe";
Name[4] = "Rice";
Name[5] = "Noddle";
Name[6] = "Basketball";
Name[7] = "football";
Name[8] = "vollyball";
Name[9] = "tennis";
for (int i = 0; i < 10; i++) {
Date tmp = new Date((int) (31 * Math.random() % 32),
(int) (31 * Math.random() % 32), (int) (10 * Math.random()));
ArrayProduct[i] = new Product(tmp, Name[i], 10 * Math.random());
}
if (IsInList("Basketball")) {
System.out.println("Basketball is in the set!");
} else
System.out.println("Basketball is not in the set!");
System.out.println("The number of price between 3.0 and 7,0 is "
+ GetNumEterval(3, 7));
}
}
下面将介绍Java中的静态成员(静态成员函数+静态成员变量)。在类定义时,使用关键字static可将成员定义为静态成员。特征是不管这个类创建了多少个对象,其静态成员只有一个副本,这个副本被所有的对象共享。静态数据成员必须进行初始化。在程序一开始运行时静态数据成员就必须存在,因为在程序运行中要调用,所以静态数据成员不能在任何函数内分配空间和初始化。最好在类的实现部分中完成静态数据成员的初始化。两种访问:类名.静态成员;对象名 . 静态成员。
class List {
String container;
List next;
List(String ele, List tail) {
container = ele;
next = tail;
}
// 判断ele是否属于list
static boolean BelongTo(String ele, List list) {
while (list != null) {
if (list.container == ele)
return true;
list = list.next;
}
return false;
}
// 顺序显示链表中的内容
static void Display(List list) {
while (list != null) {
System.out.print(list.container + " ");
list = list.next;
}
System.out.println();
}
// 获取链表的长度
static int GetLength(List list) {
int ret = 0;
while (list != null) {
ret++;
list = list.next;
}
return ret;
}
// 插入元素
static List Insert(String ele, List list) {
return new List(ele, list);
}
// 删除某结点
static List Delete(String ele, List list) {
if (list == null)
return null;
if (list.container == ele)
return list.next;
List cur, pre;
pre = list;
cur = list.next;
while (cur != null && cur.container != ele) {
pre = cur;
cur = cur.next;
}
if (cur != null)
pre.next = cur.next;
return list;
}
// 复制产生新链表,此处倒置了
static List CopyAllListREV(List old) {
List New = null;
while (old != null) {
New = new List(old.container, New);
old = old.next;
}
return New;
}
// 复制产生新链表,此处未倒置了
static List CopyAllList(List old) {
if(old == null)
return old;
return new List(old.container, CopyAllList(old.next));
}
}
public class JavaList {
public static void main(String[] str) {
List MyList = new List("徐进", null);
MyList = new List("潘传军", MyList);
MyList = new List("叶温情", MyList);
System.out.println("下面是原链表的内容:");
List.Display(MyList);
System.out.println("综上链表的长度是" + List.GetLength(MyList));
System.out.println();
MyList = List.Insert("张伦干", MyList);
System.out.println("下面是插入后链表的内容:");
List.Display(MyList);
System.out.println("综上链表的长度是" + List.GetLength(MyList));
System.out.println();
MyList = List.Delete("张伦干", MyList);
System.out.println("下面是删除后链表的内容:");
List.Display(MyList);
System.out.println("综上链表的长度是" + List.GetLength(MyList));
System.out.println();
MyList=MyList.Insert("张伦干", MyList);
System.out.println("下面是插入后链表的内容:");
List.Display(MyList);
System.out.println("综上链表的长度是" + List.GetLength(MyList));
System.out.println();
List NewListREV = List.CopyAllListREV(MyList);
System.out.println("下面是复制后新链表的内容(倒置):");
List.Display(NewListREV);
System.out.println("综上链表的长度是" + List.GetLength(NewListREV));
System.out.println();
List NewList = List.CopyAllList(MyList);
System.out.println("下面是复制后新链表的内容(未倒置):");
List.Display(NewList);
System.out.println("综上链表的长度是" + List.GetLength(NewList));
System.out.println();
}
}
一下是链表的排序算法。注意与C/C++中用指针或带头结点的链表的区别。函数sortRec(List2 list)采用的是二路归并的思想。
class List2 {
int container;
List2 next;
// 构造函数
List2(int ele, List2 tail) {
container = ele;
next = tail;
}
// 插入元素
List2 Insert(int ele) {
return new List2(ele, this);
}
//获取链表的长度
int Length(List2 list)
{
int len=0;
while(list!=null)
{
len++;
list=list.next;
}
return len;
}
//显示链表
void Display() {
List2 cur = this;
while (cur != null) {
System.out.print(cur.container + "---->");
cur = cur.next;
}
System.out.println("null");
}
//递归合并有序链表
static List2 Merge1(List2 u, List2 v) {
if (v == null)
return u;
if (u == null)
return v;
if (u.container < v.container) {
u.next = Merge1(u.next, v);
return u;
} else {
v.next = Merge1(v.next, u);
return v;
}
}
//非递归合并有序链表
static List2 Merge2(List2 u, List2 v) {
List2 tmp, New = null;
while (u != null && v != null) {
if (u.container
持续更新中!