java - 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
解决方案
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));
推荐阅读
- android - 如何让功能更简单
- jmeter - Unable to access jar file for jmeter
- java - Memory leak while using mysql-java-drive and spring-jdbc
- c# - 为什么C#的十进制使用二进制整数有效位?
- c++ - Print integer from array A based on array B integer?
- javascript - 无法在 Python 中使用 Selenium 单击按钮(JavaScript 也不起作用)
- javascript - 如何在html中使用href隐藏和显示元素
- python - 如何在熊猫中使用条件执行多个分组和转换计数
- firebase - 无法在颤振中安装任何 Firebase 软件包
- html - 如何发送图像和 HTML 表单以供 python AI 分析?