今天进行了软件设计没有完成的实验
package test25; public class Apple implements Product { @Override public void accept(Visitor visitor) { visitor.visit(this); } } package test25; public class Book implements Product{ @Override public void accept(Visitor visitor) { visitor.visit(this); } } package test25; import java.util.ArrayList; import java.util.Iterator; public class BuyBasket{ private ArrayList list = new ArrayList(); public void accept(Visitor visitor){ Iterator iterator = list.iterator(); while (iterator.hasNext()){ ((Product)iterator.next()).accept(visitor); } } public void addProduce(Product product){ list.add(product); } public void removeProduce(Product product){ list.remove(product); } } package test25; public class Client { public static void main(String[] args) { Product b1 = new Book(); Product b2 = new Book(); Product a1 = new Apple(); Visitor visitor; BuyBasket basket = new BuyBasket(); basket.addProduce(b1); basket.addProduce(b2); basket.addProduce(a1); visitor = new Packager(); visitor.setName("张三"); basket.accept(visitor); } } package test25; public class Customer extends Visitor{ @Override public void visit(Apple apple) { System.out.println("顾客"+name+"选苹果"); } @Override public void visit(Book book) { System.out.println("顾客"+name+"买书"); } } package test25; public class Packager extends Visitor{ @Override public void visit(Apple apple) { System.out.println("打包员"+name+"打包苹果"); } @Override public void visit(Book book) { System.out.println("打包员"+name+"打包书籍"); } } package test25; public interface Product { void accept(Visitor visitor); } package test25; public class Saler extends Visitor{ @Override public void visit(Apple apple) { System.out.println("收银员"+ name+"给苹果过称,计算价格"); } @Override public void visit(Book book) { System.out.println("收银员"+name+"直接计算书的价格"); } } package test25; public abstract class Visitor { protected String name; public void setName(String name){ this.name = name; } public abstract void visit(Apple apple); public abstract void visit(Book book); }
标签:总结,test25,name,visitor,每日,visit,public,12.10,void From: https://www.cnblogs.com/louwangshayu/p/17893198.html