【问题标题】:Why we cannot use synchronized keyword in an interface method declaration [duplicate]为什么我们不能在接口方法声明中使用同步关键字[重复]
【发布时间】:2015-09-05 06:16:56
【问题描述】:

谁能通过给出一个真实世界的例子来解释为什么我们不能在接口中声明一个同步方法。

【问题讨论】:

    标签: java synchronized


    【解决方案1】:

    答案很简单,同步是一个实现细节,它不属于接口。在一个接口中,所有方法都没有实现细节。

    文档说:

    注意,接口中声明的方法一定不能声明 strictfp 或 native 或 synchronized,或发生编译时错误, 因为这些关键字描述了实现属性而不是 接口属性。但是,在接口中声明的方法可能 由声明为 strictfp 或 native 的方法实现,或 在实现接口的类中同步。

    您也可以参考:What is the reason why “synchronized” is not allowed in Java 8 interface methods?

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 2013-01-01
      • 2023-03-23
      • 2013-03-16
      • 1970-01-01
      相关资源
      最近更新 更多