Java异常处理的基本封装

以下是本人对EXCEPTION的基本封装,以后的异常继承此BASE类,继续扩展.给大家一个异常初步的编写.

package jp.co.yachiyobank.inf.com.frw.baseExp;

import java.io.PrintStream;
import java.io.PrintWriter;

public class BaseException extends Exception{

 private static final long serialVersionUID = 1L;
 
 private Throwable throwable;
 
 /**
  * クラスのコンストラクタ
  * @param なし
  */
 public BaseException(){
  super();
 }
 
 /**
  * クラスのコンストラクタ
  * @param msg エラーメッセージ
  */
 public BaseException(String msg){
  super(msg);
  
 }
 
 /**
  * クラスのコンストラクタ
  * @param msg エラーメッセージ
  * @param throwable
  */
 public BaseException(String msg,Throwable throwable){
  super(msg,throwable);
 }
 
 /**
  *
  * @param なし
  */
 public Throwable getException(){
  throwable=super.getCause();
    
  return throwable;
 }

 /**
  *
  * @param なし
  */
 public void printStackTrace(){
  super.printStackTrace();
 }
 
 /**
  *
  * @param printStream
  */
 public void printStackTrace(PrintStream printStream){
  super.printStackTrace(printStream);
 }
 
 /**
  *
  * @param printWriter
  */
 public void printStackTrace(PrintWriter printWriter){
  super.printStackTrace(printWriter);
  
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来帮你完成这个任务。首先,我们需要先设计一下奥比岛的基本元素和角色。 奥比岛的基本元素包括:地图、资源、建筑物、单位等。角色包括:玩家、敌方单位等。 接下来,我们可以开始编写代码了。为了实现上述要求,我们可以将代码分为以下几个类: 1. Map类:用于存储地图信息,包括地形、资源分布等。可以使用二维数组表示地图。 2. Building类:用于表示建筑物,包括建筑物的种类、等级、生命值等属性。可以使用继承来实现不同种类的建筑物。 3. Unit类:用于表示单位,包括单位的种类、等级、攻击力、生命值等属性。同样可以使用继承来实现不同种类的单位。 4. Player类:用于表示玩家,包括玩家的资源、建筑物、单位等属性。可以使用封装来实现对属性的保护。 5. Enemy类:用于表示敌方单位,同样继承自Unit类。 6. GameException类:用于处理游戏中可能出现的异常情况,例如资源不足、建筑物已经满级等。 7. Game类:用于控制游戏的整个流程,包括地图的初始化、建筑物、单位的创建、玩家和敌方单位的操作等。可以使用多态来实现不同种类的建筑物、单位的操作。 下面是一个简单的代码示例: ``` public abstract class Building { protected String name; protected int level; protected int health; public Building(String name, int level, int health) { this.name = name; this.level = level; this.health = health; } public abstract void upgrade(); } public class ResourceBuilding extends Building { private int resource; public ResourceBuilding(String name, int level, int health, int resource) { super(name, level, health); this.resource = resource; } public void upgrade() { // 升级建筑物 level++; health += 100; resource += 50; } public int collectResource() { // 收集资源 int collectedResource = resource; resource = 0; return collectedResource; } } public abstract class Unit { protected String name; protected int level; protected int health; protected int attack; public Unit(String name, int level, int health, int attack) { this.name = name; this.level = level; this.health = health; this.attack = attack; } public abstract void attack(Unit target); public void takeDamage(int damage) { health -= damage; if (health <= 0) { // 单位死亡 } } } public class MeleeUnit extends Unit { public MeleeUnit(String name, int level, int health, int attack) { super(name, level, health, attack); } public void attack(Unit target) { // 进行近战攻击 int damage = attack - target.defense; target.takeDamage(damage); } } public class Player { private int gold; private List<Building> buildings; private List<Unit> units; public Player(int gold) { this.gold = gold; buildings = new ArrayList<>(); units = new ArrayList<>(); } public void buildBuilding(Building building) throws GameException { // 建造建筑物 if (gold < building.getCost()) { throw new GameException("金币不足!"); } gold -= building.getCost(); buildings.add(building); } public void produceUnit(Unit unit) throws GameException { // 生产单位 if (!buildings.contains(unit.getProduceBuilding())) { throw new GameException("未找到生产该单位的建筑物!"); } if (gold < unit.getCost()) { throw new GameException("金币不足!"); } gold -= unit.getCost(); units.add(unit); } } public class Game { private Map map; private Player player; private List<Enemy> enemies; public Game() { map = new Map(); player = new Player(1000); enemies = new ArrayList<>(); } public void start() { // 初始化地图 map.init(); // 创建玩家的起始建筑物和单位 try { player.buildBuilding(new ResourceBuilding("金矿", 1, 500, 100)); player.buildBuilding(new BarrackBuilding("兵营", 1, 1000, 500)); player.produceUnit(new MeleeUnit("步兵", 1, 100, 10)); } catch (GameException e) { System.out.println("游戏初始化失败:" + e.getMessage()); return; } // 创建敌方单位 createEnemies(); // 游戏主循环 while (true) { // 玩家操作 playerOperation(); // 敌方操作 enemyOperation(); // 判断游戏是否结束 if (isGameOver()) { break; } } // 游戏结束 } private void createEnemies() { // 创建敌方单位 } private void playerOperation() { // 玩家操作 } private void enemyOperation() { // 敌方操作 } private boolean isGameOver() { // 判断游戏是否结束 return false; } } ``` 以上仅是一个简单的示例,实际上还会涉及到很多细节和功能,需要根据实际需求进行完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值