初识Java面向对象
2013-12-05 23:22
190 查看
最近在学Java,本来C++中的面向对象技术就没掌握的很好,于是非常认真的准备学,想补补OO。想下面是我近期的基于类与对象的小程序。在此罗列出来,供交流学习,望各位指正。
在Java中创建对象都得用关键字new,然后调用构造函数。Java中的“=”连接对象是申明前者是后者的引用,即共用地址(实质是同一对象的不同名称而已)。
下面将介绍Java中的静态成员(静态成员函数+静态成员变量)。在类定义时,使用关键字static可将成员定义为静态成员。特征是不管这个类创建了多少个对象,其静态成员只有一个副本,这个副本被所有的对象共享。静态数据成员必须进行初始化。在程序一开始运行时静态数据成员就必须存在,因为在程序运行中要调用,所以静态数据成员不能在任何函数内分配空间和初始化。最好在类的实现部分中完成静态数据成员的初始化。两种访问:类名.静态成员;对象名 . 静态成员。
一下是链表的排序算法。注意与C/C++中用指针或带头结点的链表的区别。函数sortRec(List2 list)采用的是二路归并的思想。
持续更新中!
在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 <v.container) { tmp = u.next; u.next = New; New = u; u = tmp; } else { tmp = v.next; v.next = New; New = v; v = tmp; } } while (v!=null) { tmp = v.next; v.next = New; New = v; v = tmp; } while (u!=null) { tmp = u.next; u.next = New; New = u; u = tmp; } return New; } //链表的归并排序 static List2 sortRec(List2 list) { int len=list.Length(list); if(len<=1)return list; List2 pre,cur; pre=list; cur=list; int i=0,tmp=len/2; while(i<tmp) { i++; pre=cur; cur=cur.next; } pre.next=null; return Merge1(sortRec(list), sortRec(cur)); } } public class JavaList2 { public static void main(String[] str) { List2 u = new List2(8, null); u = u.Insert(6); u = u.Insert(3); u.Display(); List2 v = new List2(9, null); v = v.Insert(7); v = v.Insert(5); v = v.Insert(4); v = v.Insert(2); v.Display(); List2 uv = u.Merge1(u, v);// uv=v.Merge(u,v);,uv=uv.Merge(u,v);uv=List2 // // .Merge(u,v); uv.Display(); //List2 uv = u.Merge2(u, v);// uv=v.Merge(u,v);,uv=uv.Merge(u,v);uv=List2 //// .Merge(u,v); // uv.Display(); uv=uv.sortRec(uv); uv.Display(); } }
持续更新中!
相关文章推荐
- Java基础(五)OOP面向对象之(一) 面向对象思想初识
- 初识JAVA(【面向对象】:pub/fri/pro/pri、封装/继承/多态、接口/抽象类、静态方法和抽象方法;泛型、垃圾回收机制、反射和RTTI)
- 初识Java面向对象的特性
- 一起学java【7】---结构化初识,面向对象
- java笔记——初识面向对象03一个文件调用另一个文件
- java面向对象——继承--初识继承
- java笔记——初识面向对象04继承
- java笔记——初识面向对象05接口
- java学习(二)java基本语法 初识java面向对象
- java笔记—初识面向对象01
- java笔记——初识面向对象(主函数)
- Java初识面向对象
- Java中面向对象三大特征总结
- Java的Polymorphism多态-笔记(面向对象3-多态)
- Java面向对象-代码块
- Java中面向对象三大特征总结
- 黑马程序员---java学习笔记之面向对象:接口
- 黑马程序员--Java基础学习笔记【二维数组、面向对象】
- Java 面向对象三大特征之一:继承
- Java面向对象-构造方法,this关键字