java 接口可以多继承

叁歲伎倆 2022-08-01 00:28 354阅读 0赞

接口是常量值和方法定义的集合。接口是一种特殊的抽象类。

java类是单继承的。classB Extends classA

java接口可以多继承。Interface3 Extends Interface0, Interface1, interface……

以下是spring ApplicationContext 接口的代码,同时继承了多个接口

  1. public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
  2. MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
  3. /**
  4. * Return the unique id of this application context.
  5. * @return the unique id of the context, or {@code null} if none
  6. */
  7. String getId();
  8. /**
  9. * Return a name for the deployed application that this context belongs to.
  10. * @return a name for the deployed application, or the empty String by default
  11. */
  12. String getApplicationName();
  13. /**
  14. * Return a friendly name for this context.
  15. * @return a display name for this context (never {@code null})
  16. */
  17. String getDisplayName();
  18. /**
  19. * Return the timestamp when this context was first loaded.
  20. * @return the timestamp (ms) when this context was first loaded
  21. */
  22. long getStartupDate();
  23. /**
  24. * Return the parent context, or {@code null} if there is no parent
  25. * and this is the root of the context hierarchy.
  26. * @return the parent context, or {@code null} if there is no parent
  27. */
  28. ApplicationContext getParent();
  29. /**
  30. * Expose AutowireCapableBeanFactory functionality for this context.
  31. * <p>This is not typically used by application code, except for the purpose of
  32. * initializing bean instances that live outside of the application context,
  33. * applying the Spring bean lifecycle (fully or partly) to them.
  34. * <p>Alternatively, the internal BeanFactory exposed by the
  35. * {@link ConfigurableApplicationContext} interface offers access to the
  36. * {@link AutowireCapableBeanFactory} interface too. The present method mainly
  37. * serves as a convenient, specific facility on the ApplicationContext interface.
  38. * <p><b>NOTE: As of 4.2, this method will consistently throw IllegalStateException
  39. * after the application context has been closed.</b> In current Spring Framework
  40. * versions, only refreshable application contexts behave that way; as of 4.2,
  41. * all application context implementations will be required to comply.
  42. * @return the AutowireCapableBeanFactory for this context
  43. * @throws IllegalStateException if the context does not support the
  44. * {@link AutowireCapableBeanFactory} interface, or does not hold an
  45. * autowire-capable bean factory yet (e.g. if {@code refresh()} has
  46. * never been called), or if the context has been closed already
  47. * @see ConfigurableApplicationContext#refresh()
  48. * @see ConfigurableApplicationContext#getBeanFactory()
  49. */
  50. AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
  51. }

不允许类多重继承的主要原因是,如果A同时继承B和C,而B和C同时有一个D方法,A如何决定该继承那一个呢?

但接口不存在这样的问题,接口全都是抽象方法继承谁都无所谓,所以接口可以继承多个接口。

注意:

1)一个类如果实现了一个接口,则要实现该接口的所有方法。

2)方法的名字、返回类型、参数必须与接口中完全一致。如果方法的返回类型不是void,则方法体必须至少有一条return语句。

3)因为接口的方法默认是public类型的,所以在实现的时候一定要用public来修饰(否则默认为protected类型,缩小了方法的使用范围)。

发表评论

表情:
评论列表 (有 0 条评论,354人围观)

还没有评论,来说两句吧...

相关阅读