JAvaインタフェースは複数継承可能

3229 ワード

インタフェースは定数値とメソッド定義の集合です.インタフェースは特殊な抽象クラスです.
JAvaクラスは単一継承です.classB Extends classA
JAvaインタフェースは複数継承できます.Interface3 Extends Interface0, Interface1, interface……
以下はspring ApplicationContextインタフェースのコードであり、複数のインタフェースを継承しています.
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

	/**
	 * Return the unique id of this application context.
	 * @return the unique id of the context, or {@code null} if none
	 */
	String getId();

	/**
	 * Return a name for the deployed application that this context belongs to.
	 * @return a name for the deployed application, or the empty String by default
	 */
	String getApplicationName();

	/**
	 * Return a friendly name for this context.
	 * @return a display name for this context (never {@code null})
	 */
	String getDisplayName();

	/**
	 * Return the timestamp when this context was first loaded.
	 * @return the timestamp (ms) when this context was first loaded
	 */
	long getStartupDate();

	/**
	 * Return the parent context, or {@code null} if there is no parent
	 * and this is the root of the context hierarchy.
	 * @return the parent context, or {@code null} if there is no parent
	 */
	ApplicationContext getParent();

	/**
	 * Expose AutowireCapableBeanFactory functionality for this context.
	 * 

This is not typically used by application code, except for the purpose of * initializing bean instances that live outside of the application context, * applying the Spring bean lifecycle (fully or partly) to them. *

Alternatively, the internal BeanFactory exposed by the * {@link ConfigurableApplicationContext} interface offers access to the * {@link AutowireCapableBeanFactory} interface too. The present method mainly * serves as a convenient, specific facility on the ApplicationContext interface. *

NOTE: As of 4.2, this method will consistently throw IllegalStateException * after the application context has been closed. In current Spring Framework * versions, only refreshable application contexts behave that way; as of 4.2, * all application context implementations will be required to comply. * @return the AutowireCapableBeanFactory for this context * @throws IllegalStateException if the context does not support the * {@link AutowireCapableBeanFactory} interface, or does not hold an * autowire-capable bean factory yet (e.g. if {@code refresh()} has * never been called), or if the context has been closed already * @see ConfigurableApplicationContext#refresh() * @see ConfigurableApplicationContext#getBeanFactory() */ AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException; }


クラス多重継承が許されない主な原因は,AがBとCを同時に継承し,BとCが同時にDメソッドを持つ場合,Aはどのようにしてその継承を決定するかである.
しかしインタフェースにはこのような問題はなく,インタフェースはすべて抽象的な方法で誰を継承しても構わないので,インタフェースは複数のインタフェースを継承することができる.
注意:
1)クラスがインタフェースを実装した場合、そのインタフェースを実装するすべての方法.
2)メソッドの名前、戻りタイプ、パラメータはインタフェースと完全に一致する必要があります.メソッドの戻りタイプがvoidでない場合、メソッドボディには少なくとも1つのreturn文が必要です.
3)インタフェースのメソッドのデフォルトはpublicタイプであるため,実装時には必ずpublicで修飾しなければならない(そうでなければprotectedタイプであり,メソッドの使用範囲を縮小する).