java中接口私有反方

Java 9 has been released and there has been a lot of changes. Today we will look into Java 9 private methods in interfaces change.

Java 9已发布,并且进行了许多更改。 今天,我们将研究接口更改中的Java 9私有方法。

接口中的Java 9私有方法 (Java 9 Private Methods in Interfaces)

There has been a lot of changes in interfaces in recent java releases. Few days back I wrote an extensive post on Java 9 features. We will look into following sections in this post.

在最近的Java版本中,接口有很多更改。 几天前,我写了一篇有关Java 9功能的文章 。 我们将研究本文的以下部分。

  1. Java 7 InterfaceJava 7接口
  2. Java 8 Interface ChangesJava 8接口更改
  3. Java 9 Interface ChangesJava 9接口更改
  4. Rules to define private methods in an Interface?在接口中定义私有方法的规则?
  5. Why do we need private methods in an Interface?为什么在接口中需要私有方法?
  6. Interview Questions And Answers面试问答

In this post, I have provided pseudocode only to understand this new feature because everyone knows what is an interface in java and what is a private method in Java.

在本文中,我仅提供伪代码来理解此新功能,因为每个人都知道Java中的接口是什么,而Java中的私有方法是什么。

Java 7接口 (Java 7 Interface)

In Java SE 7 or earlier versions, an interface can have only two kinds of things.

在Java SE 7或更早版本中,接口只能具有两种类型的东西。

  1. Constant variables常数变数
  2. Abstract methods抽象方法

We couldn’t provide method implementations in interfaces. If we want to provide the combination of abstract methods and non-abstract methods (methods with implementation), we had to go for abstract class only.

我们无法在接口中提供方法实现。 如果我们要提供抽象方法和非抽象方法(带有实现方法的方法)的组合,则只能选择抽象类 。

public interface DBLogging{String MONGO_DB_NAME = "ABC_Mongo_Datastore";String NEO4J_DB_NAME = "ABC_Neo4J_Datastore";String CASSANDRA_DB_NAME = "ABC_Cassandra_Datastore";void logInfo(String message);void logWarn(String message);void logError(String message);void logFatal(String message);}

Here we have defined couple of constants and public abstract methods. What if I would like to provide some implementation to these methods here, say default implementation. Then we should go for abstract class in Java 7 or earlier versions.

在这里,我们定义了几个常量和公共抽象方法。 如果我想在这里为这些方法提供一些实现,例如默认实现,该怎么办? 然后,我们应该使用Java 7或更早版本的抽象类。

Java 8接口更改 (Java 8 Interface Changes)

Oracle Corporation has introduced some new features to interface in Java 8 Release. That is Default methods and Static methods feature.

Oracle Corporation引入了一些新功能来与Java 8 Release进行接口。 这就是默认方法静态方法功能。

Yes, we can write method implementations in Interface from Java 8 onwards. We need to use default keyword to define them as shown below.

是的,我们可以从Java 8开始在Interface中编写方法实现。 我们需要使用default关键字来定义它们,如下所示。

In Java 8, an interface can have only four kinds of things:

在Java 8中,接口只能包含四种内容:

  1. Constant variables常数变数
  2. Abstract methods抽象方法
  3. Default methods默认方法
  4. Static methods静态方法

Default and Static methods were added in Java 8 interface changes.

在Java 8接口更改中添加了默认方法和静态方法。

public interface DBLogging{String MONGO_DB_NAME = "ABC_Mongo_Datastore";String NEO4J_DB_NAME = "ABC_Neo4J_Datastore";String CASSANDRA_DB_NAME = "ABC_Cassandra_Datastore";// abstract method examplevoid logInfo(String message);// default method exampledefault void logWarn(String message){// Step 1: Connect to DataStore// Step 2: Log Warn Message// Step 3: Close the DataStore connection}default void logError(String message){// Step 1: Connect to DataStore// Step 2: Log Error Message// Step 3: Close the DataStore connection}default void logFatal(String message){// Step 1: Connect to DataStore// Step 2: Log Fatal Message// Step 3: Close the DataStore connection  }// static method examplestatic boolean isNull(String str) {System.out.println("Interface Null Check");return str == null ? true : "".equals(str) ? true : false;}// Any other abstract, default, static methods}

Above example shows everything we can do in Java 8 interfaces, notice the extra set of default and static methods.

上面的示例显示了我们在Java 8接口中可以做的所有事情,请注意额外的默认和静态方法集。

Did you notice the code redundancy in all the log methods? Every method is opening and closing a connection on it’s own. If we want code reuse, then we will have to move these common code to a public method, but then it will be accessible to all other classes.

您是否注意到所有日志方法中的代码冗余? 每个方法都自己打开和关闭连接。 如果我们想要代码重用,那么我们将不得不将这些通用代码移至一个公共方法,但是所有其他类都可以访问它。

What if we want to reuse the code but also don’t want to expose it to others? We will have to go for abstract class with a private method for the common code.

如果我们想重用代码但又不想将其公开给别人怎么办? 我们将不得不使用带有通用方法私有方法的抽象类。

Java 9接口更改 (Java 9 Interface Changes)

To provide a resolution to above scenarios, Oracle Corporation has introduced private methods in interfaces in java 9 release.

为了解决上述情况,Oracle Corporation在Java 9版本中的接口中引入了私有方法。

From Java 9 onwards, we can write private methods in interfaces using private access modifier as shown below (like other private methods).

从Java 9开始,我们可以使用private访问修饰符在接口中编写私有方法,如下所示(就像其他私有方法一样)。

In Java 9 and later versions, an interface can have six kinds of things:

在Java 9和更高版本中,接口可以包含六种内容:

