1 public class Ornament { 2 3 public static void main(String[] args) { 4 Personnel personnel = new Personnel(); 5 CompanyA companyA = new CompanyA(); 6 CompanyB companyB = new CompanyB(); 7 companyA.MakeUp(personnel); 8 companyA.logo(); 9 companyA.per();10 companyB.MakeUp(personnel);11 companyB.logo();12 companyB.per();13 }14 }15 //人员类16 class Personnel{17 public void per() {18 System.out.println("在工作");19 }20 }21 //公司类22 class Company extends Personnel{23 protected Personnel personnel;24 public void MakeUp(Personnel personnel) {25 this.personnel = personnel;26 }27 public void per() {28 if(personnel != null) personnel.per();29 }30 }31 //A公司32 class CompanyA extends Company{33 34 public void logo() {35 System.out.println("A公司");36 }37 public void work() {38 System.out.print("UI");39 }40 public void per() {41 work();42 super.per();43 }44 }45 //B公司46 class CompanyB extends Company{47 48 public void logo() {49 System.out.println("B公司");50 }51 public void work() {52 System.out.print("Java");53 }54 public void per() {55 work();56 super.per();57 }58 59 }
输出: