内部类:
1.Light类、Lamp类、TestLamp类:
package org.jsoft.zuoye;
//灯接口
interface Light{
void shine();
}
//定义Lamp类
class Lamp{
public void on(Light light){
light.shine();
}
}
//TeseLamp,测试类
public class TestLamp {
public static void main(String[] args) {
Lamp lamp = new Lamp();
//1,局部内部类
class A implements Light{
public void shine(){
System.out.println("shine in red");
}
}
lamp.on(new A());
//2,匿名内部类
lamp.on(new Light(){
public void shine(){
System.out.println("shine in yellow");
}
});
}
}
2.看程序,输出结果:
in method
ma in anonymous inner class
in method
ma in local inner class
3.中文下引号都不能输出
英文下引号都能输出
论坛题目:
Hunter类:
package org.jsoft.zuoye;
public class Hunter {
private String name;
private int life=500;
private String weapon="青龙偃月刀";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
private boolean isLive=true;
public void fight(Monster monster){//Hunter对象攻击Monster对象
System.out.println(this.getName()+"手持"+this.getWeapon()+"攻击"+monster.getType());
monster.injured();
}
public void injured(){//Hunter对象受到伤害
System.out.println(getName()+"被打中了");
setLife(getLife()-100);
}
public void dead(){//判断是否活着
if(this.getLife()<=0){
this.setLive(false);
}
}
public void show(){//展示当前生命值
this.dead();
if(!isLive()){
System.out.println(this.getName()+"死亡");
}
else{
System.out.println(this.getName()+"的生命值:"+this.getLife());
}
}
}
Monster类:
package org.jsoft.zuoye;
public class Monster {
private String type="僵尸";//类型
private boolean isLive=true;//是否活着
private int life=1000;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public void injured(){//Monster受到Hunter攻击
System.out.println(this.getType()+"被打中了");
setLife(getLife()-500);
}
public void kill(Hunter hunter){//Monster攻击Hunter
System.out.println(getType()+"攻击"+hunter.getName());
hunter.injured();
}
public void dead(){//判断是否活着
if(this.getLife()<=0){
setLive(false);
}
}
public void show(){//显示当前生命值
this.dead();
if(!isLive()){
System.out.println(this.getType()+"死亡");
}
else{
System.out.println(this.getType()+"的生命值:"+this.getLife());
}
}
}
TestGame类:
package org.jsoft.zuoye;
import java.util.Scanner;
public class TestGame {
public static void main(String[] args) {
Hunter h = new Hunter();
Monster m = new Monster();
Scanner sc = new Scanner(System.in);
System.out.println("请为你的猎人起一个霸气的名字:");
h.setName(sc.next());
System.out.println("你的武器是:");
System.out.println(h.getWeapon());
System.out.println("你即将对战的怪物是:");
System.out.println(m.getType());
for(int i=1;;i++){
System.out.println("---------------");
System.out.println("第"+i+"回合开始:");
h.fight(m);
m.kill(h);
System.out.println("第"+i+"回合结束");
System.out.println("--战斗统计--");
h.show();
m.show();
if(h.getLife()<=0||m.getLife()<=0){
System.out.println("----对局结束----");
break;
}
}
if(h.getLife()<=0){
System.out.println("战胜方是:"+m.getType());
}
else{
System.out.println("战胜方是:"+h.getName());
}
}
}