gpt4 book ai didi

java - Classname.Variable 使用 Classname 作为变量

转载 作者:行者123 更新时间:2023-12-01 23:07:59 28 4
gpt4 key购买 nike

根据另一个类中给定的输入,此类必须从要在其中找到该变量的 4 个类之一检索变量。例如。如果输入是 s,则此类要查找的变量可在 Sumup.counter 中找到,而当输入是 a 时,它需要的变量可以在 Abstract.counter 等中找到,以便填充数组。在这 4 个不同的类别之一中,它必须选取 5 个不同的变量。因此,为了避免大量额外的编程,我想用变量替换这些类名 SumupAbstract 。因此它必须看起来像 Variable.counterVariable.part1 等。其中 Variable 可以是 Sumup摘要(或)。

有人建议如何做到这一点吗?

public class Erroranalysis {

public static int[][] wrongcalculations = new int[5][Numberofcalculations.numcalc];

static int runner = 0;
static String routine;

public static void main(String[] args)
{
switch (kindofcalculation.choice)
{
case "s":
routine = "Sumup";
break;

case "a":
routine = "Abstract";
break;

case "m":
routine = "Multiply";
break;

case "d":
routine = "Divide";
break;
}

if (routine.counter <= Numberofcalcluations.numcalc)
{

wrongcalculations[0][runner] = routine.counter;
wrongcalculations[1][runner] = routine.part1;
wrongcalculations[2][runner] = routine.part2;
wrongcalculations[3][runner] = routine.result;
wrongcalculations[4][runner] = routine.answer;


runner++;
}
}
}

最佳答案

您可以选择一个基本抽象类并创建该类的数组:

class Base{
public int counter = 0;
}
class Sumup extends Base{
}
class Abstract extends Base{
}


public class Erroranalysis {

public static int[][] wrongcalculations = new int[5][Numberofcalculations.numcalc];

static int runner = 0;
static Base routine;
static Sumup sumup;
static Abstract abs;

public static void main(String[] args)
{
sumup = new Sumup();
abs = new Abstract();

// possibly, this part won't be in main, but in some for loop of another method
switch (kindofcalculation.choice)
{
case "s":
routine = sumup;

case "a":
routine = abs;

}

if (routine.counter <= Numberofcalcluations.numcalc)
{

wrongcalculations[0][runner] = routine.counter;
wrongcalculations[1][runner] = routine.part1;
wrongcalculations[2][runner] = routine.part2;
wrongcalculations[3][runner] = routine.result;
wrongcalculations[4][runner] = routine.answer;

runner++;
}
}
}

关于java - Classname.Variable 使用 Classname 作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58392041/

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