我如何在java中使用setter和getter

时间:2015-08-11 02:17:34

标签: java string int setter getter

我是java的新手,但仍然存在setter和getter的问题。 我真的不了解getter和setter的设置。有人能指出我正确的方向吗?

*This is the output i get:* 
your cards are four & jack 
fourjack

*This is the output im looking for:*
your cards are four & jack
14



String[] cards = {"ace","two","three","four","five","six","seven","eight", "nine", "ten", "jack", "queen", "king"};
String card1 = (cards[new Random().nextInt(cards.length)]);
String card2 = (cards[new Random().nextInt(cards.length)]);
System.out.println("your cards are " + card1() + " & "+ card2);
System.out.println(card1 + card2);
}
public static int getCard1() {
    return getCard1();
}

public static void setCard1(int card1) {
    Player.card1 = card1;
}   
public static int getCard2() {
    return getCard2();
}
**strong text**
public static void setCard2(int card2) {
    Player.card2 = card2;
}
}

2 个答案:

答案 0 :(得分:0)

您应该使用最后一部分中的索引号

String[] cards = {"ace","two","three","four","five","six","seven","eight", "nine", "ten", "jack", "queen", "king"};
int c1 = new Random().nextInt(cards.length);
int c2 = new Random().nextInt(cards.length);
String card1 = (cards[c1]);
String card2 = (cards[c2]);
System.out.println("your cards are " + card1 + " & "+ card2);

c1++;
if (c1>9) c1 = 10;
c2++;
if (c2>9) c2 = 10;

System.out.println(c1+c2);

答案 1 :(得分:0)

封装是使类中的成员变量成为私有并提供访问的基本机制之一 通过公共方法对这些变量,即setter和getter。如果字段声明为私有, 它不能被班级以外的任何人访问,即使不是子班级, 从而隐藏了班级中的字段。出于这个原因,封装也被称为"数据隐藏"。

在这个示例中,您有直接使用的变量,但如果您真的想使用getter和setter,则需要生成/创建它们。我已经更改了你的代码并创建了单独的类来通过getter和setter实现。

class Game {

  private String card1 ;
  private String card2 ;

  private int c1;
  private int c2; 

  String[] cards = {"ace","two","three","four","five","six","seven","eight", "nine", "ten", "jack", "queen", "king"}; 
  int len = cards.length;


    public  String getCard1(){
      return card1;
   }

    public  void setCard1(String cardName1){
      card1 = cardName1;
   }

    public  String getCard2(){
      return card2;
   }

    public void setCard2(String cardName2){
      card2 = cardName2;
   }

    public  int getC1(){
     setC1();
      return c1;
   }

    public void setC1(){
      c1 = new Random().nextInt(len);
   }


    public  int getC2(){
      setC2();
      return c2;
   }

    public void setC2(){
      c2 = new Random().nextInt(len);
   }


    public String getCards(int i) {
        return cards[--i];
    }
}

现在您可以在主类中使用这些getter和setter,如下所示:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class test
{


    public static void main (String[] args) throws java.lang.Exception
    {
    Game game = new Game();
      //get random numbers
    int a1 = game.getC1();
    int a2 = game.getC2();    
    //get the array values
    game.setCard1(game.getCards(a1));
    game.setCard2(game.getCards(a2));

    System.out.println("Your cards are \n1: " + game.getCard1() + " \n2: "+ game.getCard2());
    //sum logic 
    if (a1>9) a1 = 10;
    if (a2>9) a2 = 10;
    //Output
    System.out.println("Sum of you cards is :");
    System.out.print(a1+a2);
    }
}

因此,您可以使用setter和getter构建代码,从而提高代码的可维护性,灵活性和可扩展性。

您可以查看完整代码here。您可以运行此代码here