Java Hello:修订间差异

来自牛奶河Wiki
跳到导航 跳到搜索
(创建页面,内容为“JavaHello.java * udf/IUdfHello.java * udf/UdfHello.java 编译 javac JavaHello.java 执行 java JavaHello ====JavaHello.java==== import udf.UdfHello; public class JavaHello extends UdfHello{ public static final String INFO = "This is a test."; public static void main(String args[]) { JavaHello test1 = new JavaHello(); test1.info(); UdfHello test2 = new UdfHello("Hello, Java!", 1.1); test2.info();…”)
 
无编辑摘要
第89行: 第89行:
     }
     }
  }
  }
[[分类:Develop]]
[[分类:Java]]

2024年1月23日 (二) 15:22的版本

JavaHello.java

  • udf/IUdfHello.java
  • udf/UdfHello.java

编译

javac JavaHello.java

执行

java JavaHello


JavaHello.java

import udf.UdfHello;

public class JavaHello extends UdfHello{
    public static final String INFO = "This is a test.";

    public static void main(String args[]) {
        JavaHello test1 = new JavaHello();
        test1.info();
        UdfHello test2 = new UdfHello("Hello, Java!", 1.1);
        test2.info();
        UdfHello test3 = new UdfHello("Hello!", 0.1);
        test3.info();

        System.out.println(test1.INFO);
        System.out.println(test3.VERSION);
    }
}

IUdfHello.java

package udf;

public interface IUdfHello {
    public void info();
}

UdfHello.java

package udf;

/*
------------------------------------------------------------------------------
  Name     : UdfHello
  Purpose  : Java Sample
  Author   : Adam
  Revisions:
  Ver        Date        Author           Description
  ---------  ----------  ---------------  ------------------------------------
  1.0        2024/1/22   Adam             My first Java program.
  1.1        2024/1/22   Adam             M01: comments & Spaces/CR/LF of code do not affect the compilation result

 format:
    object  : (), (string, double)
    property: VERSION
    method  : info
------------------------------------------------------------------------------
*/
public class UdfHello implements IUdfHello {
    // private static final String Version = "v1.0";
    public static final String VERSION = "v1.0";
    private double Version = 1.0;
    private String INFO;

    // constructor with no parameter
    public UdfHello() {
        INFO = "Hello, World!";
    }

    // constructor & parameter
    public UdfHello(String info, double ver) {
        System.out.println("Variable: info=" + info + ", ver=" + ver);
        try {
            INFO = info;
            Version = ver > Version ? ver : Version;
            // Error: error: cannot assign a value to final variable VERSION  // M01:
            //  VERSION = "v1.1";
        }
        catch(Exception e) {
            System.out.println(e);
        }

    }

    public void info() {
        System.out.println("VERSION: " + VERSION);
        System.out.println("Ver: v"    + Version);      // M01:
        System.out.println(INFO);
        System.out.println("");
    }
}