初识Java面向对象

          最近在学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 <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();
	}
}


 

 

持续更新中!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值