2013-05-11 58 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. 

} 

.运营商允许你访问的成员方法,或变量为一个类的实例变量。