  1. Constant variables常数变数
  2. Abstract methods抽象方法
  3. Default methods默认方法
  4. Static methods静态方法
  5. Private methods私人方法
  6. Private Static methods私有静态方法

Private methods and private static methods are added in Java 9 interface changes.

Java 9接口更改中添加了私有方法和私有静态方法。

public interface DBLogging {String MONGO_DB_NAME = "ABC_Mongo_Datastore";String NEO4J_DB_NAME = "ABC_Neo4J_Datastore";String CASSANDRA_DB_NAME = "ABC_Cassandra_Datastore";default void logInfo(String message) {log(message, "INFO");}default void logWarn(String message) {log(message, "WARN");}default void logError(String message) {log(message, "ERROR");}default void logFatal(String message) {log(message, "FATAL");}private void log(String message, String msgPrefix) {// Step 1: Connect to DataStore// Step 2: Log Message with Prefix and styles etc.// Step 3: Close the DataStore connection}// Any other abstract, static, default methods
}

Here we have moved redundant code into a common private method so that our API Clients can’t see them. Notice the below image from Eclipse Oxygen supporting Java 9.

在这里,我们将冗余代码移到了通用的私有方法中,以便我们的API客户端无法看到它们。 请注意下图来自支持Java 9的Eclipse Oxygen。

在接口中定义私有方法的规则? (Rules to define private methods in an Interface?)

While writing private methods in an Interface, we should follow these rules:

在接口中编写私有方法时,我们应遵循以下规则:

  1. We should use private modifier to define these methods.我们应该使用private修饰符来定义这些方法。
  2. No private and abstract modifiers together, it will give compiler error.没有私有和抽象修饰符在一起,它将给编译器错误。
  3. It is not a new rule. We cannot use the combination of private and abstract modifiers, because both have different meanings.

    这不是一个新规则。 我们不能使用私有修饰符和抽象修饰符的组合,因为两者都有不同的含义。

  • The “private” method means fully implemented method because sub-classes cannot inherit and override this method.“私有”方法意味着完全实现的方法,因为子类无法继承和覆盖此方法。
  • The “abstract” method means no-implementation method. Here sub-classes should inherit and override this method.“抽象”方法是指不执行方法。 在这里,子类应该继承并重写此方法。

That’s why private methods should have full implementation.

这就是为什么私有方法应具有完整的实现。

  • Private methods must contain body.私有方法必须包含主体。
  • No lesser accessibility than private modifier.可访问性不比private修饰符低。
  • In Java, as we know private is the least visible access modifier. So we cannot reduce it’s visibility from private to any other modifier.

    众所周知,在Java中,private是最不可见的访问修饰符。 因此,我们不能将其可见性从private更改为其他修饰符。

    These interface private methods are useful or accessible only within that interface only. We cannot access or inherit private methods from an interface to another interface or class.

    这些接口专用方法仅在该接口内有用或可访问。 我们不能从一个接口访问或继承私有方法到另一个接口或类。

    为什么在接口中需要私有方法? (Why do we need private methods in Interface?)

    Java 9 private methods in interfaces have following benefits.

    接口中的Java 9私有方法具有以下优点。

    1. No need to write duplicate code, hence more code reusability.无需编写重复的代码,因此具有更高的代码可重用性。
    2. We got the choice to expose only our intended methods implementations to clients.我们可以选择仅向客户公开我们预期的方法实现。

    That’s all about Java 9 private methods in interfaces change.

    这就是有关接口更改中的Java 9私有方法的全部内容。

    Reference: JEP 213: Milling Project Coin

    参考: JEP 213:铣削项目硬币

    翻译自: https://www.journaldev.com/12850/java-9-private-methods-interfaces

    java中接口私有反方

java中接口私有反方_接口中的Java 9私有方法相关推荐

  1. 6.java中什么是类_类、对象(java基础知识六)

