先附上书上的demo:
class Processor{ public String name(){ return getClass().getSimpleName(); } Object process(Object input){ return input; } } class Upcase extends Processor{ String process(Object input){ return ((String)input).toUpperCase(); } } class Downcase extends Processor{ String process(Object input){ return ((String)input).toLowerCase(); } } class Splitter extends Processor{ String process(Object input){ return Arrays.toString(((String)input).split(" ")); } } public class Apply { public static void process(Processor p, Object s){ System.out.println("Using Processor " + p.name()); System.out.println(p.process(s)); } public static String s = "Disagreement with beliefs is by definition incorrect"; public static void main(String[] args) { process(new Upcase(), s); process(new Downcase(), s); process(new Splitter(), s); } }
解释:在面向对象程序设计中,协变返回类型指的是子类中的成员函数的返回值类型不必严格等同于父类中被重写的成员函数的返回值类型,而可以是更 "狭窄" 的类型。
Java 5.0添加了对协变返回类型的支持,即子类覆盖(即重写)基类方法时,返回的类型可以是基类方法返回类型的子类。协变返回类型允许返回更为具体的类型。
以上解释来自:http://www.cnblogs.com/NotOnlyAnAnswer/p/3538539.html
创建一个能够根据所传递的参数对象的不同而具有不同行为的方法,被称为策略设计模式。这类方法包含所要执行的算法中固定不变的部分,而“策略”包含变化的部分。策略就是传递进去的参数对象,它包含要执行的代码。这里,Processor对象就是一个策略,在main()中可以看到有三种不同类型的策略应用到了String类型的s对象上。