在第二段代码中,必须在调用
locationListener接口的方法之前先调用属性。
在第一段代码中,您可以直接访问接口方法。
因此,如果您知道每个方法调用都会花费cpu时间,则直接在类中实现它而不是将其作为属性将是有益的。
在这种情况下,您有1个引用,可以
BackgroundService使用该引用访问LocationListener的方法
public class BackgroundService extends Service implements LocationListener {}在这种情况下,您有2个引用,一个是 BackgroundService ,另一个是 locationListener
public class BackgroundService extends Service { private LocationListener locationListener = new LocationListener() {};}但是话又说回来,如果您的程序没有关键的时间限制,那就没关系了。最重要的是您的代码可读。
我希望能回答您的问题。



