【问题标题】:How to use strictfp keyword in methods?如何在方法中使用 strictfp 关键字?
【发布时间】:2015-02-13 05:38:09
【问题描述】:

我知道strictfp关键字可以应用于方法、类和接口。

strictfp class A{} //Accept

strictfp interface M{} //Accept  

class B{  
strictfp A(){} // Not Accept
}  

【问题讨论】:

  • strictfp 关键字不能应用于抽象方法、变量或构造函数。

标签: java strictfp


【解决方案1】:

它也可以用于方法。但不是在构造函数上。在您的示例中,您已将其应用于构造函数而不是方法。

但是,一旦应用于类,它将自动应用于静态和实例块、所有类变量、所有构造函数

【讨论】:

    【解决方案2】:

    如果您将 strictfp 应用于类,则它适用于所有成员。 正如您在 javadoc 中看到的:

    strictfp 修饰符的作用是使类声明中的所有浮点或双精度表达式(包括变量初始化器、实例初始化器、静态初始化器和构造器中)显式地为 FP-strict

    这意味着类中声明的所有方法,以及类中声明的所有嵌套类型,都是隐式的 strictfp

    strictfp keyword usage

    注意:strictfp 关键字不能显式应用于抽象方法、变量或构造函数。

    【讨论】:

      【解决方案3】:

      您可以显式声明一个类 strictfp 和该类中的方法显式 strictfp 但不能显式声明成员变量,例如

      // Compiles Fine
      strictfp class A {strictfp void mA(){ } int i=0; } 
      
      // Compiler Error because of declaring i variable strictfp 
      strictfp class B {strictfp void mB(){ } strictfp int i=0;}
      
      // Compiler Error because of declaring i variable strictfp 
      class C {strictfp void mC(){ } strictfp int i=0; }
      

      接口

      您不能在声明接口 strictfp 和该接口中的方法/成员显式 strictfp 因为它会产生编译器错误:

      // Compiler Error
      strictfp interface A {strictfp double mA(); static int i = 0;} 
      

      解决方案:声明接口 strictfp 而不是接口中的方法/成员。通过这样做,接口中的所有方法和成员都将是 strictfp

      //Compiles Fine
      strictfp interface A {double mA(); static int i = 0;} 
      

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2012-07-12
        • 1970-01-01
        • 1970-01-01
        • 2017-01-12
        • 2017-01-08
        • 1970-01-01
        相关资源
        最近更新 更多