电影Movie类,setPrice(String movieClass)(工厂)方法,通过java反射机制实现movieClass(包名,类名)类。若没有movieClass这个类,则抛出MovieException异常。package com.qujingbo.movie;/** *//** * Title:影片类 * * Description: * * Date:2006-10-14 15:47:55 * * * @author EOMS 曲静波 * @version 1.0 */public class Movie { // 普通片标识 public static String REGULAR = "com.qujingbo.movie.RegularlPrice"; // 新片标识 public static String NEW_RELEASE = "com.qujingbo.movie.NewReleasePrice"; // 儿童片标识 public static String CHILDREN = "com.qujingbo.movie.ChildrenPrice"; private Price price; public Price getPrice() { return price; } /** *//** * 确定返回具体某个影片类型的实现类,有点像工厂 * * @param movieCode * 影片类型 * @throws MovieException * 若无影片类型则抛异常。 */ public void setPrice(String movieClass) throws MovieException { try { Class cls = Class.forName(movieClass); this.price = (Price) cls.newInstance(); } catch (Exception e) { throw new MovieException("影片不存在"); } }}给出MovieException源码。package com.qujingbo.movie;/** *//** * Title:自定义异常 * * Description: * * Date:2006-10-14 19:21:08 * * * @author EOMS 曲静波 * @version 1.0 */public class MovieException extends Exception { public MovieException(String msg) { super(msg); }}下面模访一个顾客租赁影片。package com.qujingbo.movie;/** *//** * Title: * * Description: * * Date:2006-10-14 19:26:23 * * * @author EOMS 曲静波 * @version 1.0 */public class Customer { /** *//** * 消费(测试程序) * * @throws MovieException * 若没有影片,抛出异常 */ public void consume() throws MovieException { // 普通电影 Movie regularMovie = new Movie(); regularMovie.setPrice(Movie.REGULAR); // 最新发布电影 Movie newReleaseMovie = new Movie(); newReleaseMovie.setPrice(Movie.NEW_RELEASE); // 儿童电影 Movie childrenMovie = new Movie(); childrenMovie.setPrice(Movie.CHILDREN); System.out.println("普通影片租赁10天的价格" + regularMovie.getPrice().getCharge(10)); System.out.println("最新影片租赁10天的价格" + newReleaseMovie.getPrice().getCharge(10)); System.out.println("儿童影片租赁10天的价格" + childrenMovie.getPrice().getCharge(10)); System.out.println("普通影片租赁10天的积分" + regularMovie.getPrice().getIntegral(10)); System.out.println("最新影片租赁10天的积分" + newReleaseMovie.getPrice().getIntegral(10)); System.out.println("儿童影片租赁10天的积分" + childrenMovie.getPrice().getIntegral(10)); }}
电影Movie类,setPrice(String movieClass)(工厂)方法,通过java反射机制实现movieClass(包名,类名)类。若没有movieClass这个类,则抛出MovieException异常。
给出MovieException源码。
下面模访一个顾客租赁影片。