Java基礎學習筆記 - Day11 - 第四章 參考型別用法總結
- Java基礎學習筆記 - Day11 - 第四章 參考型別用法總結
- 4.1 class作為成員變數
- 4.2 interface作為成員變數
- 4.3 interface作為方法引數和回傳值型別
Java基礎學習筆記 - Day11 - 第四章 參考型別用法總結
系統:Win10
JDK:1.8.0_121
IDE:IntelliJ IDEA 2017.3.7
實際的開發中,參考型別的使用非常重要,也是非常普遍的,我們可以在理解基本型別的使用基礎上,進一步去掌握參考型別的使用方式,基本型別可以作為成員變數、作為方法的引數、作為方法的回傳值,那么當然參考型別也是可以的
4.1 class作為成員變數
在定義一個類Role(游戲角色)時,代碼如下:
public class Role {
int id; // 角色id
int blood; // 生命值
String name; // 角色名稱
}
使用int型別表示角色id和生命值,使用String型別表示姓名,此時,String本身就是參考型別,由于使用的方式類似常量,所以往往忽略了它是參考型別的存在,如果我們繼續豐富這個類的定義,給Role增加武器,穿戴裝備等屬性,我們將如何撰寫呢?
定義武器類,將增加攻擊能力:
public class Weapon {
private String name; // 武器名稱
private int hurt; // 傷害值
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHurt() {
return hurt;
}
public void setHurt(int hurt) {
this.hurt = hurt;
}
}
定義穿戴盔甲類,將增加防御能力,也就是提升生命值:
public class Armour {
private String name; // 裝備名稱
private int protect; // 防御值
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProtect() {
return protect;
}
public void setProtect(int protect) {
this.protect = protect;
}
}
重新定義角色類:
public class Role {
private int id; // 角色id
private int blood; // 生命值
private String name; // 角色名稱
// 添加武器屬性
private Weapon weapon;
// 添加盔甲屬性
private Armour armour;
public Role() {
}
public Role(int id, int blood, String name) {
this.id = id;
this.blood = blood;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public Armour getArmour() {
return armour;
}
public void setArmour(Armour armour) {
this.armour = armour;
}
// 攻擊方法
public void attack() {
System.out.println(this.name + "使用" + weapon.getName() + "造成" + weapon.getHurt() + "點傷害");
}
// 防御方法
public void defense() {
System.out.println(this.name + "穿上" + armour.getName() + "增加" + armour.getProtect() + "點防御");
}
}
測驗類:
public class Test {
public static void main(String[] args) {
// 創建Role物件
Role role = new Role(1,616,"蓋倫");
// 創建Weapon物件
Weapon weapon = new Weapon();
weapon.setName("多蘭劍");
weapon.setHurt(8);
// 創建Armour物件
Armour armour = new Armour();
armour.setName("反傷甲");
armour.setProtect(250);
// 設定武器和盔甲
role.setWeapon(weapon);
role.setArmour(armour);
// 攻擊
role.attack();
// 盔甲
role.defense();
}
}
運行結果:

類作為成員變數時,對他進行賦值的操作,實際上是賦給該類一個物件
4.2 interface作為成員變數
介面是對方法的封裝,對應游戲當中,可以看做是拓展游戲角色的技能,所以,如果想擴展更強大技能,我們在Role中,可以增加介面作為成員變數,來設定不同的技能
定義介面:
// 技能
public interface Skill {
public abstract void Q();
}
重新定義角色類:
public class Role {
private int id; // 角色id
private int blood; // 生命值
private String name; // 角色名稱
// 添加武器屬性
private Weapon weapon;
// 添加盔甲屬性
private Armour armour;
// 添加技能
private Skill skill;
public Role() {
}
public Role(int id, int blood, String name) {
this.id = id;
this.blood = blood;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public Armour getArmour() {
return armour;
}
public void setArmour(Armour armour) {
this.armour = armour;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
// 攻擊方法
public void attack() {
System.out.println(this.name + "使用" + weapon.getName() + "造成" + weapon.getHurt() + "點傷害");
}
// 防御方法
public void defense() {
System.out.println(this.name + "穿上" + armour.getName() + "增加" + armour.getProtect() + "點防御");
}
// Q技能攻擊
public void useQ() {
System.out.println("使用Q技能");
skill.Q();
System.out.println("攻擊結束");
}
}
定義測驗類:
public class Test {
public static void main(String[] args) {
// 創建Role物件
Role role = new Role(1,616,"蓋倫");
// 創建技能
Skill skill = new Skill() {
@Override
public void Q() {
System.out.println(" 蓋倫的移動速度獲得爆發性提升," +
"\n 同時移除身上的所有減速效果," +
"\n 他的下次攻擊將打擊敵人的要害部位," +
"\n 造成額外傷害并將目標沉默,");
}
};
// 設定技能
role.setSkill(skill);
// 使用技能
role.useQ();
}
}
運行結果:

我們使用一個介面,作為成員變數,以便隨時更換技能,這樣的設計更加靈活,增加了程式的拓展性,
介面作為成員變數時,對他進行賦值的操作,實際上,是賦給該介面的一個子類物件
4.3 interface作為方法引數和回傳值型別
當介面作為方法的引數時,需要傳遞什么呢?當介面作為方法的回傳值型別時,需要回傳什么呢?其實都是他的子類物件,ArrayList類我們并不陌生,查看API我們可以發現,實際上,他是java.util.List介面的實作類,所以,當我們看見List介面作為引數或者回傳值型別時,當然可以將ArrayList的物件進行傳遞或回傳
觀察如下方法:獲取某集合中所有的偶數
public class Test {
public static void main(String[] args) {
// 創建ArrayList集合,并添加數字
ArrayList<Integer> srcList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
srcList.add(i);
}
/*
獲取偶數集合
因為getEvenNum方法的引數是List,而ArrayList是List的子類
所以srcList可以傳遞
*/
List list = getEvenNum(srcList);
System.out.println(list);
}
public static List<Integer> getEvenNum(List<Integer> list) {
// 創建保存偶數的集合
ArrayList<Integer> evenList = new ArrayList<>();
// 遍歷集合list,判斷元素為偶數時,就添加到evenList中
for (int i = 0; i < list.size(); i++) {
Integer index = list.get(i);
if (index % 2 == 0) {
evenList.add(index);
}
}
/*
回傳偶數集合
因為getEvenNum方法的回傳值型別時List,而ArrayList是List的子類
所以evenList可以回傳
*/
return evenList;
}
}
運行結果:

介面作為引數時,傳遞它的子類物件
介面作為回傳值型別時,回傳它的子類物件
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/226309.html
標籤:其他
上一篇:ccpc 江西省賽 博弈論 J Split Game
下一篇:棋盤問題C++實作與分析
