java中接口私有反方_接口中的Java 9私有方法

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 Interface

    Java 7接口
  2. Java 8 Interface Changes

    Java 8接口更改
  3. Java 9 Interface Changes

    Java 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 example
      void logInfo(String message);

      // default method example
      default 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 example
      static 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中接口私有反方

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值