Java Hello

来自牛奶河Wiki
阿奔讨论 | 贡献2024年1月24日 (三) 10:23的版本
跳到导航 跳到搜索

First Java program, test Java class, encapsulation, Inheritance.

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);

        System.out.println("==== 封装 ====");
        test3.setVer(2.1);
        test3.setInfo("This is a test setInfo.");
        System.out.println(test3.getVer());
        System.out.println(test3.getInfo());
    }
}


IUdfHello.java

package udf;

public interface IUdfHello {
    public void setVer(double ver);
    public void setInfo(String info);
    public double getVer();
    public String getInfo();
    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("");
    }

    public void setVer(double ver) {
        Version = ver > Version ? ver : Version;
    }
    public void setInfo(String info) {
        INFO = info;
    }
    public double getVer() {
        return Version;
    }
    public String getInfo() {
        return INFO;
    }
}