首页 > 解决方案 > Java 自定义数据类型

问题描述

我想创建一个具有返回类对象的自定义数据类型的类。考虑一个自定义类:

public class Custom {
    // Some fields.
    public Custom(String custom) {
        // Some Text.
    }
    // Some Methods.
    public void customMethod() {
        // Some Code.
    }
}

现在,考虑第二类 TestCustom:

public class TestCustom {
    public static void main(String[] args) {
        Custom custom = new Custom("Custom");
        System.out.println(custom); // This should print "Custom"
        custom.customMethod(); // This should perform the action
    }
}

因此,问题是如何在实例化对象而不是内存位置时获得自定义值。就像我得到的是:

Custom@279f2327

标签: javaobjectcustom-data-type

解决方案


java.util.Date 类返回当前日期。这可以看作是类的构造函数是

public Date() {
    this(System.currentTimeMillis());
}

例如,以下代码将打印出当前日期:

DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
System.out.println(format.format(date));

推荐阅读