转载于:http://quicker.iteye.com/blog/603935
- package
com.lwf.design.learn; -
- public
abstract class Duck { -
-
Flyable flyable; -
Quackable quackable; -
-
public void swim(){ -
System.out.println("duck is swim"); -
} -
public abstract void display(); -
-
public void setFlayable(Flyable flyable){ -
this.flyable = flyable; -
} -
-
public void setQuackable(Quackable quackable){ -
this.quackable = quackable; -
} -
-
public void perFormFly(){ -
flyable.fly(); -
} -
-
public void perFormQuack(){ -
quackable.quack(); -
} -
- }
- package
com.lwf.design.learn; -
- public
class SubDuck extends Duck { -
-
public SubDuck(){ -
flyable = new NomorFly(); -
quackable = new GaQuack(); -
} -
public void display(){ -
System.out.println("subberDuck is display"); -
} -
-
-
public static void main(String[] args) { -
SubDuck s = new SubDuck(); -
s.display(); -
s.swim(); -
s.perFormFly(); -
s.perFormQuack(); -
-
s.setFlayable(new HighFly()); -
s.perFormFly(); -
- //
Quackable quackable = new GaQuack(); - //
s.quackable = quackable; - //
s.perFormQuack(); -
} - }
- package
com.lwf.design.learn; -
- public
class NomorFly implements Flyable { -
-
public void fly() { -
System.out.println("Normal fly"); -
} -
- }
- package
com.lwf.design.learn; -
- public
class HighFly implements Flyable { -
-
public void fly() { -
System.out.println("HighFly fly"); -
} -
- }
- package
com.lwf.design.learn; -
- public
class GaQuack implements Quackable { -
-
public void quack() { -
System.out.println("GaQuack"); -
} -
- }
- package
com.lwf.design.learn; -
- public
class GuQuack implements Quackable { -
-
public void quack() { -
System.out.println("GuQuack"); -
} -
- }