繁体   English   中英

如何在 Java 类中更改变量

[英]How do I change a Variable, in a Java Class

我必须能够在我的班级中转换一些变量。 我有一个布尔变量 WaGa(代表工作站/游戏计算机),如果它是真的,我想转换 String WorGam

我必须通过服务和支持方法来做到这一点,我一直在努力,但我经常失败。 它只是打印出驱动程序中的内容。 帮助。

public class Graphics
//instance data
{
    private int Ram; 
    private String Brand;
    private int Res;
    private int BiWi;
    private int BaCl;
    private boolean K4;
    private boolean WaGa;
    private String WorGam;

    //boolean WaGa,  boolean K4, int BaCl, int BiWi, int Res,  String Brand, int Ram


    public  Graphics (int R, String B, int Re, int Bi, int Ba, boolean K4, boolean Wa, String Wor )     // constructor
    {
        Ram = R;
        Brand = B;
        Res = Re;
        BiWi = Bi;
        BaCl = Ba;
        K4 = K4;
        WaGa = Wa;
        Wor = WorGam;
    }

    public int get_Ram() //Accessor Method - there are 3 of them
    {
        return Ram;
    }

    public String get_Brand() //Accessor Method - there are 3 of them
    {
        return Brand;
    }

    public int get_Res() //Accessor Method - there are 3 of them
    {
        return Res;
    }

    public int get_BiWi() //Accessor Method - there are 3 of them
    {
        return BiWi;
    }

    public int get_BaCl()
    {
        return BaCl;
    }

    public boolean get_K4()
    {
        return K4;
    }

    public String WorGam(boolean WaGa)
    {
        String WorGam;
        if ( WaGa == true) {
             return WorGam = "Workstation";
        } else {
            return WorGam = "True";
        }
    }

    public String toString()
    {
        return ("Ram" + " " + Ram + ". " + "Brand:" + " " + Brand + ". " + "Resolution" + " " + Res + ". " + "Processer" + " " + BiWi + "." + " " + "Base Clock" + " " + BaCl+ " " + "K4?" + " " + K4+ " " +WorGam);
    }
}

public class Graphicse_Driver
{
    public static void main(String [] args)
{

Graphics unique=new Graphics(4, "Nvinda", 6, 7, 9, false, false, "sdf"  );

System.out.println(unique);

您需要更正您的worGam()函数:

public String worGam(boolean waGa) {
    if (waGa == true) 
        return "Workstation";
    else
        return "True";
}

main()函数:

public static void main(String [] args) {

    Graphics unique = new Graphics(4, "Nnn", 6, 7, 9, false, false, "xxx");

    System.out.println(unique.WorGam(false));
}

您可能需要重新阅读代码以确保代码中没有任何其他错误,但这是问题的根源。

为了访问WarGam getter,您需要调用:

System.out.println(unique.WarGam());

当您执行System.out.println(unique) ,您试图打印出整个Graphics对象,而不仅仅是WarGam字符串。

然后,您应该将WarGam()方法更改为如下所示:

public String WorGam()
{
    if (WaGa) {
        return "Workstation";
    }

   return "Gaming";
}

以下是对这些变化的更深入的解释:

  1. WaGaGraphics类的私有变量。 由于WarGam()方法在同一个Graphics类中,它已经可以访问WaGa变量,因此您不需要传入它。
  2. if(WaGa == true)只是if(WaGa)一种写法。
  3. 您可以直接返回所需的字符串,而不是创建String WorGam变量。
  4. 围绕第二个returnelse是不必要的,因为只有在跳过第一个return才会命中该代码。

在这些更改之后, private String WarGam变量也确实没有必要了。

public String worGam(boolean waGa) {
    if (waGa) 
        return "Workstation";
    else
        return "Gaming";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM