当抽象类实现接口方法时,这些方法是否与抽象类的常规方法被同等对待?

huangapple go评论75阅读模式
英文:

When an abstract class implements interface methods, are those methods treated same way as regular methods of that abstract class?

问题

好的,我正在尝试理解抽象类如何处理接口的已实现方法。
假设我们有一个抽象类,它实现了一个接口的3个方法,我们将在另一个类中初始化这个抽象类,在这个类中,我们只需要实现这些接口方法中的一个。它能够正常运行,我只是想理解Java如何重新覆盖在初始化抽象类的地方的接口方法。我只想知道整个机制是如何工作的。

public abstract class AnimationListenerAdapter implements Animation.AnimationListener
{   
    @Override
    public void onAnimationStart(Animation animation)
    {

    }

    @Override
    public void onAnimationEnd(Animation animation)
    {

    }

    @Override
    public void onAnimationRepeat(Animation animation)
    {

    }
}
fadeIn.setAnimationListener(new AnimationListenerAdapter(){
    @Override
    public void onAnimationEnd(Animation animation)
    {
        System.out.println("动画结束!");
    }
});
英文:

Ok I'm trying to understand how an abstract class treats implemented methods of an interface.
Let's say we have an abstract class that implements 3 methods of an interface and we will initialize this abstract class in another class, where we need to implement only of of those interfaces methods. It works perfectly I'm just trying to understand how java re-overrides the interface method in where abstract class is initialized. I just wanna know how this whole thing works.

public abstract class AnimationListenerAdapter implements Animation.AnimationListener
{   
    @Override
    public void onAnimationStart(Animation animation)
    {

    }

    @Override
    public void onAnimationEnd(Animation animation)
    {

    }

    @Override
    public void onAnimationRepeat(Animation animation)
    {

    }
}
fadeIn.setAnimationListener(new AnimationListenerAdapter(){
    @Override
    public void onAnimationEnd(Animation animation)
    {
        System.out.println("Anim Ended!");
    }
});

答案1

得分: 1

  1. 如果一个 抽象类A 实现接口B,那么 A 可能会或者可能不会实现在 B 中声明的抽象方法:

    • A 中实现的方法将会是具体的、已实现的方法,你可以在直接或间接扩展 A 的类的实例上调用/调用这些方法;

    • A 中未实现的方法必须在第一个(在继承体系中遇到的)具体类中实现,该类扩展了 A

      <sup>接口的抽象方法必须在实现它的抽象类中实现,或者可以将这些抽象方法的实现责任交给扩展该抽象类的具体类</sup>

  2. 不能 显式地初始化(也不能实例化)你的抽象类。抽象类的字段唯一的初始化方式是当其子具体类被实例化并且子类的构造函数调用抽象类的构造函数时。

英文:
  1. If an abstract class A implements interface B, A may or may not implement abstract methods declared in B:

    • methods implemented in A, will be concrete, implemented methods, that you can call/invoke on the instance of a class, that directly or indirectly extends A;

    • methods not implemented in A, will have to be implemented in a first encountered (in the hierarchy) concrete class, that extends A.

      <sup>Abstract methods of interface, must be implemented in its implementer abstract class, or the responsibility of implementation of those abstract methods, can be handed off down to the concrete class, that extends that abstract class</sup>

  2. You cannot initialize (nor you can instantiate), as such, your abstract class explicitly. The only way abstract class's fields are initialized is when its child concrete class is instantiated and child class's constructor, invokes the abstract class's one.

答案2

得分: 1

事情的关键是你的抽象类在这里并不是真正的抽象类。它通过不做任何操作来完全实现了接口。

因此,当你执行“new AnimationListenerAdapter()”时,你就不再需要提供这三个方法的实现,因为它已经有了一个实现!但是,你为onAnimationEnd提供了一个新的实现,它现在扩展了来自AnimationListenerAdapter的实现(AnimationListenerAdapter实现了来自Animation.AnimationListener的实现)。

简而言之,你有一个从抽象类到接口的第一层实现(尽管在这里这并不真的是一个抽象类,你可以直接去掉抽象关键字,在其中没有任何抽象内容...),然后你有第二层继承,连接着你的类和匿名类,后者覆盖了它的一个方法。

英文:

The thing is your abstract class isn't really abstract here. It implements fully the interface by ... doing nothing.

So, when you do your "new AnimationListenerAdapter()", you're no more required to provide the implementation of the three methods, since it has already one implementation ! But, you are providing a new implementation for onAnimationEnd, which now extends the one from AnimationListenerAdapter (which implemented the one from Animation.AnimationListener).

In brief, you have a first layer of implementation from your abstract class to your interface (even though this isn't really an abstract class here, you can just remove the abstract, nothing is abstract in it ...), then you have a second layer of inheritance between your class and your anonymous class overriding one of its method.

huangapple
  • 本文由 发表于 2020年10月15日 03:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64360653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定