gpt4 book ai didi

java - 如何从静态方法轻松访问类的各种实例中的变量

转载 作者:行者123 更新时间:2023-11-30 04:05:10 24 4
gpt4 key购买 nike

我一直在研究纸牌游戏Gin Rummy的基本模拟,虽然目前还不完整,但在我尝试之前它能够运行一副纸牌的基本功能实现一种向每个玩家发牌的方法。

Player应该由一个ArrayList(它保存每个玩家手中的牌)和一个integer(用作玩家 ID)。我已将它们声明为 static 对象,但这当然意味着我创建的每个 Player 实例现在都具有相同的 Handplayerid 因为变量是静态的。

这就是我当时面临的问题。我相信任何static类或方法只能访问其他static类、方法对象等。因为main方法必须是static,它可以访问的任何东西肯定也必须是static等等?我的 Player 类中的对象如何不是静态的,从而允许我为 Player 的每个实例分配不同的 Hand玩家ID

我在下面包含了我的代码,很抱歉它太长了,我想我应该把所有内容都放在以防万一。郑重声明,正是在 PlayGinRummy 类中我调用 Player 新实例的点引发了错误 non-static variable Hand Can not be referenced from静态上下文

package labexercise1;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class DeckOfCardsTest {

public DeckOfCardsTest(){}

private static int Menu;

public static void main(String[] args) throws InterruptedException{

DeckOfCards.FillDeck();

do{
Thread.sleep(1000);
System.out.println("Please select from the following menu options using the corresponding number:");
System.out.println("1. Display Deck | 2. Shuffle Deck | 3. Cut Deck | 4. Start Game | 5. Exit"); //Start game will lead into another switch menu to set no of players etc.
System.out.print("Your choice: ");
Scanner UserInput = new Scanner(System.in);
System.out.println();
Menu = UserInput.nextInt(); // Maybe build in a system that recognises, for example, "1", "one" and "One" as the menu selection 1, using if/else
System.out.println();

switch(Menu){
case 1:
DeckOfCards.DisplayDeck();
break;

case 2:
DeckOfCards.ShuffleDeck();
break;

case 3:
DeckOfCards.CutDeck();
break;

case 4:
PlayGinRummy.DealDeck(); // Maybe shuffle things around so this just calls a method in player, from which everything else is called. Would mean moving things over from DeckOfCards so Player had access to hp, cp1, cp2 and cp3.
break;

case 5:
System.out.println("The program will now end.");
System.out.println();
break;

default:
System.out.println("Your input is not a valid selection.");
System.out.println();
break;
}
} while(Menu!=5);
}

public class DeckOfCards {

public DeckOfCards(){}

public static ArrayList<Integer> Deck = new ArrayList<>();
public static ArrayList<Integer> Discard = new ArrayList<>();

public static void FillDeck() throws InterruptedException{
System.out.print("Compiling deck");
DeckOfCards.CountdownEffect();
for(int count=0; count<52; count++){
Deck.add(count);
}
System.out.println(" Deck compiled!");
System.out.println();
}

public static void DisplayDeck() throws InterruptedException{
System.out.println("Displaying deck contents:");
Thread.sleep(1000);
for(int count=0; count<52; count++){
System.out.println(Deck.get(count));
}
System.out.println();
}

public static void ShuffleDeck() throws InterruptedException{
System.out.print("Shuffling deck");
Thread.sleep(1000);
for(int count=0; count<3; count++){
System.out.print(".");
Thread.sleep(1000);
}
Collections.shuffle(Deck);
System.out.println(" Deck shuffled!");
System.out.println();
}

public static void CutDeck() throws InterruptedException{
System.out.print("Cutting deck");
Thread.sleep(1000);
for(int count=0; count<3; count++){
System.out.print(".");
Thread.sleep(1000);
}
System.out.println();
System.out.println("The cut card is:");
Random random = new Random();
int CutPoint = 51 - (random.nextInt(52));
System.out.println(Deck.get(CutPoint));
System.out.println();
}

public static void CountdownEffect() throws InterruptedException{
Thread.sleep(1000);
for(int count=0; count<3; count++){
System.out.print(".");
Thread.sleep(1000);
}
}
}

public class Player{

public Player(ArrayList<Integer> h, int id, int n){
Hand = new ArrayList<>(h);
playerid = id;
NoOfPlayers = n;
}

public ArrayList<Integer> Hand;
public int playerid;
public static int NoOfPlayers;

public int SizeOfHand(){
int s = Hand.size();
return s;
}

public void DisplayHand() throws InterruptedException{
System.out.print("Displaying ");
if (playerid==1)
System.out.print("player's");
else if (playerid==2)
System.out.print("computer player 1's");
else if (playerid==3)
System.out.print("computer player 2's");
else if (playerid==4)
System.out.print("computer player 3's");
System.out.println(" hand:");
Thread.sleep(1000);
for(int count=0; count<7; count++){
if (count<6)
System.out.print(Hand.get(count) + ", ");
else if (count==6)
System.out.println(Hand.get(count) + ".");
}
}
}

public class PlayGinRummy {

public PlayGinRummy(){}

public static int NoOfPlayers;

public static void DealDeck() throws InterruptedException{
int cardno, playerno; // Tracks index of top card
System.out.println("Please input the desired number of players (2-4): ");
Scanner UserInput = new Scanner(System.in);
NoOfPlayers = UserInput.nextInt();
System.out.println();
System.out.println();

cardno=51;
playerno=0;

do{
if (NoOfPlayers==2){
Player hp = new Player(Player.Hand,1,NoOfPlayers);
Player cp1 = new Player(Player.Hand,2,NoOfPlayers);

do{
if (playerno%2==0)
hp.Hand.add(DeckOfCards.Deck.get(cardno));
else if (playerno%2==1)
cp1.Hand.add(DeckOfCards.Deck.get(cardno));

DeckOfCards.Deck.remove(cardno);

playerno++;
cardno--;
}while(hp.SizeOfHand()!=7 && cp1.SizeOfHand()!=7);

hp.DisplayHand();
cp1.DisplayHand();
}
else if (NoOfPlayers==3){
Player hp = new Player(Player.Hand,1,NoOfPlayers);
Player cp1 = new Player(Player.Hand,2,NoOfPlayers);
Player cp2 = new Player(Player.Hand,3,NoOfPlayers);

do{
if (playerno%3==0)
hp.Hand.add(DeckOfCards.Deck.get(cardno));
else if (playerno%3==1)
cp1.Hand.add(DeckOfCards.Deck.get(cardno));
else if (playerno%3==2)
cp2.Hand.add(DeckOfCards.Deck.get(cardno));

DeckOfCards.Deck.remove(cardno);

playerno++;
cardno--;
}while(hp.SizeOfHand()!=7 && cp1.SizeOfHand()!=7 && cp2.SizeOfHand()!=7);

hp.DisplayHand();
cp1.DisplayHand();
cp2.DisplayHand();
}
else if (NoOfPlayers==4){
Player hp = new Player(Player.Hand,1,NoOfPlayers);
Player cp1 = new Player(Player.Hand,2,NoOfPlayers);
Player cp2 = new Player(Player.Hand,3,NoOfPlayers);
Player cp3 = new Player(Player.Hand,4,NoOfPlayers);

do{
if (playerno%4==0)
hp.Hand.add(DeckOfCards.Deck.get(cardno));
else if (playerno%4==1)
cp1.Hand.add(DeckOfCards.Deck.get(cardno));
else if (playerno%4==2)
cp2.Hand.add(DeckOfCards.Deck.get(cardno));
else if (playerno%4==3)
cp3.Hand.add(DeckOfCards.Deck.get(cardno));

DeckOfCards.Deck.remove(cardno);

playerno++;
cardno--;
}while(hp.SizeOfHand()!=7 && cp1.SizeOfHand()!=7 && cp2.SizeOfHand()!=7 && cp3.SizeOfHand()!=7);

hp.DisplayHand();
cp1.DisplayHand();
cp2.DisplayHand();
cp3.DisplayHand();
}
else{
System.out.println("The choice made is invalid, please try again.");
System.out.println();
}
}while(NoOfPlayers!=2 && NoOfPlayers!=3 && NoOfPlayers!=4);

System.out.println();
}
}

最佳答案

As the "main" method must be static, surely anything it can access has to also be static and so on and so forth?

没有。静态方法可以访问实例方法。他们通过使用实例来做到这一点。 main 创建实例以使用实例方法(无论是 main 类本身还是其他类的实例方法)是非常非常常见的。

一般来说:

public static final void main(String[] args) {
SomeClass instance = new SomeClass();
instance.method();
}

如果您正在制作一些小型且完全独立的东西,那么 main 创建它所在的类是很常见的。例如:

public class Simple {
private String name;

public static final void main(String[] args) {
// Create a couple of instances
Simple s1 = new Simple();
Simple s2 = new Simple();

// Use instance methods
s1.setName("s1");
s2.setName("s2");
System.out.println(s1.getName());
System.out.println(s2.getName());
}

private void setName(String name) {
this.name = name;
}

private String getName() {
return this.name;
}
}

我使用了两个单独的实例来强调。我为它们使用了单独的变量,但当然,如果您要将它们视为分组列表或实例集,您可能会使用数组、ListSet。数组示例:

public class Simple {
private String name;

public static final void main(String[] args) {
// Create the array (no instances are created)
Simple[] simples = new Simple[2];

// Create a couple of instances, set their names
for (int n = 0; n < simples.length; ++n) {
simples[n] = new Simple();
simples[n].setName("s[" + n + "]");
}

// Use instance methods
for (Simple s : simples) {
System.out.println(s.getName());
}
}

private void setName(String name) {
this.name = name;
}

private String getName() {
return this.name;
}
}

关于java - 如何从静态方法轻松访问类的各种实例中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20957026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com