标签:java
package test; public class Manager { public static void main(String args[]){ Second s=new Second(); s.getFirst(); } /** * 打印结果: */ } class First{ } class Second{ First f; Second(){ System.out.println("Creating Second"); } First lazy(){ if(f==null){ System.out.println("Creating First"); f=new First(); } return f; } public First getFirst(){ return lazy(); } }
package test; public class Manager { public static void main(String args[]){ Sub s=new Sub(); s.apply();s.dilute();s.foam();s.scrub();s.sterilize(); new print(s); } /** * 打印结果: Cleanser apply() dilute() foam() sub.scrub Detergent.scrub()sub.sterilize() */ } class print{ print(Object obj){ System.out.println(obj); } } class Cleanser{ private String s="Cleanser"; public void append(String a){ s+=a; } public void dilute(){ append(" dilute()"); } public void apply(){ append(" apply()"); } public void scrub(){ append(" scrub() "); } public String toString(){ return s; } public static void main(String[] args){ Cleanser x=new Cleanser(); x.dilute();x.apply();x.scrub(); new print(x); } } class Detergent extends Cleanser{ public void scrub(){ append(" Detergent.scrub()"); } public void foam(){ append(" foam()"); } } class Sub extends Detergent{ public void scrub(){ append(" sub.scrub"); super.scrub(); } public void sterilize(){ append("sub.sterilize()"); } }
标签:java
原文地址:http://blog.csdn.net/ztt_1119/article/details/40898563