Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java继承-同级';s重写方法问题_Java_Android_Inheritance_Interface_Overriding - Fatal编程技术网

Java继承-同级';s重写方法问题

Java继承-同级';s重写方法问题,java,android,inheritance,interface,overriding,Java,Android,Inheritance,Interface,Overriding,我有4个类,1个父类和2个继承子类(兄弟),还有一个接口(服务回调,它在每一组时间(例如每5秒)执行一次操作),这是通过Android开发完成的 我将发布3个类,不包括接口,以减少一些代码 父类BaseActivity.java public class BaseActivity extends Activity implements BackgroundServiceCallback{ @Override public void onCreate(Bundle save

我有4个类,1个父类和2个继承子类(兄弟),还有一个接口(服务回调,它在每一组时间(例如每5秒)执行一次操作),这是通过Android开发完成的

我将发布3个类,不包括接口,以减少一些代码

父类BaseActivity.java

 public class BaseActivity extends Activity implements BackgroundServiceCallback{

     @Override
     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         BaseActivity.this.startBackgroundService();
     }

     @Override
     public void onBackgroundServiceCallback() {
         // no concrete implementation (will be implemented in child_a)
     }

     public void startBackgroundService() {
         // configure and start the service here
     }
}
  public class ChildActivity_A extends BaseActivity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // some activity configurations here.
     }

     @Override
     public void onBackgroundServiceCallback() {
         // concrete implementation goes here, and will work
     }
 }
  public class ChildActivity_B extends BaseActivity {

     @Override
     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         // some activity configurations here.

         // if I call super.startBackgroundService() or this.startBackgroundService
         // ChildActivity_A's implementation is not being called
     }
 }
继承子级,ChildActivity\u A.java

 public class BaseActivity extends Activity implements BackgroundServiceCallback{

     @Override
     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         BaseActivity.this.startBackgroundService();
     }

     @Override
     public void onBackgroundServiceCallback() {
         // no concrete implementation (will be implemented in child_a)
     }

     public void startBackgroundService() {
         // configure and start the service here
     }
}
  public class ChildActivity_A extends BaseActivity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // some activity configurations here.
     }

     @Override
     public void onBackgroundServiceCallback() {
         // concrete implementation goes here, and will work
     }
 }
  public class ChildActivity_B extends BaseActivity {

     @Override
     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         // some activity configurations here.

         // if I call super.startBackgroundService() or this.startBackgroundService
         // ChildActivity_A's implementation is not being called
     }
 }
正如您在上面所看到的(后台服务正在做什么),Child_A的具体方法(实现将在每次回调时工作)

现在,这是一个棘手的问题(在我看来) 这是第三节课

继承子ChildActivity_B.java

 public class BaseActivity extends Activity implements BackgroundServiceCallback{

     @Override
     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         BaseActivity.this.startBackgroundService();
     }

     @Override
     public void onBackgroundServiceCallback() {
         // no concrete implementation (will be implemented in child_a)
     }

     public void startBackgroundService() {
         // configure and start the service here
     }
}
  public class ChildActivity_A extends BaseActivity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // some activity configurations here.
     }

     @Override
     public void onBackgroundServiceCallback() {
         // concrete implementation goes here, and will work
     }
 }
  public class ChildActivity_B extends BaseActivity {

     @Override
     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         // some activity configurations here.

         // if I call super.startBackgroundService() or this.startBackgroundService
         // ChildActivity_A's implementation is not being called
     }
 }
如果我从ChildActivity_B调用startBackgroundService,则不会调用ChildActivity_A的具体实现

在android开发之前,我一直在做java程序,认为这是理所当然的,我想我错过了一些关于继承的东西,这让我在这种情况下感到困惑


我只需要了解一下为什么会发生这种情况(顺便说一下,我通过在自定义应用程序类中设置静态标志来启动服务或不启动活动来解决了真正的问题)

在您的代码中
“BaseActivity.this.startBackgroundService()”
实际上是启动后台服务,可在
BaseActivity.java
中使用。在
ChildActivity\u B
中,您通过
super.onCreate(savedInstanceState)调用它所以它只会调用它的super方法,它的super类
BaseActivity.java
,而不是
ChildActivity\u A
ChildActivity\u B
实际上对
ChildActivity\u A
一无所知,我错过了一个测试,我试图调用super.startBackgroundService()在ChildActivity_A上,重写的方法可以工作。现在我明白了。有什么叫做“隐式方法重写”的吗?这就是为什么当我在ChildActivity_B上调用super.startBackgroundService()时,没有调用ChildActivity_A的onBackgroundServiceCallback()实现,因为存在**“隐式”**或者隐藏在ChildActivity\u B中的覆盖?“ChildActivity\u B实际上对ChildActivity\u A一无所知”-如果对你有任何帮助的话,你给了我很大的帮助。等等,我在上面添加了一条评论,我真的想证明我的理解。有隐式重写吗?@zabdiel没有隐式方法重写。重写总是显式的。而且:一个类只是从其父类继承非私有方法。没有重写也不是兄弟类之间的继承关系。