Getter & Setter
案例
修改 hp 值
Player.java
public class Player {
public int atk;
public int def;
public int hp;
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
}
Game.java
public class Game {
public static void main(String[] args) {
new Game().run();
}
public void run(){
Player player1 = new Player();
Player player2 = new Player();
player1.atk = 100;
player1.def = 100;
player1.hp = 100;
player2.atk = 100;
player2.def = 100;
player2.hp = 100;
int damage = 200;
player1.hp -= damage;
player2.hp -= damage;
}
}
hp 修改保护
Game.java
public class Game {
public static void main(String[] args) {
new Game().run();
}
public void run(){
Player player1 = new Player();
Player player2 = new Player();
player1.atk = 100;
player1.def = 100;
player1.hp = 100;
player2.atk = 100;
player2.def = 100;
player2.hp = 100;
int damage = 200;
player1.hp -= damage;
if (player1.hp < 0) {
player1.hp = 0;
}
player2.hp -= damage;
if (player2.hp < 0) {
player2.hp = 0;
}
}
}
使用 Setter 对修改进行保护
Player.java
public class Player {
public int atk;
public int def;
public int hp;
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
public void setHp(int hp){
if (hp < 0) {
hp = 0;
}
this.hp = hp;
}
}
Game.java
public class Game {
public static void main(String[] args) {
new Game().run();
}
public void run(){
Player player1 = new Player();
Player player2 = new Player();
player1.atk = 100;
player1.def = 100;
player1.hp = 100;
player2.atk = 100;
player2.def = 100;
player2.hp = 100;
int damage = 200;
player1.setHp(player1.hp - damage);
player2.setHp(player2.hp - damage);
}
}
是什么
针对属性的访问和赋值的专用方法(一套)
getter
用于取值
// 使用前
int value = player.hp;
// 使用后
int value = player.getHp();
setter
用于赋值
// 使用前
player.hp = 200;
// 使用后
player.setHp(200);
固定格式
// Getter
public 属性类型作为返回值类型 getXxxxXxx(){
return xxxxXxx;
}
// Setter
public void setXxxxXxx(属性类型作为参数类型 xxxxXxx){
this.xxxxXxx = xxxxXxx;
}
访问控制
拒绝直接访问
属性前面改为 private
只读属性
不提供 setter
赋值保护
setter加代码
内部属性
不提供 setter 也不提供 getter
类结构
所有属性 private
所有方法 public
public class 类名 {
private 属性类型 属性名;
public 返回值类型 方法名(参数1类型 参数1名,...){
//方法的执行代码
}
public getters & setters
}
完全使用 Getter Setter 的代码
Player.java
public class Player {
private int atk;
private int def;
private int hp;
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
public int getAtk(){
return atk;
}
public void setAtk(int atk){
this.atk = atk;
}
public int getDef(){
return def;
}
public void setDef(int def){
this.def = def
}
public int getHp(){
return hp;
}
public void setHp(int hp){
if (hp < 0) {
hp = 0;
}
this.hp = hp;
}
}
Game.java
public class Game {
public static void main(String[] args) {
new Game().run();
}
public void run(){
Player player1 = new Player();
Player player2 = new Player();
player1.setAtk(100);
player1.setDef(100);
player1.setHp(100);
player2.setAtk(100);
player2.setDef(100);
player2.setHp(100);
int damage = 200;
player1.setHp(player1.getHp() - damage);
player2.setHp(player2.getHp() - damage);
}
}
快捷键
写 Getter Setter 时 要使用快捷键避免不必要的错误