有很多情形,需要让不同的程序员都遵守某个契约,来约束他们软件的行为
程序员之间无需关心他人是如何实现的,通常来说,这也就是接口的来源
比如各种各样的车在路上行驶,某个公司来定义他们的交通规则,另一个公司则教导他们使用GPS系统查看路况,或者如何使用收音机
公司之间无需关心对方在做什么,实际上每家公司都拥有自己的特殊权利,任何时候都能进行内部运作
Java中的接口跟类相似,也是一个引用类型
接口不能实例化,只能被某个类实现,或被其他接口继承(后面详述)
接口的定义:修饰符,interface,接口名,extends多个接口{}
接口的修饰符跟类一样只有两种,但接口可以继承多个接口
如果定义一个变量的类型是接口,只能用实现了这个接口的类或子类的对象赋值
接口里可以有字段,他们全部只能是public
, static
, 和 final
的,因此也可以不写
接口里可以有abstract方法,default方法和static方法,他们全部只能是public的,因此public可以不写
接口里也可以什么都没有
abstract方法是没有方法体的,不写就默认abstract,在小括号后直接跟的是分号
public interface OperateCar { // constant declarations, if any // method signatures // An enum with values RIGHT, LEFT int turn(Direction direction, double radius, double startSpeed, double endSpeed);
实现他的类需要实现这些方法,不实现的类也是abstract类
default方法和static方法是有方法体的
考虑往一个接口里添加一个方法时,直接添加的话,所有实现这个接口的类都要报错,因为没有实现新加的方法
第1种.可以新建一个接口,继承原来的那个,再另外添加个新方法,这样哪些类可以选择是否换成新的接口,不换也没事
第2种,声明为static方法,其他类可以用接口名直接调用
第3种.可以在原接口里添加新方法,声明为default,并有方法体,有默认实现,其他类会当做自己也有
public interface DoIt { void doSomething(int i, double x); int doSomethingElse(String s); default boolean didItWork(int i, double x, String s) { // Method body } }
他的子类(确切的说是子接口)处理default方法有几种情况:
无视,将会默认继承
不加default,重新声明一次,将变成abstract方法
加default,并覆盖掉,成为新的接口
default方法与lambda expressions( 略)
相关推荐
It supports the latest versions of the Java Servlet, JavaServer Pages (JSP), and Java Expression Language (EL) specifications. ##### Inside Servlets - **`javax.servlet` Package**: This package ...
Session_28_High_Density_Memories_and_High_Speed_Interfaces.pdf
We_unified_the_interfaces_of_instruction-tuning_da_Alpaca-CoT
程序_用户_接口_快速通道_焦点的_Program_user_interfaces_the_FRP_way._focal
Human-in-the-Loop Machine Learning lays out methods for humans and machines to work together effectively. Summary Most machine learning systems that are deployed in the world today learn from human ...
This book teaches programmers the essential Java language skills necessary for effectively picking up and using the new Android SDK platform to build mobile, embedded, and even PC apps, especially ...
persist.sys.language=zh \ persist.sys.country=CN PRODUCT_PACKAGES += Bluetooth #PRODUCT_PROPERTY_OVERRIDES += \ # ro.product.8723b_bt.used=true #GPS Feature #PRODUCT_PACKAGES += gps.polaris #...
Interfaces and Inheritance Collections Generics Threads Summary Chapter 3 The Stack Stack Overview Linux Native Layer Dalvik Application Framework Applications Summary Chapter 4 Installing and ...
Java编程演示继承抽象函数和实现接口经典代码Java programming presentation and implementation abstract function interfaces inherited classic code
【标题解析】:“Big_problems_with_big_data_-_Hadoop_interfaces_security.pdf”这个标题指出文档主要讨论的是大数据(Big Data)领域中的重大问题,特别是与Hadoop接口相关的安全性问题。Hadoop是一个开源框架,...
在ATL项目中,为了更好地调试代码,尤其是接口引用计数的问题,微软引入了一个预处理器宏`_ATL_DEBUG_INTERFACES`。这个宏在编译时启用,可以帮助开发者追踪接口的生命周期,确保接口的正确使用和释放。 当`_ATL_...
The Java Programming Language Architectural Tradeoffs Future Trends On the CD-ROM The Resources Page 2 Platform independence Why Platform Independence? Java's Architectural Support for Platform ...
Maintained by The MathWorks, MATLAB allows easy matrix manipulation, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other ...
You will learn all the technical Java programming language such as Loops and Arrays, Boolean Logic, Methods, Inheritance and Polymorphism, Algorithms, etc. You will get a chance to apply what you ...
- Before writing the commands make sure you have npm and react installed on your system 打开命令提示符并编写以下命令 创建react项目后,您将获得一个文件结构,看起来像所附的图片 这是一个样板,它是由许多...
Best-selling programming author Herb Schildt covers the entire Java language, including its syntax, keywords, and fundamental programming principles. You’ll also find information on key portions of ...
covered: The Java programming language for people with no Java experience, and even people with no programming experience at all. Key concepts read like a list of Java features: Object oriented design...
From natural language processing (NLP) applications, such as language translation and understanding news articles and other text sources, to creating chatbots with both voice and text interfaces, you...
Learn Java for Android Development, Third Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the...
covered: The Java programming language for people with no Java experience, and even people with no programming experience at all. Key concepts read like a list of Java features: Object oriented design...