方法
程序里面出現(xiàn)了重復(fù)或相對獨(dú)立的功能 ,那么應(yīng)該將這些功能單獨(dú)寫成一個(gè)方法
重載方法: 在一個(gè)類中可以出現(xiàn)同名方法 只要他們的參數(shù)列表不同就能夠加以區(qū)分.
參數(shù)列表不同指的是, 參數(shù)的類型不相同或者參數(shù)的個(gè)數(shù)不相同或者二者皆不同數(shù)組 - 用一個(gè)變量保存多個(gè)同類型的值
for-each只讀循環(huán),不能改變數(shù)組的元素
(在for循環(huán)里面用x代表數(shù)組里的所有的值)
面向?qū)ο?/h2>
- 對象是能夠接收消息的實(shí)體
特征:1.一切都是對象
- 對象都有屬性和行為
- 每一個(gè)對象都是第一無二的
- 對象都屬于某個(gè)類
類:
類是對象的藍(lán)圖和模板
對象和對象之間可以互相發(fā)送消息就能構(gòu)造出復(fù)雜的系統(tǒng)
面向?qū)ο缶幊痰牡?步 - 定義類
數(shù)據(jù)抽象 - 屬性 - 找名詞
構(gòu)造器
行為抽象 - 方法 - 找動(dòng)詞
面向?qū)ο缶幊痰?步 - 創(chuàng)建對象
new 構(gòu)造器();
面向?qū)ο缶幊痰?步 - 給對象發(fā)消息
奧特曼打小怪獸
奧特曼類
package com.baidu;
public class Ultraman {
private String name;
private int hp; //Health Point
private int mp;
public Ultraman(String name, int hp, int mp) {
this.name = name;
this.hp = hp;
this.mp = mp;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp < 0 ? 0 : hp;
}
public int getMp() {
return mp;
}
public void setMp(int mp) {
this.mp = mp;
}
public void resumeMp(){
mp += 4;
}
public String info(){
return String.format("%s奧特曼 - 生命值 : %d, 魔法值 : %d",
name,hp,mp);
}
public void attack(Monster m){
int injury = (int) (Math.random() * 11 + 10);
m.setHp(m.getHp() - injury);
}
public void hugeAttack(Monster m){
if(mp >= 40){
int injury = m.getHp() /4 * 3 > 50 ? m.getHp() / 4 * 3 : 50;
m.setHp(m.getHp() - injury);
mp -= 40;
}
}
public void magicalAttack(Monster[] mArray){
if(mp >= 15){
for (Monster m: mArray) {
if(m.getHp() > 0){
int injury = (int) (Math.random() * 5 + 15);
m.setHp(m.getHp() - injury);
}
}
mp -= 15;
}
}
}
特征:1.一切都是對象
類:
類是對象的藍(lán)圖和模板
對象和對象之間可以互相發(fā)送消息就能構(gòu)造出復(fù)雜的系統(tǒng)
面向?qū)ο缶幊痰牡?步 - 定義類
數(shù)據(jù)抽象 - 屬性 - 找名詞
構(gòu)造器
行為抽象 - 方法 - 找動(dòng)詞
面向?qū)ο缶幊痰?步 - 創(chuàng)建對象
new 構(gòu)造器();
面向?qū)ο缶幊痰?步 - 給對象發(fā)消息
奧特曼打小怪獸
奧特曼類
package com.baidu;
public class Ultraman {
private String name;
private int hp; //Health Point
private int mp;
public Ultraman(String name, int hp, int mp) {
this.name = name;
this.hp = hp;
this.mp = mp;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp < 0 ? 0 : hp;
}
public int getMp() {
return mp;
}
public void setMp(int mp) {
this.mp = mp;
}
public void resumeMp(){
mp += 4;
}
public String info(){
return String.format("%s奧特曼 - 生命值 : %d, 魔法值 : %d",
name,hp,mp);
}
public void attack(Monster m){
int injury = (int) (Math.random() * 11 + 10);
m.setHp(m.getHp() - injury);
}
public void hugeAttack(Monster m){
if(mp >= 40){
int injury = m.getHp() /4 * 3 > 50 ? m.getHp() / 4 * 3 : 50;
m.setHp(m.getHp() - injury);
mp -= 40;
}
}
public void magicalAttack(Monster[] mArray){
if(mp >= 15){
for (Monster m: mArray) {
if(m.getHp() > 0){
int injury = (int) (Math.random() * 5 + 15);
m.setHp(m.getHp() - injury);
}
}
mp -= 15;
}
}
}
怪獸類
package com.baidu;
public class Monster {
private String name;
private int hp;
public Monster(String name, int hp) {
this.name = name;
this.setHp(hp);
}
public String info(){
return String.format("%s怪獸 - 生命值 : %d ", name,getHp());
}
public void attack(Ultraman u){
int injury = (int) (Math.random() * 5 + 10);
u.setHp(u.getHp() - injury);
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp < 0 ? 0 : hp;
}
}
測試類調(diào)用兩個(gè)類實(shí)現(xiàn)對象
package com.baidu;
public class Test03 {
public static Monster selectOne(Monster[] mArray){
Monster temp = null;
do {
int randomIndex = (int) (Math.random() * mArray.length);
temp = mArray[randomIndex];
} while (temp.getHp() == 0);
return temp;
}
public static void showMonstersInfo(Monster[] mArray){
for(Monster monster : mArray){
System.out.println(monster.info());
}
}
public static boolean isAllDead(Monster[] mArray){
for(Monster monster : mArray){
if(monster.getHp() > 0){
return false;
}
}
return true;
}
public static void main(String[] args){
Ultraman u = new Ultraman("迪迦", 1250, 50);
System.out.println(u.info());
Monster m1 = new Monster("白玉龍1號", 100);
Monster m2 = new Monster("白玉龍2號", 100);
Monster m3 = new Monster("白玉龍3號", 100);
Monster m4 = new Monster("白玉龍4號", 100);
Monster[] mArray = {m1,m2,m3,m4};
showMonstersInfo(mArray);
int round = 1;
do{
System.out.println("=======第" + round + "回合=======");
int random = (int) (Math.random() * 10 + 1);
Monster m = selectOne(mArray);
if(random <= 7){
usNormalAttack(u, m);
}
else if(random <= 9){
if (u.getMp() >= 15) {
System.out.println("奧特曼使用了魔法攻擊");
u.magicalAttack(mArray);
for (Monster monster : mArray) {
if (monster.getHp() > 0) {
monster.attack(u);
}
}
}
else{
usNormalAttack(u, m);
}
}
else {
if(u.getMp() >= 40){
System.out.println("奧特曼使用了終極攻擊");
u.hugeAttack(m);
if(m.getHp() > 0){
m.attack(u);
}
}
else{
usNormalAttack(u, m);
}
}
if(u.getHp() > 0){
u.resumeMp();
}
round += 1;
System.out.println(u.info());
showMonstersInfo(mArray);
}while(u.getHp() > 0 && !isAllDead(mArray));
if(u.getHp() > 0){
System.out.println("奧特曼勝利?。?!");
}
else{
System.out.println("小怪獸勝利?。?!");
}
}
public static void usNormalAttack(Ultraman u, Monster m) {
System.out.println("奧特曼使用了普通攻擊");
u.attack(m);
if(m.getHp() > 0){
m.attack(u);
}
}
}