Java Player类代码正确性校验及修正方案咨询
你的Player类代码问题分析与修正
首先咱们对照需求一条条捋,当前代码存在几个关键问题,我会先梳理问题,再给出修正后的完整代码:
核心问题梳理
- 未遵循类依赖要求:需求明确要求使用
Name和PairOfDice类封装数据,但你直接用了firstName/lastName字符串和两个独立的Die对象,完全偏离了设计要求。 - 构造方法不符合规范:需求要求的三个构造是「默认构造」「接收Name对象」「接收Name+PairOfDice对象」,但你写的是接收String参数的构造,和要求完全不匹配。
- 方法功能错位:
- 你写的
setFullName()是返回全名的方法,但需求需要的是void setFullPlayerName(String),功能是拆分输入的全名字符串来设置Name的名和姓,而不是返回结果。 - 缺少
PairOfDice的全局get方法,反而提供了单个Die的get方法,不符合「委托给PairOfDice」的设计要求。
- 你写的
- 空指针风险:第二个构造方法没有初始化
red和blue对象,调用rollDice或getDiceScore时会直接抛出空指针异常。 - 命名不规范:
setlastName和getlastName没有遵循Java驼峰命名法,应该改为setLastName和getLastName。 - toString方法不符合预期:当前toString只输出骰子信息,没有包含Player的名字,格式也不符合Player类的展示逻辑。
修正后的完整代码
首先假设你已经有基础的Name、PairOfDice和Die类(如果没有,我也附上了标准实现):
先补充依赖类的基础实现
// Name类:封装名和姓 public class Name { private String firstName; private String lastName; public Name() { this("", ""); } public Name(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return firstName + " " + lastName; } } // PairOfDice类:封装两个Die对象,提供骰子操作方法 public class PairOfDice { private Die red; private Die blue; public PairOfDice() { red = new Die(); blue = new Die(); } public PairOfDice(Die red, Die blue) { this.red = red; this.blue = blue; } public void roll() { red.roll(); blue.roll(); } public int getTotalScore() { return red.getScore() + blue.getScore(); } public Die getRed() { return red; } public Die getBlue() { return blue; } @Override public String toString() { return "Red: " + red.getScore() + ", Blue: " + blue.getScore(); } } // Die类:单个骰子的基础实现 public class Die { private int score; public Die() { roll(); // 初始化时直接掷一次骰子 } public void roll() { score = (int)(Math.random() * 6) + 1; } public int getScore() { return score; } }
修正后的Player类
public class Player { // 按照需求使用Name和PairOfDice作为成员变量 private Name playerName; private PairOfDice dicePair; // a) 三个要求的构造方法 // 默认构造方法 public Player() { playerName = new Name(); dicePair = new PairOfDice(); } // 接收Name对象的构造方法 public Player(Name playerName) { this.playerName = playerName; this.dicePair = new PairOfDice(); // 初始化默认骰子对 } // 接收Name与PairOfDice对象的构造方法 public Player(Name playerName, PairOfDice dicePair) { this.playerName = playerName; this.dicePair = dicePair; } // b) 实现要求的get/set与委托方法 // Name的get/set方法 public Name getPlayerName() { return playerName; } public void setPlayerName(Name playerName) { this.playerName = playerName; } // PairOfDice的get方法(仅提供get,符合需求) public PairOfDice getDicePair() { return dicePair; } // 委托给PairOfDice的rollDice方法 public void rollDice() { dicePair.roll(); } // 委托给PairOfDice的getDiceScore方法 public int getDiceScore() { return dicePair.getTotalScore(); } // 重写toString方法,包含玩家名字和骰子信息 @Override public String toString() { return "Player: " + playerName + ", Dice Status: " + dicePair; } // c) void类型的setFullPlayerName方法,拆分全名设置名和姓 public void setFullPlayerName(String fullName) { if (fullName == null || fullName.trim().isEmpty()) { playerName.setFirstName(""); playerName.setLastName(""); return; } // 按第一个空格拆分全名,兼容只有名的情况 String[] nameParts = fullName.trim().split(" ", 2); if (nameParts.length == 1) { playerName.setFirstName(nameParts[0]); playerName.setLastName(""); } else { playerName.setFirstName(nameParts[0]); playerName.setLastName(nameParts[1]); } } }
关键修改说明
- 类结构对齐需求:把零散的字符串和Die对象替换成
Name和PairOfDice类,完全符合需求的封装设计。 - 构造方法严格匹配:实现了三个要求的构造方法,每个构造都确保成员变量被正确初始化,避免空指针。
- 方法功能修正:
- 实现了
Name的get/set和PairOfDice的get方法。 rollDice和getDiceScore完全委托给PairOfDice的对应方法,符合设计要求。- 新增的
setFullPlayerName方法是void类型,能正确处理各种输入场景(空字符串、只有名、完整全名)。
- 实现了
- 细节优化:修正了命名规范,优化了toString方法的展示逻辑,让输出更符合Player类的身份。
内容的提问来源于stack exchange,提问作者RookieCode
相关产品推荐
相关产品推荐

