public class Adventure {
static void doFight(CanFight x) { x.fight(); }
static void doSwim (CanSwim x) { x.swim(); }
static void doFly (CanFly x) { x.fly(); }
static void doAct (ActionCharacter x) { x.fight(); }
public static void main(String[] args) {
Hero superman = new Hero("hero1");
doFight(superman); // CanFight
doSwim(superman); // CanSwim
doFly(superman); // CanFly
doAct(superman); // ActionCharacter
}
}
interface CanFight {
void fight();
}
interface CanSwim {
void swim();
}
interface CanFly {
void fly();
}
class ActionCharacter {
protected String name;
public ActionCharacter(String n) {
name = n;
}
public void fight() {
System.out.println(name + " fights.");
}
}
class Hero
extends ActionCharacter
implements CanFight, CanSwim, CanFly
{
public Hero(String n) {
super(n);
}
public void swim() {
System.out.println(name + " swims.");
}
public void fly() {
System.out.println(name + " flies.");
}
}
Advanture.java output
hero1 fights.
hero1 swims.
hero1 flies.
hero1 fights.
Tag: Study Code Program Java
No comments:
Post a Comment