Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调用其他方法和不可变实例_Java_Immutability - Fatal编程技术网

Java 调用其他方法和不可变实例

Java 调用其他方法和不可变实例,java,immutability,Java,Immutability,这是一道考卷上的题。我已经完成了这个问题,它是有效的。但是,我觉得我的实现可能很弱,例如,我在整个Gregorian类中使用了static 在Gregorian类中,我得到了三种方法,可以用我认为合适的任何方式来编写,每种方法都有一个场景。我在Gregorian类的三个方法上使用static是对的 此外,日、月和年字段是不可变的,是否将它们设置为私有字段就足够了?一旦创建字段值,就不能更改字段值 public class Date { private int day;// needs to b

这是一道考卷上的题。我已经完成了这个问题,它是有效的。但是,我觉得我的实现可能很弱,例如,我在整个Gregorian类中使用了static

在Gregorian类中,我得到了三种方法,可以用我认为合适的任何方式来编写,每种方法都有一个场景。我在Gregorian类的三个方法上使用static是对的

此外,日、月和年字段是不可变的,是否将它们设置为私有字段就足够了?一旦创建字段值,就不能更改字段值

public class Date {

private int day;// needs to be immutable?
private String month;// needs to be immutable?
private int year;// needs to be immutable?

public Date(int theDay, String theMonth, int theYear) {
    this.day = theDay;
    this.month = theMonth;
    this.year = theYear;
}

public int getDay() {
    return day;
}

public String getMonth() {
    return month;
}

public int getYear() {
    return year;
}
}

公共类公历{


}默认情况下,字符串是不可变的

private int day;// needs to be immutable?
private int year;// needs to
不是您定义的不变字段。他们的状态可以改变。让它们成为最终的


注意:将引用设为final并不意味着在您的情况下不能更改对象状态。此注释不相关,因为您没有引用对象。

字符串默认不可变

private int day;// needs to be immutable?
private int year;// needs to
不是您定义的不变字段。他们的状态可以改变。让它们成为最终的


注意:制作引用最终版本并不意味着在您的情况下无法更改对象状态。此注释与此无关,因为您没有引用对象。

我同意ThinkStip的观点-将最终版本添加到字段将有助于避免更改它们。没有二传者会强化这一点

此外,我想指出

private String month;// needs to be immutable?
可以创建为任何形式,从一月到馅饼。如果我可以建议,请将其更改为枚举,并为数月建立允许的值

public enum MonthName {
    JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC;
}
将日期类别更改为以下内容:

private final int day;
private final MonthName month;
private final int year;

public Date(int theDay, MonthName theMonth, int theYear) {
    this.day = theDay;
    this.month = theMonth;
    this.year = theYear;
}

我同意ThinkStiple的观点——在字段中添加final将有助于防止字段被更改。没有二传者会强化这一点

此外,我想指出

private String month;// needs to be immutable?
可以创建为任何形式,从一月到馅饼。如果我可以建议,请将其更改为枚举,并为数月建立允许的值

public enum MonthName {
    JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC;
}
将日期类别更改为以下内容:

private final int day;
private final MonthName month;
private final int year;

public Date(int theDay, MonthName theMonth, int theYear) {
    this.day = theDay;
    this.month = theMonth;
    this.year = theYear;
}
day和year都是原语,并且已经有不可变的int版本可用,它是整数,您可以利用它

第二件事是,在Gregorian中有一个对Date的静态引用,将Date作为参数传递给每个静态方法。您可以确保线程的安全性。

日期和年份都是原语,并且已经有不可变的int版本可用,您可以使用它


第二件事是,在Gregorian中有一个对Date的静态引用,将Date作为参数传递给每个静态方法。然后可以确保线程安全。

而不是静态方法,静态字段d使它们都是非静态的

public class Gregorian {
    private final Date d;

    public Gregorian(Date d_) {
        this.d = d_;
    }

    public boolean isLeapyear() {
        ... // implemented as above
    }

    ... // Other methods as above, but all non-static.
}
主要内容如下:

public static void main (String[] args){
    Date d = new Date(9, "June", 8);
    Gregorian g = new Gregorian(d);
    System.out.println(g.getGregorianDateNumber());
    System.out.println(g.getISO8601Date());
    System.out.println(g.leapYear());
}

而不是静态方法和静态字段d使它们都是非静态的

public class Gregorian {
    private final Date d;

    public Gregorian(Date d_) {
        this.d = d_;
    }

    public boolean isLeapyear() {
        ... // implemented as above
    }

    ... // Other methods as above, but all non-static.
}
主要内容如下:

public static void main (String[] args){
    Date d = new Date(9, "June", 8);
    Gregorian g = new Gregorian(d);
    System.out.println(g.getGregorianDateNumber());
    System.out.println(g.getISO8601Date());
    System.out.println(g.leapYear());
}

这更像是一个代码审查的问题。这更像是一个代码审查的问题。我是否也需要使该类成为最终类?如果您不希望其他类扩展您的类,您可以这样做。顺便问一下,为什么要用Date作为类名?Java已经有一个名为Date的类。我是否也需要使该类成为最终类?如果不希望其他类扩展您的类,您可以这样做。顺便问一下,为什么要用Date作为类名?Java已经有一个名为Date的类。作为额外的预防措施,建立允许的值是有意义的。谢谢。作为额外的预防措施,建立允许值是有意义的。谢谢你。