值傳遞、引用傳遞的區(qū)別
posted @ 2014-03-26 22:29 午后星期午 閱讀(149) | 評論 (0) | 編輯 收藏
2014年3月26日 #
posted @ 2014-03-26 22:29 午后星期午 閱讀(149) | 評論 (0) | 編輯 收藏
2013年12月31日 #
posted @ 2013-12-31 14:21 午后星期午 閱讀(189) | 評論 (0) | 編輯 收藏
所謂動態(tài)代理類是在運行時生成的class,在生成它時,你必須提供一組interface給它,則動態(tài)代理類就宣稱它實現(xiàn)了這些 interface。當(dāng)然,動態(tài)代理類就充當(dāng)一個代理,你不要企圖它會幫你干實質(zhì)性的工作,在生成它的實例時你必須提供一個handler,由它接管實際的工作。
下面通過實例來說明:
Subject.java 抽象借口:聲明代理對象和真實對象的共同接口
[java]
public interface Subject {
public void doSomething();
}
public interface Subject {
public void doSomething();
}
RealSubject.java 真實被tb代理對象
[java]
public class RealSubject implements Subject {
@Override
public void doSomething() {
System.out.println("RealSubject.doSomething");
}
}
public class RealSubject implements Subject {
@Override
public void doSomething() {
System.out.println("RealSubject.doSomething");
}
}
DynamicProxy.java 代理對象
[java]
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class DynamicProxy implements InvocationHandler {
private Object object;
public DynamicProxy(Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
System.out.println("Before Invoke ! method : " + method);
//我們可以再代理方法調(diào)用前后添加功能
Object result = method.invoke(object, args);
System.out.println("object : " + object + " result : " + result + " args : " + args);
System.out.println("After Invoke !");
return result;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class DynamicProxy implements InvocationHandler {
private Object object;
public DynamicProxy(Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
System.out.println("Before Invoke ! method : " + method);
//我們可以再代理方法調(diào)用前后添加功能
Object result = method.invoke(object, args);
System.out.println("object : " + object + " result : " + result + " args : " + args);
System.out.println("After Invoke !");
return result;
}
}
Client.java 測試
[java]
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Client {
public static void main(String[] args) throws Exception {
//創(chuàng)建目標(biāo)對象,也就是被代理對象
RealSubject realSubject = new RealSubject();
//將目標(biāo)對象交給代理
InvocationHandler handler = new DynamicProxy(realSubject);
// Class proxyClass = Proxy.getProxyClass(Subject.class.getClassLoader()
// , new Class[]{Subject.class});
// Subject subject = (Subject)proxyClass.getConstructor(new Class[]{InvocationHandler.class})
// .newInstance(new Object[]{handler});
//返回代理對象,相當(dāng)于上面兩句
Subject subject = (Subject) Proxy.newProxyInstance(handler.getClass().getClassLoader(),
realSubject.getClass().getInterfaces(),
handler);
//叫代理對象去doSomething(),其實在代理對象中的doSomething()中還是會
//用handler來調(diào)用invoke(proxy, method, args) 參數(shù)proxy為調(diào)用者subject(this),
//method為doSomething(),tb參數(shù)為方法要傳入的參數(shù),這里沒有
subject.doSomething();
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Client {
public static void main(String[] args) throws Exception {
//創(chuàng)建目標(biāo)對象,也就是被代理對象
RealSubject realSubject = new RealSubject();
//將目標(biāo)對象交給代理
InvocationHandler handler = new DynamicProxy(realSubject);
// Class proxyClass = Proxy.getProxyClass(Subject.class.getClassLoader()
// , new Class[]{Subject.class});
// Subject subject = (Subject)proxyClass.getConstructor(new Class[]{InvocationHandler.class})
// .newInstance(new Object[]{handler});
//返回代理對象,相當(dāng)于上面兩句
Subject subject = (Subject) Proxy.newProxyInstance(handler.getClass().getClassLoader(),
realSubject.getClass().getInterfaces(),
handler);
//叫代理對象去doSomething(),其實在代理對象中的doSomething()中還是會
//用handler來調(diào)用invoke(proxy, method, args) 參數(shù)proxy為調(diào)用者subject(this),
//method為doSomething(),參數(shù)為方法要傳入的參數(shù),這里沒有
subject.doSomething();
}
}
打印結(jié)果:
Before Invoke ! method : public abstract void Subject.doSomething()
RealSubject.doSomething
object : RealSubject@ec6b00 result : null args : null
After Invoke !
注意:
Java動態(tài)代理涉及到的兩個類:
InvocationHandler:該接口中僅定義了一個Object : invoke(Object proxy, Method method, Object[] args);參數(shù)proxy指代理類,method表示被代理的方法,args為method中的參數(shù)數(shù)組,返回值Object為代理實例的方法調(diào)用返回的值。這個抽象方法在代理類中動態(tài)實現(xiàn)。
Proxy:所有動態(tài)代理類的父類,提供用于創(chuàng)建動態(tài)代理類和實例的靜態(tài)方法。
posted @ 2013-12-31 14:07 午后星期午 閱讀(121) | 評論 (0) | 編輯 收藏
本文分享了關(guān)于Java數(shù)組最頂級的11大方法,幫助你解決工作流程問題,無論是運用在團(tuán)隊環(huán)境或是在私人項目中,你都可以直接拿來用!
0. 聲明一個數(shù)組(Declare an array)
String[] aArray = new String[5];
String[] bArray = {"a","b","c", "d", "e"};
String[] cArray = new String[]{"a","b","c","d","e"};
1. 在Java中輸出一個數(shù)組(Print an array in Java)
int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
// print directly will print reference value
System.out.println(intArray);
// [I@7150bd4d
System.out.println(intArrayString);
// [1, 2, 3, 4, 5]
2. 從數(shù)組中創(chuàng)建數(shù)組列表(Create an ArrayList from an array)
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]
3. 檢查愛淘寶數(shù)組中是否包含特定值(Check if an array contains a certain value)
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true
4. 連接兩個數(shù)組( Concatenate two arrays)
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
5. 聲明一個數(shù)組內(nèi)鏈(Declare an array inline )
method(new String[]{"a", "b", "c", "d", "e"});
6. 將數(shù)組元素加入到一個獨立的字符串中(Joins the elements of the provided array into a single String)
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);
// a, b, c
7. 將數(shù)組列表轉(zhuǎn)換成一個數(shù)組 (Covnert an ArrayList to an array)
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
8. 將數(shù)組轉(zhuǎn)換成一個集合(Convert an array to a set)
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]
9. 反向數(shù)組(Reverse an array)
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]
10. 刪除數(shù)組元素(Remove element of an array)
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);
//create a new array
System.out.println(Arrays.toString(removed));
One more – convert int to byte array
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
System.out.format("0x%x ", t);
}
posted @ 2013-12-31 14:07 午后星期午 閱讀(110) | 評論 (0) | 編輯 收藏
posted @ 2013-12-31 13:58 午后星期午 閱讀(130) | 評論 (0) | 編輯 收藏
posted @ 2013-12-31 13:56 午后星期午 閱讀(125) | 評論 (0) | 編輯 收藏
posted @ 2013-12-31 13:55 午后星期午 閱讀(106) | 評論 (0) | 編輯 收藏
posted @ 2013-12-31 13:45 午后星期午 閱讀(116) | 評論 (0) | 編輯 收藏
①
②
③
④
⑤
[ 轉(zhuǎn)載出處:http://www.aygfsteel.com/fancydeepin
posted @ 2013-12-31 13:27 午后星期午 閱讀(118) | 評論 (0) | 編輯 收藏
package cc.wshao.steer.util;
import java.util.Random;
public class StrUtils {
}
posted @ 2013-12-31 13:22 午后星期午 閱讀(129) | 評論 (0) | 編輯 收藏