    1.Java约定俗成 java约定俗成1,类名接口名 一个单词首字母大写,多个单词每个单词首字母都大写2,方法名和变量名 一个单词全部小写,多个单词从第二个单词首字母大写 建议:如果能用英语尽量用英语 ...

  2. java中审核订单流程图_看95后java妹子横扫阿里,京东,小米,滴滴,美团等大厂,一份热腾腾的面经(最终入职阿里)...

    内容目录 头条 美团 滴滴 京东 others 算法题 HR面 tips 自序 这次面试的公司有一点点多,主要是因为毕业后前两份工作找的都很草率,这次换工作就想着,emm,毕业三年了,该找个工作好好沉 ...

  3. java中字符串的创建_【转载】 Java中String类型的两种创建方式

    本文转载自 https://www.cnblogs.com/fguozhu/articles/2661055.html Java中String是一个特殊的包装类数据有两种创建形式: String s ...

  4. java中二进制怎么说_面试:说说Java中的 volatile 关键词?

    volatile 这个关键字可能很多朋友都听说过,或许也都用过.在 Java 5 之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 Java 5之后,volatile 关 ...

  5. Java中的变量分类_开发简单的Java应用

    第一章 开发简单的Java应用 1.Java语言的技术分类 (1)JavaSE:标准版,开发桌面应用 (2)JavaEE:企业版,Web应用 (3)JavaME:小型版,嵌入式 2.如何开发和运行Ja ...

  6. Java中映射怎么实现_我们如何在Java 9的JShell中实现映射?

    JShell是Java 9中引入的Java Shell工具.它是一个交互式工具,可以读取输入,执行输入并在命令行提示符下打印输出.我们不需要像Java类一样编写一种方法来执行它.main() 我们可以 ...

  7. java中 单目运算符_(2-6)Java语言中,(      )不属于单目运算符

    [其它]1 .撰写分析报告文档 2 .制作汇报 PPT 3 .小组成员互评打分 [简答题]综合利用形状路径工具和笔刷等工具绘制出水墨画效果.插入图片并上传psd源文件 [简答题]画笔作业 [判断题]利 ...

  8. java中nodelist的用法_我可以在Java中使用for-each遍历一个NodeList吗?

    这个问题的解决方法是直截了当的,而且幸运的是你必须实现一次. import java.util.*; import org.w3c.dom.*; public final class XmlUtil ...

  9. java 中定义整形变量_智慧职教: 在Java语言中,一条语句可以定义多个变量。例如int a; b;就定义了两个整型变量a 和b。...

    智慧职教: 在Java语言中,一条语句可以定义多个变量.例如int a: b:就定义了两个整型变量a 和b. 答:0 "物的依赖性关系"是( ) 答:资本主义社会之中的人与人之间的 ...

最新文章

  1. python变量域名_Python实现从url中提取域名的几种方法
  2. osgEarth3.0 加载天地图
  3. 1.4操作系统的变革
  4. 纯 Win32 SDK程序为什么每次要获取设备描述表句柄
  5. Servlet入门篇(GenericServlet 类 - HttpServlet 类 -ServletConfig 接口 - HttpServletRequest 接口……)
  6. 关于使用 ./ 执行sh文件报错-bash: ./startup.sh: /bin/sh^M: bad interpreter: No such file or directory
  7. Linux下gcc编译生成动态链接库*.so文件并调用它
  8. 优达学城深度学习之三(上)——卷积神经网络
  9. java多线程的安全_java-多线程的安全问题
  10. Flutter进阶—质感设计之进度条
  11. php file_get_contents 效率,php 浅析file_get_contents、curl 的效率和稳定性
  12. 苹果或在 WWDC 宣布放弃英特尔转向自研 5nm ARM 芯片,这次时机成熟了?
  13. 旅行商问题(回溯算法)
  14. 编程语言c语言程序包括的几种语句
  15. 数模转换DAC-TLC5615的说明
  16. 仙人掌树学习1:仙人掌图 洛谷:[SHOI2008]仙人掌图 II
  17. 粤嵌实习-linux下madplay播放器的下载和使用、线程的介绍和创建一个广告循环播放线程
  18. idea中设置jdk
  19. react+echarts 实现中国地图
  20. linux医学软件,Schrodinger Suites 2015-2 Linux版简单安装经验分享

热门文章

  1. HDU 3420 Bus Fair [补]
  2. Python生成器 yield
  3. java如何打JAR包
  4. 编程珠玑笔记-第12章习题
  5. 纪念宾得4000万象素的120数码相机645D推出两周年
  6. Win7允许/禁用 PING命令
  7. verilog中generate语句的使用
  8. Element-ui框架Tree树形控件切换高亮显示选中效果
  9. 11 旋转数组的最小数字
  10. 软工实践第八次作业(软件工程实践总结)