gpt4 book ai didi

java - 找到枚举大小的更好方法是什么?

转载 作者:行者123 更新时间:2023-11-29 09:42:10 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Enum values().length vs private field

我想知道这些查找枚举大小的方法中哪一种更好。

这样:

public enum Company {

AUDI(4), BMW(5), CADILLAC(11), FORD(44), JAGUAR(45);

private final int id;
public final int length = Company.values().length;

private Company(int id) {
this.id = id;
}
}

或者这样:

public enum Company {

AUDI(4), BMW(5), CADILLAC(11), FORD(44), JAGUAR(45);

private final int id;
public final int length = 5;

private Company(int id) {
this.id = id;
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com