在java中如何使用getter和setter方法?

时间:2016-12-11 02:31:26

标签: java getter-setter

我正在尝试在此程序中使用getter和setter方法,但仍然无法编译该文件。

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

  String nane ="";
   int Gold = 0;
   int Silver = 0;
   int Bronze = 0;
   int total = 0;


   int totalG = 0;
   int totalS = 0;
   int totalB = 0;
   int sumofall = 0;

   country c1 = new country();


   c1 = setcountryname( "United Kindom");
   c1 = setcountryGold( 27);
   c1 = setcountrySilver( 23);
   c1 = setcountryBronze( 17);
   c1 = setcountryTotal( 67);




   String messagetoprint = "";
   String choosemedals = "";

  int totalGold = calculateG(c1,totalG, Gold);
  int totalSilver = calculateS(c1, totalS, Silver);
  int totalBronze = calculateB(c1,totalB, Bronze);
  sumofall = calculateall(totalGold,totalSilver,totalBronze,sumofall);


   System.out.println("                 G   S   B   Total");
   System.out.println(" Great Britain "+totalGold+"  " +totalSilver +"  "+totalBronze +"  "+ sumofall);


  String which = inputString ("which medal total do you want to know? (Gold, Silver or Bronze)"); 
  w(which,totalGold,totalSilver,totalBronze);


  String again = inputString ("DO you want to see the data again? (Yes or No)"); 
  a(again,totalGold,totalSilver,totalBronze,sumofall);

  String oldnew = inputString ("What do you want to see old or new?"); 
  o(oldnew,Gold,Silver,Bronze,totalGold, totalSilver, totalBronze,total,sumofall);

System.exit(0);
}



 public static int calculateG(country c1,int totalG,int Gold)
 {
    Gold = getcountryGold(c1);
    int G = inputInt(" How many people won a Gold ?");
    totalG = Gold + G;
    return totalG;   
    }


public static int calculateS(country c1, int totalS, int Silver)
{   
    Silver = getcountrySilver(c1);
   int S = inputInt(" How many people won a silver ?");  
    totalS = Silver + S;
    return totalS;
 }

 public static int calculateB(country c1,int totalB, int Bronze)
{
     Bronze = getcountryBronze(c1);
    int B = inputInt(" How many people won a bronz ?");
    totalB = Bronze + B;
    return totalB;
}   

  public static int calculateall(int totalGold,int totalSilver,int totalBronze, int sumofall)
{
    int k = 0;
    k = totalGold + totalSilver + totalBronze;
    sumofall = k;
    return  sumofall;   
   }


public static void w(String which, int totalGold, int totalSilver, int totalBronze)
{
    if (which.equals("Gold") ||which.equals("gold"))
    {
        System.out.println(totalGold);

    }
    else if(which.equals("Silver") || which.equals("silver"))
    {
        System.out.println(totalSilver);
    }
    else if(which.equals("Bronze")  ||which.equals("bronze"))
    {
        System.out.println(totalSilver);
    }
    else 
    {
        System.out.println(" Can you check spelling?");
        System.exit(0);
    }


}
public static String inputString(String message)
{
   Scanner scanner = new Scanner(System.in);
   String answer;

   System.out.println(message);
   answer = scanner.nextLine();

   return answer;
} 

