2013-05-11 88 views
1

我是面向對象編程的初學者,我需要一些答案來清除一些東西。我有一個MainActivity和幾個不同的操作類。例如,在MainActivity中,我使用BluetoothReceiver類創建名爲mBluetoothReceiver的對象。有一些建立和管理BT連接的方法,例如sendData。在Nmea類中,我得到了一些使用BluetoothReceiver方法的方法,因此我通過了構造函數mBluetoothReceiver。對創建對象和使用它的方法感到困惑

MainActivity類別:

public class MainActivity extends Activity { 

    BluetoothService mBluetoothService = new BluetoothService(this); 

    //create new object from Nmea class and pass mBluetoothService to mNmea 
    Nmea mNmea = new Nmea(mBluetoothService); 
} 

NMEA類:

public class Nmea { 

BluetoothService mBluetoothService; 

    //constructor for Nmea for BluetoothServce object 
    public Nmea(BluetoothService bluetoothService) { 
     mBluetoothService = bluetoothService; 
    } 

    public Nmea() 
    { 
    //empty constructor { 

    } 

    //Nmea methods... 
} 

我的問題是,我也有GPS類,它也將使用從NMEA類的方法,但我不知道如何做到這一點。可以在Nmea類中使用空的構造函數並在GPS類中創建Nmea對象嗎?如果我沒有通過藍牙服務對象,藍牙可能無法工作?在GPS類中,我無法創建新的BluetoothService連接對象並將其傳遞給Nmea構造函數,因爲我只需要在整個項目中建立一個連接。

GPS類:

public çlass GPS { 

Nmea gpsNmea = new Nmea(); 

//I need to use Nmea methods here 

} 

我希望你明白我的問題。用這些東西來實現它的好方法是什麼? 謝謝!

+0

如果您的方法是公開的,您可以使用點符號訪問它! gpsNmea.yourMethod();如果你沒有指定任何修飾符,你將得到默認修飾符(public爲你包中的所有元素,private爲其他包中的元素) – GVillani82 2013-05-11 12:36:01

回答

1

你可以寫這樣的事情:

public class MainActivity extends Activity { 
    BluetoothService mBluetoothService = new BlueToothService(this); 

    Nmea mNmea = new Nmea(mBluetoothService); 

    Gps mGps = new Gps(mNmea);  
} 

而且你Gps cconstructor需要看起來像這樣:

public class Gps { 

    private Nmea mNmea; 

    public Gps(Nmea nmea) { 
     mNmea = nmea; 
    } 
} 

如果你需要有BluetoothService類只有一個實例,您需要寫他使用Singleton design pattern和所有需要的方法在Nmea類宣佈爲public

+0

在你編寫的例子中,它將只有一個BluetoothService實例,對吧?我認爲這對我來說是最好的和邏輯的解決方案.. – anze87 2013-05-11 13:05:21

+0

是的。只有一個實例! – 2013-05-11 13:08:06

+0

很酷,謝謝! ;) – anze87 2013-05-11 13:12:11

1

訪問類方法

取決於方法access modifier,您可以通過使用.運營商提供一種方法。像這樣:

String s = "Hello"; 
s = s.substring(0,3); // See how you use the ".", then the name of the method. 

其他查詢

這是確定把空的構造在NMEA類和類GPS NMEA創建對象?

這沒有任何價值。如果不明確寫入,Java將提供default constructor

在GPS類中,我無法創建新的BluetoothService連接對象並將其傳遞給Nmea構造函數,因爲我只需要在整個項目中建立一個連接。

然後,您需要將處理對象BluetoothService的類轉換爲單例。你可以閱讀關於單身人士here。使用單例模式,您可以靜態訪問該對象,而無需一直創建一個新對象。

例如

public abstract class BluetoothSingleton 
{ 
    private static BluetoothService instance; 
    // The one instance of BluetoohService that will be created. 

    public static BluetoothService getInstance() 
    { 
     if(instance == null) 
     { 
      // If an object doesn't currently exist. 
      instance = new BluetoothService(); // or whatever you're using. 
     } 
     return instance; 
    } 
} 

然後,當你想獲得BluetoothService對象,只需調用在BluetoothSingleton類的getInstance()方法。

BluetoothService = BluetoothSingleton.getInstance(); 
// This code will return the exact same instance. Only one will ever be created. 
0

您可以使用一個實例Nmea類的內部GPS使用的Nmea方法。只是裏面GPS類如添加到您的代碼gpsNmea.any_Nmea_function()

public çlass GPS { 

Nmea gpsNmea = new Nmea(); 

gpsNmea.getN(); //assuming getN() is a function defined in Nmea class. 

} 

.運營商允許你訪問的成員方法,或變量爲一個類的實例變量。