Java 连接两个类
抱歉打扰了,我是编程新手,有点困惑 如何链接两个类的变量?把一个类的变量转换成另一个类?我正在尝试为一个玩家可以加入游戏的游戏平台制作一个面向对象的场景(每个游戏最多2名玩家,并支付相关费用。我正在尝试在游戏类中获得一个将这些玩家相加的方法,但我不确定如何将所有玩家“amuntowed”相加我还必须确保两名球员不是同一名球员?非常感谢你的建议!以下是我迄今为止得到的Java 连接两个类,java,oop,Java,Oop,抱歉打扰了,我是编程新手,有点困惑 如何链接两个类的变量?把一个类的变量转换成另一个类?我正在尝试为一个玩家可以加入游戏的游戏平台制作一个面向对象的场景(每个游戏最多2名玩家,并支付相关费用。我正在尝试在游戏类中获得一个将这些玩家相加的方法,但我不确定如何将所有玩家“amuntowed”相加我还必须确保两名球员不是同一名球员?非常感谢你的建议!以下是我迄今为止得到的 import java.time.LocalDate; import java.time.Period; import java.
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
public class Player {
private static int instanceCounter = 0;
private int playerId;
private String playerName;
public int birthDay;
public int birthMonth;
public int birthYear;
public int playerAge;
public double amountOwed;
public Player(int playerId, String playerName, int birthDay, int birthMonth, int birthYear, double amountOwed) {
this.playerId = playerId;
this.playerName = playerName;
this.birthDay = birthDay;
this.birthMonth = birthMonth;
this.birthYear = birthYear;
this.amountOwed = amountOwed;
instanceCounter++;
}
public double getAmountOwed() {
return (amountOwed);
}
public int calculateAge() {
LocalDate birthDate = LocalDate.of(birthYear, birthMonth, birthDay);
LocalDate currentDate = LocalDate.now();
return playerAge = Period.between(birthDate, currentDate).getYears();
}
public String printDetails() {
return (playerName + ", with ID " + playerId + ", is " + playerAge + " and owes the game platform£" + amountOwed);
}
public String payFees() {
amountOwed = 0;
return ("Thank you" + playerName + ",you have paid for your game, your balance now stands at £0.00");
}
public String joinGameChess() {
if (instanceCounter < 3) {
return (playerName + " has joined the game.");
} else {
return ("Sorry, maximum of 2 players per game");
}
}
public String leaveGame() {
return (playerName + "has left the game.");
}
}
public class Game {
private String gameName;
private int gameId;
private int minAge;
private double fee;
public double amountOwed;
public double totalFeesOwed;
public Game(String gameName, int gameId, int minAge, double fee) {
this.gameId = gameId;
this.gameName = gameName;
this.minAge = minAge;
this.fee = fee;
}
public String printGameDetails() {
return (gameName + "of ID " + gameId + "has a minimum age of " + minAge + " and costs " + fee + " to play");
}
}
public class W07Practical {
public static void main(String[] args) {
Player marina = new Player(123, "Marina", 15, 4, 1999, 0);
marina.calculateAge();
System.out.println(marina.printDetails());
Game chess = new Game("chess", 1234, 19, 2);
marina.getAmountOwed();
System.out.println(marina.joinGameChess());
Player elise = new Player(153, "elise", 16, 3, 2000, 0);
System.out.println(elise.joinGameChess());
Player john = new Player(322, "john", 23, 5, 2002, 0);
System.out.println(john.joinGameChess());
System.out.println(john.printDetails());
System.out.println(elise.printDetails());
}
}
import java.time.LocalDate;
导入java.time.Period;
导入java.util.Date;
公开课选手{
私有静态int instanceCounter=0;
私人int playerId;
私人弦乐演奏者姓名;
公众生日;
公众生日月;
公众生日;
公共娱乐场所;
公营机构不受限制;
公共播放器(int playerId,String playerName,int生日,int生日,int生日,双倍数量){
this.playerId=playerId;
this.playerName=playerName;
这个生日=生日;
this.birthMonth=birthMonth;
this.birthYear=生日;
this.amontowed=amontowed;
instanceCounter++;
}
公共双GetAmounted(){
返回(未列出);
}
公共整数计算表(){
LocalDate birthDate=LocalDate.of(出生年、出生月、生日);
LocalDate currentDate=LocalDate.now();
return playerAge=Period.between(生日,当前日期).getYears();
}
公共字符串printDetails(){
return(playerName+,ID为“+playerId+”,为“+playerAge+”,欠游戏平台的钱”+amounted);
}
公共字符串支付费(){
amuntowed=0;
return(“谢谢”+playerName+”,您已经支付了游戏费用,您的余额现在为0.00英镑);
}
公共字符串joinGameChess(){
如果(instanceCounter<3){
return(playerName+“已加入游戏”);
}否则{
返回(“对不起,每场游戏最多2名玩家”);
}
}
公共字符串游戏(){
return(playerName+“已离开游戏。”);
}
}
公开课游戏{
私有字符串游戏名;
私有int配子体;
私人住宅;
私人双重收费;
公营机构不受限制;
播下的公共双重种子;
公共游戏(字符串gameName、整数gameId、整数minAge、双倍费用){
this.gameId=gameId;
this.gameName=游戏名称;
this.minAge=minAge;
这个。费用=费用;
}
公共字符串printGameDetails(){
返回(ID为“+gameId+”的gameName+”的最低年龄为“+minAge+”,费用为“+fee+”);
}
}
公共课W070实用{
公共静态void main(字符串[]args){
玩家玛丽娜=新玩家(123,“玛丽娜”,15,4,1999,0);
marina.calculateAge();
System.out.println(marina.printDetails());
国际象棋=新游戏(“国际象棋”,1234,19,2);
marina.getamuntowed();
System.out.println(marina.joinGameChess());
玩家elise=新玩家(153,“elise”,16,32000,0);
System.out.println(elise.joinGameChess());
玩家约翰=新玩家(322,“约翰”,23,52002,0);
System.out.println(john.joinGameChess());
System.out.println(john.printDetails());
System.out.println(elise.printDetails());
}
}
欠款总额:
amountOwed = player1.getAmountOwed() + player2.getAmountOwed();
您可以检查两个玩家的姓名是否相同,或者通过比较他们的生日来更精确地检查。为了更精确地比较这两个玩家。如果您只想在用户与其他玩家一起玩时添加费用,您可以使用
布尔isTwoPlayer;
并在getAmount
中根据