public static void a(String again, int totalGold, int totalSilver, int totalBronze, int sumofall)
{
    if (again.equals("Yes") || again.equals("yes"))
    {
       System.out.println("                 G   S   B   Total");
       System.out.println(" Great Britain "+totalGold+"  " +totalSilver +"  "+totalBronze +"  "+ sumofall);

    }
    else if(again.equals("No") || again.equals("no"))
    {
        System.out.println("This is your choose");
    }

    else 
    {
        System.out.println(" Can you check spelling?");
        System.exit(0);
    }


    }
      public static void o (String oldnew, int Gold, int Silver, int    Bronze,int totalGold, int totalSilver, int totalBronze, int sumofall, int total)
    {
        if (oldnew.equals("Old") || oldnew.equals("old"))
        {
          int tt = Gold+Silver+Bronze;
           System.out.println("                 G   S   B   Total");
           System.out.println(" Great Britain "+Gold+"  " +Silver    +"  "+Bronze +"  "+ tt);

        }
        else if(oldnew.equals("New") || oldnew.equals("new"))
        {
           int newtotal = total-sumofall; 
           int gg = totalGold-Gold;
          int ss = totalSilver-Silver;
           int bb = totalBronze-Bronze;
           System.out.println("                 G   S   B   Total");
          System.out.println(" Great Britain "+gg+"  " +ss +"  "+bb +"  "+ newtotal);
    }

        else 
     {
       System.out.println(" Can you check spelling?");
            System.exit(0);
    }


    }


    public static int inputInt(String message)
   {
        return Integer.parseInt(input(message));
     }




      public static String input(String message)
   {
      Scanner scanner =new Scanner(System.in);
        String answer;

       System.out.println(message);
       answer = scanner.nextLine();

       return answer;
   }

       public static void print(String message)
    { 
       System.out.println(message);

    }

     public static String getcountryname (country c1)
    {
       return  countryname;
 }    
      public static int getcountryGold (country c1)
      {
        return  countryGold;
   }    
      public static int getcountrySilver ( country c1)
    {
       return  countrySilver;
 }    
 public static int getcountryBronze ( country c1)
{
    return  countryBronze;
     }    


    public static country setcountryname (  String newname)
   {
      countryname = newname;
       return countryname;
    }
     public static country setcountryGold ( int newGold)
     {
    countryGold = newGold;
     return countryGold;
    }   public static country setcountrySilver ( int newSilver)
    {
         countrySilver = newSilver;
        return countrySilver;
      }   public static country setcountryBronze (  int newBronze)
   {
      countryBronze = newBronze;
     return countryBronze;
   }
    public static country setcountryTotal ( int newTotal)
    {
     countryTotal = newTotal;
     return countryTotal;
   }

   } 
class country
{
    String countryname;
    String countryGold;
    String countrySilver;
    String countryBronze;
    String countryTotal;

       }   

编译之后它有多个错误,这里我想问一下我的getter和setter方法有什么问题,我怎样才能正确使用getter和setter方法?

1 个答案:

答案 0 :(得分:0)

您不需要执行以下操作:

  • 不要使getter,setter方法静态
  • 通常我们不会将任何参数传递给getter方法
  • 一般情况下,我们不会从setter方法返回任何内容(如果您需要,那么您可以这样做)

按如下方式修改Country课程:

class Country {

    private String countryname;
    private int countryGold;
    private int countrySilver;
    private int countryBronze;
    private int countryTotal;

    public String getcountryname() {
        return countryname;
    }

    public int getcountryGold() {
        return countryGold;
    }

    public int getcountrySilver() {
        return countrySilver;
    }

    public int getcountryBronze() {
        return countryBronze;
    }

    public int getcountryTotal() {
        return countryTotal;
    }

    public void setcountryname(String newname) {
        this.countryname = newname;
    }

    public void setcountryGold(int newGold) {
        this.countryGold = newGold;
    }

    public void setcountrySilver(int newSilver) {
        this.countrySilver = newSilver;
    }

    public void setcountryBronze(int newBronze) {
        this.countryBronze = newBronze;
    }

    public void setcountryTotal(int newTotal) {
        this.countryTotal = newTotal;
    }

}

您可以编写如下方法来返回countryTotal,而不是为countryTotal保留其他字段。在这种情况下,您不需要setter方法 - setcountryTotal

public int getcountryTotal() {
    return countryGold + countrySilver + countryBronze;
}

感谢@DavidWallace的评论。我根据他的建议更新了我的答案。