//: Adventure.java
package javabasic;
interface CanFight
{
void fight();
}
interface CanSwim
{
void swim();
}
interface CanFly
{
void fly();
}
class ActionCharacter
{
public void fight()
{
System.out.println("ActionCharacter fight");
}
}
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly
{
public void swim()
{
System.out.println("Hero swim");
}
public void fly()
{
System.out.println("Hero fly");
}
}
public class Adventure
{
public static void t(CanFight x)//接口可以作为函数参数,但是在对象的使用中,要明确在哪个类里实现,所以可以用类来取代接口从而传递参数******************
{
x.fight();
}
public static void u(CanSwim x)
{
x.swim();
}
public static void v(CanFly x)
{
x.fly();
}
public static void w(ActionCharacter x)
{
x.fight();
}
public static void main(String[] args)
{
Hero h = new Hero();
t(h); // 把h当作CanFight,用对象取代接口******************
u(h); // 把h当作CanSwim,用对象取代接口******************
v(h); // 把h当作CanFly,用对象取代接口******************
w(h); // 把h当作ActionCharacter,用对象取代接口******************
}
}