我現在已經寫了一些藍牙應用程序,但我從來沒有見過this particular approach直到最近。在這個例子中,他們使用device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
,這在我看來只是說BluetoothSocket bs = createRfcommSocket(...
。當我使用getClass.getMethod(...)時,它是「反射」嗎?
是什麼他們的做法
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
sock = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
和礦山
sock = createRfcommSocket(.....
之間的區別是有沒有理由使用一個或其他?
謝謝
是createRfcommSocket的一種靜態方法嗎? – Carsten 2010-07-25 23:11:50
@Carsten我認爲它不能是靜態的,因爲在這種情況下,invoke()的第一個爭論應該是null。 – Gopi 2010-07-25 23:16:59
我很確定那個寫這個其他主題的人說:「我甚至嘗試過這樣做,作爲一個瘋狂的最後努力,看看它是否有所作爲。」不要提倡這是一種編寫代碼的理智方式:) – Affe 2010-07-26 00:02:53