博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 接口可以多继承
阅读量:4215 次
发布时间:2019-05-26

本文共 2894 字,大约阅读时间需要 9 分钟。

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

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,则方法体必须至少有一条return语句。

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

转载地址:http://clnmi.baihongyu.com/

你可能感兴趣的文章
poj 3863Business Center
查看>>
Android编译系统简要介绍和学习计划
查看>>
Android编译系统环境初始化过程分析
查看>>
user2eng 笔记
查看>>
DRM in Android
查看>>
ARC MRC 变换
查看>>
Swift cell的自适应高度
查看>>
【linux】.fuse_hiddenXXXX 文件是如何生成的?
查看>>
【LKM】整合多个LKM为1个
查看>>
【Windows C++】调用powershell上传指定目录下所有文件
查看>>
Java图形界面中单选按钮JRadioButton和按钮Button事件处理
查看>>
小练习 - 排序:冒泡、选择、快排
查看>>
SparkStreaming 如何保证消费Kafka的数据不丢失不重复
查看>>
Spark Shuffle及其调优
查看>>
数据仓库分层
查看>>
常见数据结构-TrieTree/线段树/TreeSet
查看>>
Hive数据倾斜
查看>>
TopK问题
查看>>
Hive调优
查看>>
HQL排查数据倾斜
查看>>