您的位置:首页 > 移动开发 > Android开发

无奈的Android之旅(Java篇)0317:面向对象

2014-03-17 15:50 351 查看
面向对象(Object Oriented)是一种以事物为中心的编程思想。


事件:英雄攻击怪物

步骤

1.声明英雄类和怪物类

2.逐步分析英雄,怪物应有的属性和行为

3.根据分析结果逐步实现各个属性和行为

public class hero{

//成员变量

//成员方法

}

protected:同一个包中可以调用(默认)

public:都可以调用

private:私有的

import java.util.Random;

public class test0317 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Math.random();
Random r = new Random();
/*		Hero hero = new Hero(100,10);
Hero master = new Hero(50,5);
// hero.getId();
System.out.println(master.getName());
hero.setName("英雄");

System.out.println(hero.getName());

System.out.println();
master.killHp(hero.getAtt());
hero.killHp(master.getAtt());
master.killHp(hero.getAtt());
hero.killHp(master.getAtt());
master.killHp(hero.getAtt());
hero.killHp(master.getAtt());
master.killHp(hero.getAtt());
hero.killHp(master.getAtt());
master.killHp(hero.getAtt());
hero.killHp(master.getAtt());
master.killHp(hero.getAtt());
hero.killHp(master.getAtt());
master.killHp(hero.getAtt());

master.killHp(hero.getMp());
hero.killHp(master.getMp());*/

FengJie fj = new FengJie("凤姐",100,r.nextInt(50),1,0,1);
//System.out.println("================================");
FengRuiLong frl = new FengRuiLong("冯瑞龙",100,r.nextInt(50),1,0,1);
//System.out.println("================================");
LiGang lg = new LiGang("李刚",100,r.nextInt(50),1,0,1);
//System.out.println(fj.getId());
//System.out.println(frl.getId());
/*		fj.killHp(frl.getAtt());
frl.killHp(fj.getAtt());
fj.killHp(frl.getAtt());
frl.killHp(fj.getAtt());
fj.killHp(frl.getAtt());
frl.killHp(fj.getAtt());
fj.killHp(frl.getAtt());
frl.killHp(fj.getAtt());
fj.killHp(frl.getAtt());
frl.killHp(fj.getAtt());
fj.killHp(frl.getAtt());
frl.killHp(fj.getAtt());*/
while(true){
fj.setAtt(r.nextInt(50));
frl.setAtt(r.nextInt(50));
lg.setAtt(r.nextInt(50));

fj.killHp(frl.getAtt());
frl.setAtt(r.nextInt(50));

lg.killHp(frl.getAtt());
frl.killHp(fj.getAtt());
fj.setAtt(r.nextInt(50));

lg.killHp(fj.getAtt());

fj.killHp(lg.getAtt());
lg.setAtt(r.nextInt(50));
frl.killHp(lg.getAtt());

}

}

}


public class Hero {

//	public Hero(){
//
//
//	}
//成员变量
int hp = 100;
int att = 10;
int mp = 50;
String name = "怪物";
public Hero(int hp ,int att){
this.hp = hp;
this.att = att;
}

public String getName(){

return name;
}

public void setName(String name){

this.name = name;
}

/**
* @return the hp
*/
public int getHp() {
return hp;
}

/**
* @param hp the hp to set
*/
public void setHp(int hp) {
this.hp = hp;
}

/**
* @return the att
*/
public int getAtt() {
if(hp > 0)
return att;
else
return 0;
}

/**
* @param att the att to set
*/
public void setAtt(int att) {
this.att = att;
}

/**
* @return the mp
*/
public int getMp() {
if(hp > 0)
return mp;
else
return 0;
}

/**
* @param mp the mp to set
*/
public void setMp(int mp) {
this.mp = mp;
}

public void killHp(int kill){

if(kill > 0)
if(hp > 0 ){
hp -= kill;
System.out.println(name+"被攻击,掉"+kill+"点血");
}
else{
System.out.println("你不能守尸,守尸惩罚:尸体满血"+(hp + 100));
}

if(hp <= 0){

System.out.println(name+"已经死亡 GAME OVER!");

}else{

System.out.println(name+"还有"+hp+"点血");
}
Math.random();

}
}


public class LiGang{
int hp;
int def;
int att;
int speed;
int id;
String name;

public LiGang(String name,int hp,int att,int def,int speed,int id){
this.name = name;
this.hp = hp;
this.att = att;
this.def = def;
this.speed = speed;
this.id = id;

}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public LiGang(){

//System.out.println("LiGang"+id);
}
/**
* @return the hp
*/
public int getHp() {
return hp;
}
/**
* @param hp the hp to set
*/
public void setHp(int hp) {
this.hp = hp;
}

/**
* @return the def
*/
public int getDef() {
return def;
}
/**
* @param def the def to set
*/
public void setDef(int def) {
this.def = def;
}
/**
* @return the att
*/
public int getAtt() {
if(hp > 0)
return att;
else
return 0;
}
/**
* @param att the att to set
*/
public void setAtt(int att) {
this.att = att;
}
/**
* @return the speed
*/
public int getSpeed() {
return speed;
}
/**
* @param speed the speed to set
*/
public void setSpeed(int speed) {
this.speed = speed;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

public void killHp(int kill){

if(kill > 0)
if(hp > 0 ){
hp -= kill;
System.out.println(name+"被攻击,掉"+kill+"点血");
}
else{
System.out.println("你不能守尸,守尸惩罚:尸体满血"+(hp + 100));
}

if(hp <= 0){

System.out.println(name+"已经死亡 GAME OVER!");
System.exit(0);

}else{

System.out.println(name+"还有"+hp+"点血");
}

}
}


public class FengRuiLong  extends LiGang{

public FengRuiLong(){

}
public FengRuiLong(String name,int hp,int att,int def,int speed,int id){
this.name = name;
this.hp = hp;
this.att = att;
this.def = def;
this.speed = speed;
this.id = id;
//System.out.println("FengRuiLong"+id);

}
}


public class FengJie extends FengRuiLong{
public FengJie(String name,int hp,int att,int def,int speed,int id){
super(name,hp,att,def,speed,id);

//System.out.println("fengjie"+id);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: