我是面向对象编程的初学者,我需要一些答案来清除一些东西。我有一个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
}
我希望你明白我的问题。用这些东西来实现它的好方法是什么? 谢谢!
如果您的方法是公开的,您可以使用点符号访问它! gpsNmea.yourMethod();如果你没有指定任何修饰符,你将得到默认修饰符(public为你包中的所有元素,private为其他包中的元素) – GVillani82 2013-05-11 12:36:01