java_1119 夏丽昀

1

A B C

in method 

ma in anonymous inner class

in method 

ma in local inner class 

程序第一个匿名内部类,第二个是局部内部类,两个都是从MyClass调用的

在用method方法时,都是先调用本类的MyClass的method 里的语句

3

匿名内部类

package org.jsoft.zouye;

interface Light{
 void shine();
}
class Lamp{
 public void on (Light light){
  light.shine();
 
  } 
}

public class TestLamp {
public static void main(String[]args){
 Lamp lamp=new Lamp();
 lamp.on(new Light(){
    

 @Override
 public void shine() {
  // TODO Auto-generated method stub
  System.out.println("shine in yellow");
  
 } 
 });
 }
  成员内部不太理解 

package org.jsoft.zouye;

public class Hunter {
 private String name;
 private int life;
 private String weapon;
 boolean isLive;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getLife() {
  return life;
 }

 public void setLife(int life) {
  // if(life>80)
  // this.life = life;
  // else{
  // System.out.println("猎人已死,不能继续减少");
  // }
 }

 public String getWeapon() {
  return weapon;
 }

 public void setWeapon(String weapon) {
  this.weapon = weapon;
 }

 public Hunter(int life) {
  this.life = life;

 }

 public void fight(Monster monster) {
  System.out.print("猎人打僵尸");
  if (monster.isLive) {
   monster.injured();
   
   System.out.print(" " + "每次僵尸被打生命值减少到" + monster.getLife());
  } else {
   System.out.println("僵尸已死");
  }
 }

 public void injured() {

  System.out.println("猎人受伤了");
  setLife(-10);
 }

 public void dead() {
  System.out.println("猎人死亡了");
 }

 public void show() {
  System.out.println("这个猎人已死");
 }
}

package org.jsoft.zouye;

public class Monster {
 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public int getLife() {
  return life;
 }

 public void setLife(int life) {
  this.life = life;
 }

 public boolean isLIve() {
  return isLive;
 }

 public void setLIve(boolean isLIve) {
  this.isLive = isLIve;
 }

 private String type;
 private int life;
 boolean isLive;

 public Monster(int life) {
  this.life = life;
 }

 public void injured() {
  System.out.println("僵局受伤了");
  setLife(-10);

 }

 public void kill(Hunter hunter) {
  System.out.print("当猎人要杀僵尸时");
  if (hunter.isLive) {
   hunter.injured();
   System.out.println("僵尸会还击,减少猎人的生命值" + hunter.getLife());
  }else{
   System.out.println("猎人已死");
  }

 }

 public void dead() {
  if(this.life<15){
  System.out.println(" " + "僵尸死亡了");}
 }

 public void show() {
  if(isLive==true){
  System.out.println("僵尸真的死了");}
 }
}

package org.jsoft.zouye;

import java.util.Scanner;

public class TestGame {
 public static void main(String[] args) {
  Hunter hunter = new Hunter(100);
  Monster monster = new Monster(100);
  Scanner sc = new Scanner(System.in);
  while (true) {
   System.out.println("欢迎来到这个游戏中,请选择你要选择的对象 ");
    monster.isLive=true;
      hunter.isLive=true;
      String in = sc.next();
   if (in.equals("hunter")) {
    System.out.println("你选择的角色成功,请为你的身份起个名字吧");
    String nam = sc.next();
    hunter.setName(nam);
    System.out.print("请选择你的武器");
    String w = sc.next();
    hunter.setWeapon(w);
    System.out.println("你的生命值初始为100,开始战斗");
    hunter.fight(monster);
    monster.injured();
    

   }
   else if (in.equals("monster")) {
    System.out.println("你选择的角色成功,请选择什么样的僵尸");
    String nam = sc.next();  
    System.out.println("你的生命值初始为100,开始战斗");
    monster.kill(hunter);
    System.out.println("当僵尸的生命值少于15时,死亡");

   }
   else {
    System.out.print("请重新输入 ");
   }
  }
 }
}

测试结果

欢迎来到这个游戏中,请选择你要选择的对象
hunter
你选择的角色成功,请为你的身份起个名字吧

猎人
请选择你的武器猎枪
你的生命值初始为100,开始战斗
猎人打僵尸僵局受伤了
 每次僵尸被打生命值减少到-10僵局受伤了
欢迎来到这个游戏中,请选择你要选择的对象

monster
你选择的角色成功,请选择什么样的僵尸

吓人的僵尸、
你的生命值初始为100,开始战斗
当猎人要杀僵尸时猎人受伤了
僵尸会还击,减少猎人的生命值100
当僵尸的生命值少于15时,死亡
欢迎来到这个游戏中,请选择你要选择的对象

转载于:https://my.oschina.net/u/3715015/blog/1576945

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值