java接口

Interface in java is one of the core concept. Java Interface is core part of java programming language and used a lot not only in JDK but also java design patterns. Most of the frameworks use java interface heavily.

Java接口是核心概念之一。 Java接口是Java编程语言的核心部分,不仅在JDK中使用很多,在Java设计模式中也使用了很多。 大多数框架都大量使用Java接口。

Java接口 (Interface in Java)

Interface in java provide a way to achieve abstraction. Java interface is also used to define the contract for the subclasses to implement.

java中的接口提供了一种实现抽象的方法 。 Java接口还用于定义要实现的子类的协定。

For example, let’s say we want to create a drawing consists of multiple shapes. Here we can create an interface Shape and define all the methods that different types of Shape objects will implement. For simplicity purpose, we can keep only two methods – draw() to draw the shape and getArea() that will return the area of the shape.

例如,假设我们要创建一个由多个形状组成的图形。 在这里,我们可以创建一个Shape界面,并定义不同类型的Shape对象将实现的所有方法。 为了简单起见,我们只能保留两种方法– draw()绘制形状和getArea()来返回形状的区域。

Java接口示例 (Java Interface Example)

Based on above requirements, our Shape interface will look like this.

基于上述要求,我们的Shape界面将如下所示。

Shape.java

Shape.java

package com.journaldev.design;public interface Shape {//implicitly public, static and finalpublic String LABLE="Shape";//interface methods are implicitly abstract and publicvoid draw();double getArea();
}

