java中回調函數以及關於包裝類的Demo

java冒泡排序

package com.wwp.test;
//冒泡排序法
public class MaoPao {

	/**
	 *@author wuwp
	 * @param args
	 */
	public static void main(String[] args) {
		// 從小到大排列
		int a[]={5,1,7,9,4,2,-1};
		sort(a);
		
		for(int k=0;k<a.length;k++){
			System.out.println(a[k]);
		}
		}

	public  static void  sort(int[] a){
		for(int j=0;j<a.length-1;j++){
			for(int i=j;i<a.length-1;i++){
				if(a[j]> a[i+1]){
					int tep=a[j];
					a[j]=a[i+1];
					a[i+1]=tep;
					
				}
				
			}
		}
		//return a;
	}
}



包裝類範圍的相等問題


以Short類和Intger類爲例,其在-128到127範圍內定義的兩個變量是相等的,而超出此範圍圍後不相等(==)。研究源碼發現,在-128到127之間進行了緩存,代碼如下:

 /***z
 *這裏定義了一個-128到127的Short數組,用於緩存。
 */
 private static class ShortCache {
        private ShortCache(){}

        static final Short cache[] = new Short[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Short((short)(i - 128));
        }
    }
    
    
    /*******
    *Short s1=100;Short s2=100;Short s3=200; Short s4=200
    *當我們定義了Short s1=100後。實際進行了自動裝箱,即下面這個函數,可以看到如果不在此範圍內。其
    *返回的是一個新的對象。而在此範圍內返回的是緩存數組裏的元素,當我們比較時s1==s2時,
    *其指向的是同一對象(true),而s3==s4則是實例化了兩個新的對象,使用雙等號時就不是同一對象了(false)
    *但如果用equals方法是相等的,因爲它比較的是兩個對象的內容。Intger類也是如此。
    *******/
    public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        if (sAsInt >= -128 && sAsInt <= 127) { // must cache
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }


在學習javascript中回調函數很好理解。即函數執行完後再回過頭來執行給定參數的函數,叫做回調函數.例如在下面的代碼中:

<html>
<head>
<script>
function a(callback){
    alert("我是parent函數a!");
    alert("開始執行");
    aert("執行完畢");
    alert("調用回調函數");
    callback();
}
function b(){
alert("我是回調函數b");
 }
function c(){
alert("我是回調函數c");
 }
 function test(){
     a(b);//a執行完後回調b
     a(c);//a執行完後回調c
 }
</script>
<body>
<h1>學習js回調函數</h1>
<button onClick="test()">點擊</button>
<p>應該能看到調用了兩個回調函數</p>
</body>
</html>

java回調(轉載)

class test  
{
 
 public static void testTime(Callback call){
     
     long beg=System.currentTimeMillis();//開始測試時間
     call.excute();
     long end =System.currentTimeMillis();
     System.out.println("UseTime"+(end-beg));
 }
 public static void main(String []args){
     testTime(new Callback(){public void excute(){TestObject.TestMethod();}});
     
 }
 
    
}


interface Callback{
    
    public void excute();
}
//Demo2
package com.wwp;

public class CallBackStudy {

	/**
	 * @author wuwp
	 * 關於java函數回調,聯想生活中的例子,你回家肚子餓了,問你媽媽煮好飯沒,
	 *她說沒有,於是你跟你媽說,我先出去玩。等飯煮好了再叫你。
	 * 於是你出去玩了。等到飯煮好的時候,你媽媽就叫你吃飯。這一過程中,
	 *你與你媽媽約定了一個飯煮好後叫你的接口,當飯煮好時,
	 *她又通過該接口回饋你飯煮好
	 * 了。
	 * 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Mama mama=new Mama();
		chifan(mama);
	}
	public static void chifan(CallMe callme){
		System.out.println("媽媽飯沒好");
		System.out.println("那我出去玩");
		//跟媽媽達成飯煮好叫我的接口協議
		callme.zhufan();
		System.out.println("回家吃飯");
		
	}

}

interface CallMe{
	public void zhufan();
	
}
class Mama implements CallMe{

	public void zhufan() {
		// TODO Auto-generated method stub
		System.out.println("開始煮飯");
		int i=0;
		while(i<10){
			System.out.println("煮飯第"+i+"分鐘");
			i++;
		}
		System.out.println("飯煮好了");
		System.out.println("叫兒子吃飯");
	}
	
	
}


一篇關於配置Hibernate的示例文章

http://blog.sina.com.cn/s/blog_7ca3aa020100yot8.html

CSS樣式的文章

CSS十三種樣式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章