如何在一个类中继承两个类的属性?

发布于 2024-12-12 07:19:21 字数 248 浏览 0 评论 0原文

我有一个程序需要继承broadcastreceiver和MapActivity的属性。实际上,我想在收到朋友发来的短信时发送当前位置(经度和纬度)。我有一个代码,但不知道如何继承这两个类的属性。这是我的代码:

http://pastebin.com/53ZJH3iN

请给我一些想法来解决这个问题或任何其他方法(如果这是不可能的)?

I have a program which requires to inherit the properties of both broadcastreceiver and MapActivity.Actually I want to send current location (longitude and latitude) on receiving an sms from friend.I have a code with me but don't know how to inherit the properties of both the classes.Here is my code:

http://pastebin.com/53ZJH3iN

Please give me some idea to solve this or any other approach if this is not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま柒月 2024-12-19 07:19:22

由于java禁止多重继承,你实际上不能。当您计划申请隐蔽服务时,您不需要任何活动 - 只需一个广播接收器
它与位置服务通信并将获得的位置发送到某处。

As java prohibits multiple inheritance, you actually can not. As you are planning application for covert srveyliance, you do not need any activity - just a broadcast receiver
which comunicates with location service and sends obtained location somewhere.

简美 2024-12-19 07:19:22

Java 不支持多重继承,但您可以实现任意数量的接口,因此可以通过组合来实现。这是一个粗略的轮廓,但有很多方法可以做到这一点。基本思想是,对于您想要继承的一个或两个类,您可以引用某个实现,并通过委托给实际实现来公开相同的接口。

interface MapActivity {
   Coordinates getCoordinates();
...
}

class SomeMapActivityImpl {
...
}

class ReceivelocationActivity extends BroadcastReceiver implements MapActivity {
  private MapActivity mapActivity = new SomeMapActivityImpl();

  public Coordinates getCoordinates() {
    return mapActivity.getCoordinates();
  }

...
}

Java doesn't support multiple inheritance, but you can implement as many interfaces as you'd like so you can do this through composition. Here is one rough outline, but there are many ways you could do it. The basic idea is that for either or both of the classes that you want to inherit from you have a reference to some implementation and you expose the same interface through delegation to the actual implementation.

interface MapActivity {
   Coordinates getCoordinates();
...
}

class SomeMapActivityImpl {
...
}

class ReceivelocationActivity extends BroadcastReceiver implements MapActivity {
  private MapActivity mapActivity = new SomeMapActivityImpl();

  public Coordinates getCoordinates() {
    return mapActivity.getCoordinates();
  }

...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文