簡體   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