从哪里继承功能?为什么?

时间:2021-10-04 20:14:22

Suppose we have one class implements Interface extends abstract class with same abstract function in both Interface and abstract class. Then class inherit which function Interface or abstract class and why.

假设我们有一个类实现接口扩展抽象类,在接口和抽象类中都使用相同的抽象函数。然后类继承哪个函数接口或抽象类以及为什么。

Like:

public class A extends B implements I
{
    public void set()
    {
         // Some code here
    }
}

Interface:

public interface I {
    public void set();
}

abstract Class:

 public abstract class B
 {
      public abstract void set();    
 }

2 个解决方案

#1


4  

Both. As long as the functions signatures match, the compiler will accept this "double"-inheritance. Keep in mind implementing interface's methods is only a "contract" your class has to verify to be compilable. Implementing the interface only means "my concrete class has to have a method set()". Extending the abstract class B means "my concrete class inherits the method set() from its superclass, and as it's defined as abstract, it needs to implement it". When both these propositions match (as per your example), all is fine.

都。只要函数签名匹配,编译器就会接受这种“双重” - 继承。请记住,实现接口的方法只是您的类必须验证可编译的“合同”。实现接口只意味着“我的具体类必须有一个方法set()”。扩展抽象类B意味着“我的具体类从其超类继承方法set(),并且因为它被定义为抽象,它需要实现它”。当这两个命题都匹配时(根据你的例子),一切都很好。

If there is a difference in the signature of the functions between the interface and the abstract class, your concrete class must then implement both versions.

如果接口和抽象类之间的函数签名存在差异,那么具体类必须实现两个版本。

BTW, slightly off-topic, try to avoid abstract classes as much as you could. If an abstract class has only abstract methods, then it should be an interface. If it has some code in some of its method, then you should probably think about refactoring it to use composition rather than inheritance. Inheritance is evil ;)

BTW,稍微偏离主题,尽量避免抽象类。如果抽象类只有抽象方法,那么它应该是一个接口。如果它的某些方法中有一些代码,那么你应该考虑重构它以使用组合而不是继承。继承是邪恶的;)

#2


0  

It doesn't matter to know from which the method will be inherited because the method in both Interface and Abstract class are abstract meaning no implementation is given.

知道从哪个方法继承是没关系的,因为Interface和Abstract类中的方法都是抽象的意思,没有给出实现。

So all you have to ensure in your concrete class that you must implement all the method defined in the interface but not implemented in the abstract class . AND implement all the abstract methods defined in its superclasses (even if they are not defined in an interface) (from Guillaume)

因此,您必须确保在具体类中必须实现接口中定义的所有方法,但未在抽象类中实现。并实现其超类中定义的所有抽象方法(即使它们未在接口中定义)(来自Guillaume)

#1


4  

Both. As long as the functions signatures match, the compiler will accept this "double"-inheritance. Keep in mind implementing interface's methods is only a "contract" your class has to verify to be compilable. Implementing the interface only means "my concrete class has to have a method set()". Extending the abstract class B means "my concrete class inherits the method set() from its superclass, and as it's defined as abstract, it needs to implement it". When both these propositions match (as per your example), all is fine.

都。只要函数签名匹配,编译器就会接受这种“双重” - 继承。请记住,实现接口的方法只是您的类必须验证可编译的“合同”。实现接口只意味着“我的具体类必须有一个方法set()”。扩展抽象类B意味着“我的具体类从其超类继承方法set(),并且因为它被定义为抽象,它需要实现它”。当这两个命题都匹配时(根据你的例子),一切都很好。

If there is a difference in the signature of the functions between the interface and the abstract class, your concrete class must then implement both versions.

如果接口和抽象类之间的函数签名存在差异,那么具体类必须实现两个版本。

BTW, slightly off-topic, try to avoid abstract classes as much as you could. If an abstract class has only abstract methods, then it should be an interface. If it has some code in some of its method, then you should probably think about refactoring it to use composition rather than inheritance. Inheritance is evil ;)

BTW,稍微偏离主题,尽量避免抽象类。如果抽象类只有抽象方法,那么它应该是一个接口。如果它的某些方法中有一些代码,那么你应该考虑重构它以使用组合而不是继承。继承是邪恶的;)

#2


0  

It doesn't matter to know from which the method will be inherited because the method in both Interface and Abstract class are abstract meaning no implementation is given.

知道从哪个方法继承是没关系的,因为Interface和Abstract类中的方法都是抽象的意思,没有给出实现。

So all you have to ensure in your concrete class that you must implement all the method defined in the interface but not implemented in the abstract class . AND implement all the abstract methods defined in its superclasses (even if they are not defined in an interface) (from Guillaume)

因此,您必须确保在具体类中必须实现接口中定义的所有方法,但未在抽象类中实现。并实现其超类中定义的所有抽象方法(即使它们未在接口中定义)(来自Guillaume)