1、
分析:
inteirface IA 定义一个接口,并有一个抽象方法ma()
定义一个类,名为MyClass,并存在一个method方法,但是参数列表是接口型的,所以自然也可以调用接口中方法。
从main方法开始查看,第一个是匿名类,原因是想执行上述MaClass中的ia.ma();首先MyClass.method执行了method里面的输出语句System.out.println("in method")。在匿名类里面重写ma()方法是因为接口类属于特殊的抽象类,并且方法都是抽象方法,所以必须进行方法重写。并且匿名类定定义以后会执行匿名类中的方法,即执行了method方法中的ia.ma(),所以会执行System.out.println("ma in anonymous inner class")
第二个是用正常的类去实现接口,所以也会重写ma()方法,但如果想要执行得创建对象
最后一个行代码是调用MyClass的method方法,因为都是在一个类里面定义的所有东西,所以可以直接用,但参数变为了MyMaClass这个类,并且MyMaClass这个类继承了接口,自然也需要重写方法。所以先执行method里面的输出语句,然后ia.ma()输出MyMaClass中重写的ma()方法中的语句
以上两种方法均是通过类去实现一个接口,所以本质上是一样的。
结果:
in method
ma in anonymous inner class
in method
ma in local inner class
2、
代码如下:
package org.jsoft.zuoye;
interface Light{
void shine();}
class Lamp{
public static void on(Light light){
light.shine();
}
}
public class TestLamp {
public static void main(String args[]){
Lamp lamp=new Lamp();
//1,使用局部内部类技术,调用lamp的on 方法要求输出 “shine in red”
class InnerClass implements Light{
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void shine() {
// TODO Auto-generated method stub
System.out.println("shine"+" "+color);
}
}
TestLamp teatlamp=new TestLamp();
InnerClass in= teatlamp. new InnerClass();
in.setColor("in red");
lamp.on(new InnerClass());
//1,使用匿名内部类技术,调用lamp的on 方法要求输出 “shine in yellow”
Lamp.on(new Light(){
@Override
public void shine() {
// TODO Auto-generated method stub
System.out.println("shine in yellow");
}
});
}
}
运行结果:
3、
ABC
4、
代码如下:
Hunter类:
package java_1119;
public class Hunter {
private String name;
private int life;//生命值
private String weapon;//武器
private 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) {
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;
}
public void fight(Monster monster){
monster.injured();//调用injured方法,因为只有当life值减少,才会发生攻击时间
monster.getLife();//获取当前生命值
}
public void injured(){
if(life>0&&life<=5){//因为生命值设为5,所以这里<=5;
life--;
System.out.println(name+"已受伤");//生命值每次减少都显示受伤状态
}
}
public void dead(){
if(life==0){
System.out.println(name+"已死亡");//当生命值为0,人物死亡
}
}
public void show(){
System.out.println(name+"当前的生命值为:"+life);
if(life==0){
isLive=false;
System.out.println(name+"已死亡");
}else{
System.out.println(name+"继续存活");
}
}
}
Monster类:
package java_1119;
public class Monster {
private String type;
private boolean isLive;
private int life;
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(){
if(life>0&&life<=5){
life--;
System.out.println(type+"已受伤");
}
}
public void kill( Hunter hunter){
hunter.injured();
hunter.getLife();
}
public void dead(){
if(life==0){
System.out.println(type+"已死亡");
}
}
public void show(){
System.out.println(type+"当前的生命值为"+life);
if(life==0){
isLive=false;
System.out.println(type+"已死亡");
}else{
System.out.println(type+"继续存活");
}
}
}
测试类:
package java_1119;
public class TestGame {
public static void main(String[] args) {
Hunter h=new Hunter();
Monster m=new Monster();
h.setName("猎人");
h.setLife(5);
h.setWeapon("枪");
m.setType("僵尸");
m.setLife(5);
for(int i = 0;i<5;i++){
System.out.println("猎人打了僵尸");
if(h.getLife()<=5){//猎人的生命值为5
h.fight(m);
h.dead();
m.show();//调用Monster里面的方法是因为,猎人先打僵尸是僵尸生命值先减少
}
if(m.getLife()<5&&m.getLife()>0){//不可以等于5是因为只有僵尸生命值不为最大值时才可以反击
System.out.println("僵尸反击猎人");
m.kill(h);
m.dead();
h.show();//僵尸反击则猎人生命值减少,所以调用Hunter 中的方法
}
}
}
}
结果为: