好的,所以我现在正在编码一个程序,该程序从JTextField获取信息,并将其乘以一定数量,具体取决于使用该按钮设置的类中double设置的值。单击该按钮后,我想从分配给它的类文件中检索一个int。但我想将它们普遍分配。这真的很难解释,但是基本上现在我必须对它们全部进行硬编码。这是当前代码:

public void actionPerformed(ActionEvent e)
{
    System.out.println("Clicked: " + getButtonName());

    if (Settings.SelectedChamp == "Aatrox") {

        TestFrame.PNameField.setText(ChampionScales.PNameAatrox);
        TestFrame.QNameField.setText(ChampionScales.QNameAatrox);
        try {
            int QMath = (int) Double.parseDouble(TestFrame.AttackDamageField.getText());
            System.out.println(ChampionScales.QBaseDamageAatrox[0] + "/"
                    + ChampionScales.QBaseDamageAatrox[1] + "/"
                    + ChampionScales.QBaseDamageAatrox[2] + "/"
                    + ChampionScales.QBaseDamageAatrox[3] + "/"
                    + ChampionScales.QBaseDamageAatrox[4] + "(+" + QMath
                    * 0.6 + ")");
        } catch (Exception e1) {
            int QMath = 0;
            System.out.println(ChampionScales.QBaseDamageAatrox[0] + "/"
                    + ChampionScales.QBaseDamageAatrox[1] + "/"
                    + ChampionScales.QBaseDamageAatrox[2] + "/"
                    + ChampionScales.QBaseDamageAatrox[3] + "/"
                    + ChampionScales.QBaseDamageAatrox[4] + "(+" + QMath
                    * 0.6 + ")");
        }
    }
}


在这里,我这样做的目的是,如果您单击Aatrox的按钮,它将进行数学运算并将其全部输入到控制台中。但是我也为这些int设置了整个课程。在这里:

public class Aatrox
{
    public static String PName = "Blood Well";

    public String QName = "Dark Flight";
    public int QBaseDamage[] = { 70, 115, 160, 205, 250 };
    public double QScalingAD = .6;
    public double QScalingAP = 0;
    public String QADDamageStyle = "Bonus";

    public String WName = "Blood Thirst";
    public int WBaseDamage[] = { 20, 25, 30, 35, 40 };
    public double WScalingAD = 1;
    public double WScalingAP = 0;
    public String WADDamageStyle = "Bonus";

    public String W2Name = "Blood Price";
    public int W2BaseDamage[] = { 60, 95, 130, 165, 200 };
    public double W2ScalingAD = 1;
    public double W2ScalingAP = 0;
    public String W2ADDamageStyle = "Bonus";

    public String EName = "Blades of Torment";
    public int EBaseDamage[] = { 75, 110, 145, 180, 215 };
    public double EScalingAD = .6;
    public double EScalingAP = .6;
    public String EADDamageStyle = "Bonus";

    public String RName = "Massacre";
    public int RBaseDamage[] = { 200, 300, 400 };
    public double RScalingAD = 0;
    public double RScalingAP = 1;
    public String RADDamageStyle = null;

}


因此,我希望能够为每个冠军创建一个这样的文件,并保留所有相同的名称。问题是..我该如何称呼冠军的积分和东西而不必硬编码Aatrox.QScalingAD(); or Teemo.EScalingAP();?我怎么能通用呢?谢谢您的帮助。我被难住了哈哈。 `

最佳答案

    You make a base class which you fill with your common stuff:

class BaseClass
{
    public static String PName = "Blood Well";

    public String QName = "Dark Flight";
    public int QBaseDamage[] = { 70, 115, 160, 205, 250 };
    public double QScalingAD = .6;
    public double QScalingAP = 0;
    public String QADDamageStyle = "Bonus";

    public String WName = "Blood Thirst";
    public int WBaseDamage[] = { 20, 25, 30, 35, 40 };
    public double WScalingAD = 1;
    public double WScalingAP = 0;
    public String WADDamageStyle = "Bonus";

    public String W2Name = "Blood Price";
    public int W2BaseDamage[] = { 60, 95, 130, 165, 200 };
    public double W2ScalingAD = 1;
    public double W2ScalingAP = 0;
    public String W2ADDamageStyle = "Bonus";

    public String EName = "Blades of Torment";
    public int EBaseDamage[] = { 75, 110, 145, 180, 215 };
    public double EScalingAD = .6;
    public double EScalingAP = .6;
    public String EADDamageStyle = "Bonus";

    public String RName = "Massacre";
    public int RBaseDamage[] = { 200, 300, 400 };
    public double RScalingAD = 0;
    public double RScalingAP = 1;
    public String RADDamageStyle = null;
}

class Aatrox extends BaseClass
{
    //Do Aatrox-specific stuff here!
}

class xyz extends BaseClass
{
    //Do xyz-specific stuff here
}


通过“扩展”,Aatrox和xyz都具有类BaseClass的所有功能。

09-07 02:42