简体   繁体   English

Java 自定义数据类型

[英]Java Custom Data Type

I wanted to create a class with a custom data type that returns the class object.我想创建一个具有自定义数据类型的类,该类返回类对象。 Consider a class Custom:考虑一个类 Custom:

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

Now, consider a second class TestCustom:现在,考虑第二个类 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
    }
}

So, the question how to get the value custom on instantiating an object instead of memory location.因此,问题是如何在实例化对象而不是内存位置时获得自定义值。 Like what I get is:就像我得到的是:

Custom@279f2327

The java.util.Date class returns the current date. java.util.Date 类返回当前日期。 This can be seen as the constructor for the class is这可以看作是类的构造函数是

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

For example, the following code would print out the current date:例如,以下代码将打印出当前日期:

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

The Answer by ML72 is correct and should be accepted. ML72回答是正确的,应该被接受。 The java.util.Date constructor captures the current moment in UTC. java.util.Date构造函数以UTC java.util.Date捕获当前时刻。

java.time时间

The java.util.Date class is terrible , for many reasons. java.util.Date类很糟糕,原因有很多。 That class is now legacy, supplanted years ago but the java.time classes as of the adoption of JSR 310.该类现在是遗留的,多年前被取代,但在采用 JSR 310 时是java.time类。

The java.time classes avoid constructors, instead using factory methods. java.time类避免使用构造函数,而是使用工厂方法。

The replacement for java.util.Date is java.time.Instant . java.util.Date的替代品是java.time.Instant To capture the current moment in UTC, call the class method .now() .要捕获 UTC 中的当前时刻,请调用类方法.now()

Instant instant = Instant.now() ;

If you want the current moment as seen through the wall-clock time used by the people of a particular region (a time zone), use ZoneId to get a ZonedDateTime object.如果您希望通过特定地区(时区)的人们使用的挂钟时间看到当前时刻,请使用ZoneId获取ZonedDateTime对象。 Notice again the factory method rather than a constructor.再次注意工厂方法而不是构造函数。

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Adjust to UTC by extracting an Instant .通过提取Instant来调整到 UTC。

Instant instant = zdt.toInstant() ;

Override the toString() method, as it is automatically invoked when you try to display an object:覆盖toString()方法,因为它会在您尝试显示对象时自动调用:

Add a field.添加一个字段。 For example;例如;

private String value;

In the constructor, add the following code:在构造函数中,添加以下代码:

value = custom;

this will assign a value passed to the constructor as a parameter, to the value field.这会将作为参数传递给构造函数的值分配给value字段。

And finally override the toString() method as follows:最后重写 toString() 方法如下:

@Override
public String toString() {
    return value;
}

Now, when you display the value of the custom object, the overridden toString() method will be invoked and the argument will be displayed instead of the memory address.现在,当您显示自定义对象的值时,将调用重写的toString()方法并显示参数而不是内存地址。 Whereas methods of the object will work as they are programmed to work.而对象的方法将按照编程的方式工作。 There is nothing to be changed with them.他们没有什么可以改变的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM