equation2.java

来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 59 行

JAVA
59
字号
package examples.inner;

/** A class definition to explore the use of local
  * inner classes
  */
public class Equation2 {

   private int equationInput;

   /** An interface defining the result from an
     * equation
     */
   public interface Result {
      public double getAnswer();
   }

   /** Constructor method
     * @param ei the equation input
     */
   public Equation2( int ei ) {
      equationInput = ei;
   }

   /** Create the result of the equation for the given
     * input values
     * @param input1 the first equation input
     * @param input2 the second equation input
     * @return the result object
     */
   public Result getResult( final int input1,
                            final int input2 ) {
      final int[] localVar = { 2,6,10,14};
      return new Result() {

         private int normalField;

         public double getAnswer() {
            return (double) input1/input2
                            - equationInput
                            + localVar[2]
                            - normalField;
         }

         // this is an instance initializer block
         {
            normalField = 2;
         }
      };
   }

   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      Equation2 e = new Equation2( 10 );
      Result r = e.getResult( 33, 5 );
      System.out.println( r.getAnswer() );
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?