有关Java接口的要点 (Important Points about Interface in Java)

  1. interface is the code that is used to create an interface in java.interface是用于在Java中创建interface的代码。
  2. We can’t instantiate an interface in java.我们无法在Java中实例化接口。
  3. Interface provides absolute abstraction, in last post we learned about abstract classes in java to provide abstraction but abstract classes can have method implementations but interface can’t.接口提供绝对的抽象,在上一篇文章中,我们了解了Java中的抽象类以提供抽象,但是抽象类可以具有方法实现,而接口则不能。
  4. Interfaces can’t have constructors because we can’t instantiate them and interfaces can’t have a method with body.接口不能具有构造函数,因为我们无法实例化它们,并且接口不能具有带有body的方法。
  5. By default any attribute of interface is public, static and final, so we don’t need to provide access modifiers to the attributes but if we do, compiler doesn’t complain about it either.默认情况下,接口的任何属性都是publicstaticfinal ,因此我们不需要为这些属性提供访问修饰符,但是如果这样做,编译器也不会抱怨它。
  6. By default interface methods are implicitly abstract and public, it makes total sense because the method don’t have body and so that subclasses can provide the method implementation.默认情况下,接口方法是隐式抽象的并且是public ,这是完全有意义的,因为该方法没有主体,因此子类可以提供该方法的实现。
  7. An interface can’t extend any class but it can extend another interface. public interface Shape extends Cloneable{} is an example of an interface extending another interface. Actually java provides multiple inheritance in interfaces, what is means is that an interface can extend multiple interfaces.一个接口不能扩展任何类,但是可以扩展另一个接口。 public interface Shape extends Cloneable{}是扩展另一个接口的接口的示例。 实际上,java在接口中提供了多个继承,这意味着一个接口可以扩展多个接口。
  8. implements keyword is used by classes to implement an interface.implements关键字由类用于实现接口。
  9. A class implementing an interface must provide implementation for all of its method unless it’s an abstract class. For example, we can implement above interface in abstract class like this:

    ShapeAbs.java

    package com.journaldev.design;public abstract class ShapeAbs implements Shape {@Overridepublic double getArea() {// TODO Auto-generated method stubreturn 0;}}

    除非是抽象类,否则实现接口的类必须为其所有方法提供实现。 例如,我们可以在抽象类中实现上述接口,如下所示:

    ShapeAbs.java

  10. We should always try to write programs in terms of interfaces rather than implementations so that we know beforehand that implementation classes will always provide the implementation and in future if any better implementation arrives, we can switch to that easily.我们应该始终尝试根据接口而不是实现来编写程序,以便我们事先知道实现类将始终提供实现,并且将来如果有更好的实现出现,我们可以轻松地切换到该实现。

Java接口实现示例 (Java Interface Implementation Example)

Now lets see some implementation of our Shape interface in java.

现在让我们看一下Java中Shape接口的一些实现。

Circle.java

Circle.java

package com.journaldev.design;public class Circle implements Shape {private double radius;public Circle(double r){this.radius = r;}@Overridepublic void draw() {System.out.println("Drawing Circle");}@Overridepublic double getArea(){return Math.PI*this.radius*this.radius;}public double getRadius(){return this.radius;}
}

Notice that Circle class has implemented all the methods defined in the interface and it has some of its own methods also like getRadius(). The interface implementations can have multiple type of constructors. Lets see another interface implementation for Shape interface.

注意,Circle类已​​经实现了接口中定义的所有方法,并且它具有一些自己的方法,例如getRadius() 。 接口实现可以具有多种类型的构造函数。 让我们看一下Shape接口的另一个接口实现。

Rectangle.java

Rectangle.java

package com.journaldev.design;public class Rectangle implements Shape {private double width;private double height;public Rectangle(double w, double h){this.width=w;this.height=h;}@Overridepublic void draw() {System.out.println("Drawing Rectangle");}@Overridepublic double getArea() {return this.height*this.width;}}

Notice the use of override annotation, learn about annotations in java and why we should always use override annotation when overriding a method in java.

注意使用override注释,了解java中的注释以及为什么在覆盖java中的方法时总是要使用override注释 。

Here is a test program showing how to code in terms of interfaces and not implementations.

这是一个测试程序,显示了如何根据接口而不是实现进行编码。

ShapeTest.java

ShapeTest.java

package com.journaldev.design;public class ShapeTest {public static void main(String[] args) {//programming for interfaces not implementationShape shape = new Circle(10);shape.draw();System.out.println("Area="+shape.getArea());//switching from one implementation to another easilyshape=new Rectangle(10,10);shape.draw();System.out.println("Area="+shape.getArea());}}

Output of the above java interface example program is:

上面的java接口示例程序的输出是:

Drawing Circle
Area=314.1592653589793
Drawing Rectangle
Area=100.0

Java接口的好处 (Java Interface Benefits)

  1. Interface provides a contract for all the implementation classes, so its good to code in terms of interfaces because implementation classes can’t remove the methods we are using.接口为所有实现类提供了一个契约,因此在接口方面进行编码非常有用,因为实现类无法删除我们正在使用的方法。
  2. Interfaces are good for starting point to define Type and create top level hierarchy in our code.接口非常适合作为在我们的代码中定义Type和创建顶层层次结构的起点。
  3. Since a java class can implements multiple interfaces, it’s better to use interfaces as super class in most of the cases.由于Java类可以实现多个接口,因此在大多数情况下,最好将接口用作超类。

Java接口的缺点 (Java Interface Disadvantages)

Although interfaces provide a lot of advantages but it has some disadvantages too.

尽管接口具有很多优点,但也有一些缺点。

  1. We need to chose interface methods very carefully at the time of designing our project because we can’t add of remove any methods from the interface at later point of time, it will lead compilation error for all the implementation classes. Sometimes this leads to have a lot of interfaces extending the base interface in our code that becomes hard to maintain.在设计我们的项目时,我们需要非常仔细地选择接口方法,因为我们不能在稍后的时间添加从接口中删除任何方法,这将导致所有实现类的编译错误。 有时,这导致许多接口扩展了我们代码中的基本接口,而这些接口变得难以维护。
  2. If the implementation classes has its own methods, we can’t use them directly in our code because the type of Object is an interface that doesn’t have those methods. For example, in above code we will get compilation error for code shape.getRadius(). To overcome this, we can use typecasting and use the method like this:
    Circle c = (Circle) shape;
    c.getRadius();

    Although class typecasting has its own disadvantages.

    shape.getRadius()编译错误。 为了克服这个问题,我们可以使用类型转换,并使用如下方法:

    尽管类类型转换有其自身的缺点。

Thats all I have for interface in java. Since we use java interface a lot, we should be aware of its features. Make sure you use interfaces in designing the system and as a contract between the client and the subclasses implementing the interfaces.

多数民众赞成在Java接口。 由于我们经常使用Java接口,因此我们应该意识到它的功能。 确保在设计系统时使用接口,并确保客户端和实现接口的子类之间使用合约。

Update: Java 8 has changed the definition of interfaces with the introduction of default methods and static methods implementation. For more details, please read Java 8 interface.

更新 :Java 8通过引入默认方法和静态方法实现更改了接口的定义。 有关更多详细信息,请阅读Java 8接口 。

翻译自: https://www.journaldev.com/1601/interface-in-java

java接口

java接口_Java接口相关推荐

  1. java private 接口_java接口中 定义 private 私有方法

    在传统的Java编程中,被广为人知的一个知识点是:java Interface接口中不能定义private私有方法.只允许我们定义public访问权限的方法.抽象方法或静态方法.但是从Java 9 开 ...

  2. java什么是接口_Java接口是什么

    Java接口是什么 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法,以下 ...

  3. 列举java接口_Java接口特点列举说明

    1.接口是一个特殊的抽象类,接口中的所有方法都是抽象方法,所有的属性都是静态常量,一个类可以实现多个接口 接口无修饰符和为abstract时,不能包之间调用:public修饰时,可以包之间调用,但是要 ...

  4. javq接口_Java为什么要使用接口_java接口怎么使用

    Java接口是什么 Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 接口 ...

  5. javq接口_java 接口详解

    定义接口 接口继承和实现继承的规则不同,一个类只有一个直接父类,但可以实现多个接口.Java 接口本身没有任何实现,只描述 public 行为,因此 Java 接口比 Java 抽象类更抽象化.Jav ...

  6. java 常量接口_java接口定义常量研究

    背景 和同事讨论到,在接口中定义常量的问题,引发了争论,即,假如在接口中定义变量,是否需要用static来限定的问题,或者说用static和不用static会有什么区别. 引论 package spr ...

  7. java 接口嵌套接口_Java接口嵌套

    在Java语言中,接口可以嵌套在类或其它接口中.由于Java中interface内是不可以嵌套class的,所以接口的嵌套就共有两种方式:class嵌套interface.interface嵌套int ...

  8. java 二义性_Java接口默认方法带来的问题分析【二义性问题】

    本文实例分析了Java接口默认方法带来的问题.分享给大家供大家参考,具体如下: 一 点睛 Java 8中,如果一个类实现两个或多个接口,即"变相"的多继承,但是若其中两个接口都包含 ...

  9. java异步接口_Java接口异步调用

    前言 java接口调用从调用方式上可以分为3类:同步调用,异步调用,回调:同步调用基本不用说了,它是一种阻塞式的调用,就是A方法中直接调用方法B,从上往下依次执行.今天来说说异步调用. 什么是异步调用 ...

最新文章

  1. JDK线程池的ThreadFactory
  2. java 静态 编译_Java中的动态和静态编译实例详解
  3. jQuery源码dom ready分析
  4. Python基础数据之列表知识(二)
  5. python电子章_Python语言程序设计(电子)答案
  6. 压缩信息立方体和集合技术内幕
  7. 什么是云计算?云计算学习基础
  8. 软件工程概论总结第四章
  9. JAVA面试要点002_Git中fetch和pull的区别
  10. mvn如何编译源码命令_java – 如何使用maven编译单个文件?
  11. 敏捷结果30天练习即将开始
  12. python 在线培训费用-Python人工智能在线培训班学费多少钱?
  13. 错误: -source 1.6 中不支持 diamond 运算符
  14. smbus协议的command_SMBus接口信号/应用框图/帧格式
  15. ads pspice 导入_ADS中使用pspice模型
  16. 第三方调试助手的与S7-1200 PLC的通信
  17. 电磁场与波 matlab,电磁场数值计算法与MATLAB实现
  18. SAP中利用标准成本报表计算成品人工成本及组成实例
  19. TestCenter测试管理工具环境配置(C)
  20. 将阿拉伯数字转为中文数字读法

热门文章

  1. 苹果开发者账号那些事儿(三)
  2. Java基础知识强化87:BigInteger类之BigInteger加减乘除法的使用
  3. Robocode教程4——Robocode的游戏物理
  4. 四川专利代理机构列表
  5. [转载] Python递归遍历目录下所有文件查找指定文件
  6. python 基础课程第二天
  7. 第三节基础篇—SQL的约束
  8. 读美国教授写给被开除中国留学生的信感悟
  9. Copy_on_write的简单实现
  10. Android加密算法之AES加密和解密实现