WEB开发网
开发学院软件开发Java hand first 设计模式 -组合模式-1 阅读

hand first 设计模式 -组合模式-1

 2009-09-17 00:00:00 来源:WEB开发网   
核心提示: //菜单项 Java代码publicclassMenuItemextendsMenuComponent{Stringname;Stringdescription;booleanvegetarian;doubleprice;publicMenuItem(Stringname,Stringdescr

//菜单项

Java代码   

public class MenuItem extends MenuComponent { 
 String name; 
 String description; 
 boolean vegetarian; 
 double price; 
   
 public MenuItem(String name, 
         String description, 
         boolean vegetarian, 
         double price) 
 { 
 this.name = name; 
 this.description = description; 
 this.vegetarian = vegetarian; 
 this.price = price; 
 } 
  
 public String getName() { 
 return name; 
 } 
  
 public String getDescription() { 
 return description; 
 } 
  
 public double getPrice() { 
 return price; 
 } 
  
 public boolean isVegetarian() { 
 return vegetarian; 
 } 
  
 public void print() { 
 System.out.print(" " + getName()); 
 if (isVegetarian()) { 
  System.out.print("(v)"); 
 } 
 System.out.println(", " + getPrice()); 
 System.out.println("   -- " + getDescription()); 
 } 
}

//服务员

Java代码   

public class Waitress { 
 MenuComponent allMenus; 
 
 public Waitress(MenuComponent allMenus) { 
 this.allMenus = allMenus; 
 } 
 
 public void printMenu() { 
 allMenus.print(); 
 } 
}

上一页  1 2 3 4  下一页

Tags:hand first 设计模式

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接