package org.jsoft.homework;
public interface Light {
void shine();
}
//定义Lamp类
class Lamp{
public void on (Light light){
light.shine();
}
}
//TestLamp
class TestLamp{
public static void main(String[] args) {
Lamp lamp =new Lamp();
//局部内部类
class A implements Light{
@Override
public void shine() {
// TODO Auto-generated method stub
System.out.println("shine in red");
}
}
lamp.on(new A());
lamp.on(new Light(){
public void shine(){
System.out.println("shine in yellow");
}
});
}
}
执行不了 方
in method
ma in anonymous inner class
in method
ma in local inner class
ABCD均可以通过
package Game;
public class Hunter {
String name;
int life;
String weapon;
boolean isLive;
public void fight(Monster monster){
if(monster.life!=100){
//System.out.println("fight with monster");
System.out.println(name+"打爆了僵尸");
}
}
public void injured(int life){
life=life-1;
System.out.println("life is"+life);
}
public void dead(String name){
if(life==0){
System.out.println(name+"挂了");
}
else{
System.out.println(name+"继续打僵尸");
}
}
public void show(String name,int life){
System.out.println(name+"的生命值为"+life);
}
}
package Game;
public class Monster {
String type;
int life=100;
boolean isLive;
public void kill(Hunter hunter){
if(hunter.life!=100){
//System.out.println("fight with monster");
System.out.println("僵尸打伤了猎人");
}
}
public void injured(){
if(life!=100){
System.out.println("受伤");
}else{
System.out.println("满血的僵尸");
}
}
public void dead(String type){
if(life==0){
System.out.println(type+"挂了");
}
else{
System.out.println(type+"继续攻击猎人");
}
}
public void show(){
System.out.println(life);
}
}
package Game;
public class TestGame {
public static void main(String[] args) {
Hunter h1=new Hunter();
h1.name="灰太狼";
h1.weapon="坦克";
h1.life=100;
h1.isLive=true;
Monster m1=new Monster();
m1.life=100;
m1.type="千年老僵尸";
m1.isLive=true;
}
}