gpt4 book ai didi

java - 如果它们都在同一个包中,是否可以在一个 java 文件中声明数组并在不同的 java 文件中使用它们

转载 作者:行者123 更新时间:2023-11-29 07:12:45 27 4
gpt4 key购买 nike

那么你如何将它与完全不同的 java 文件一起使用,但在同一个包中

public static void main (int[] args)
{
int [] HotDog = {18,8,10,0};
int [] ToastedChicken = {25,8,17,0};
int [] ToastedSteak = {30,8,22,0};
int [] ToastedEggT= {20,8,6,6};
int [] ToastedSteakE={36,8,22,6};
int [] ChickenRoll = {25,8,17,0};
int [] SteakRoll = {30,8,22,0};
int [] EggTomato = {20,8,6,6};
int [] CheeseTomato = {20,8,6,6};
int [] steakEgg = {36,8,22,6};`

IE在这里

if (contents ==  "Hot Dog")

{jLabel2.setText(HotDog[2]); }

最佳答案

您需要将它们设为静态。例如:

final class MyConstants {
static final int[] HotDog = {18, 8, 10, 0};
static final int[] ToastedChicken = {25, 8, 17, 0};
static final int[] ToastedSteak = {30, 8, 22, 0};
static final int[] ToastedEggT = {20, 8, 6, 6};
static final int[] ToastedSteakE ={36, 8, 22, 6};
static final int[] ChickenRoll = {25, 8, 17, 0};
static final int[] SteakRoll = {30, 8, 22, 0};
static final int[] EggTomato = {20, 8, 6, 6};
static final int[] CheeseTomato = {20, 8, 6, 6};
static final int[] SteakEgg = {36, 8, 22, 6};

private MyConstants() {
// Prevents users of this class from instantiating a useless instance of
// this class, since all members are static.
}
}

然后您可以在同一个包中的任何类中使用它们,例如:

if (contents.equals("Hot Dog")) {
jLabel2.setText(MyConstants.HotDog[2]);
}

关于java - 如果它们都在同一个包中,是否可以在一个 java 文件中声明数组并在不同的 java 文件中使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12287736/

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