A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jonn 高级黑马   /  2013-1-24 14:45  /  1102 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张向辉 于 2013-1-25 09:48 编辑
  1. import java.util.*;
  2. import java.io.*;
  3. import java.io.File;
  4. interface Car
  5. {
  6.         public void run();
  7.         public void stop();
  8. };
  9. class Bus implements Car
  10. {
  11.         public void run(){
  12.                 System.out.println("Bus is starting run...");
  13.         }
  14.         public void stop(){
  15.                 System.out.println("Bus is stoping run...");
  16.         }
  17. };
  18. class MinCar implements Car
  19. {
  20.         public void run(){
  21.                 System.out.println("MinCar is starting run...");
  22.         }
  23.         public void stop(){
  24.                 System.out.println("MinCar is stoping run...");
  25.         }
  26. };
  27. /*
  28. class Factory
  29. {
  30.    public static Car car=null;
  31.    public static Car getCar(int i){
  32.            switch(i){
  33.          case 1: car=new Bus();
  34.                          break;
  35.                  case 2: car=new MinCar();
  36.                          break;
  37.            }
  38.            return car;
  39.    }
  40. };
  41. */
  42. class FactoryCar{
  43.         public static Car car=null;
  44.         public static Properties properties=new Properties();
  45.         public static File file=null;
  46.         public static Car getCar(String carType) throws Exception{
  47.                 InputStream in=null;
  48.                 try{
  49.                         in=new FileInputStream("d:\\car.properties");
  50.                 }catch(FileNotFoundException e){
  51.                         file=new File("d:"+File.separator+"car.properties");
  52.                 }
  53.     properties.load(in);
  54.         String carClass=properties.getProperty(carType);
  55.         if(carClass==null){
  56.                 throw new RuntimeException("获得值为null");        
  57.         }else{
  58.         car=(Car)Class.forName(carClass).newInstance();
  59.         }
  60.         return car;
  61.         }
  62. };
  63. class FactoryDemo
  64. {
  65.         public static void main(String[] args) throws Exception{
  66.                 Object obj=FactoryCar.getCar("minicar");
  67.                 if(obj==null){
  68.                         System.out.println("工厂没有为你造出车来....");
  69.                 }
  70.                 else{
  71.                         Car car=(Car)obj;
  72.                         car.run();
  73.                         car.stop();
  74.                 }
  75.         }
  76. };
复制代码

评分

参与人数 1黑马币 +6 收起 理由
Rancho_Gump + 6

查看全部评分

3 个回复

倒序浏览
改了好多地方,总算能运行了,你对着看吧
  1. import java.util.*;
  2. import java.io.*;
  3. import java.io.File;
  4. interface Car
  5. {
  6.         public void run();
  7.         public void stop();
  8. };
  9. class Bus implements Car
  10. {
  11.         public void run(){
  12.                 System.out.println("Bus is starting run...");
  13.         }
  14.         public void stop(){
  15.                 System.out.println("Bus is stoping run...");
  16.         }
  17. };
  18. class MinCar implements Car
  19. {
  20.                
  21.         public MinCar() {
  22.                
  23.         }
  24.                 public void run(){
  25.                 System.out.println("MinCar is starting run...");
  26.         }
  27.         public void stop(){
  28.                 System.out.println("MinCar is stoping run...");
  29.         }
  30. };
  31. /*
  32. class Factory
  33. {
  34.    public static Car car=null;
  35.    public static Car getCar(int i){
  36.            switch(i){
  37.          case 1: car=new Bus();
  38.                          break;
  39.                  case 2: car=new MinCar();
  40.                          break;
  41.            }
  42.            return car;
  43.    }
  44. };
  45. */
  46. class FactoryCar{
  47.         public static Car car=null;
  48.         public static Properties properties=new Properties();
  49.         public static File file=null;
  50.         public static Car getCar(String carType) throws Exception{
  51.                 InputStream in=null;
  52.                 try{
  53.                         in=new FileInputStream("d:\\car.properties");
  54.                   } catch(FileNotFoundException e){
  55.                         file=new File("d:"+File.separator+"car.properties");
  56.                        // file.createNewFile();
  57.                         FileOutputStream fos = new FileOutputStream(file);
  58.                         fos.write((carType + "=bmw").getBytes());
  59.                         fos.flush();
  60.                         in = new FileInputStream(file);
  61.                         fos.close();
  62.                   }
  63.                 properties.load(in);
  64.         String carClass=properties.getProperty(carType);
  65.         if(carClass==null){
  66.                 throw new RuntimeException("获得值为null");        
  67.         }else{
  68.         car=(Car)Class.forName("MinCar").getConstructor(null).newInstance();                //没有carClass这个类,帮你改成minicar
  69.         }
  70.         return car;
  71.         }
  72. };
  73. public class FactoryDemo
  74. {
  75.         public static void main(String[] args) throws Exception{
  76.                 Object obj=FactoryCar.getCar("minicar");
  77.                 if(obj==null){
  78.                         System.out.println("工厂没有为你造出车来....");
  79.                 }
  80.                 else{
  81.                         Car car=(Car)obj;
  82.                         car.run();
  83.                         car.stop();
  84.                 }
  85.         }
  86. };
复制代码

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 赞一个!

查看全部评分

回复 使用道具 举报
vmvm555 发表于 2013-1-24 16:25
改了好多地方,总算能运行了,你对着看吧

谢谢了....:handshake
回复 使用道具 举报
  1. /*
  2. * 异常的处理我还没有学好,我只是做了注释,异常的处理我还不知道怎么能更好的处理,只听老师说过要是在真实的项目中出现的异常要提示用户的。希望能给分啊,哈哈
  3. *
  4. * */

  5. package cn.liu.javaenhance;

  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.util.Properties;

  12. //汽车的接口类,
  13. interface Car
  14. {
  15.         public void run();
  16.         public void stop();
  17.        
  18. //};                   //这里没必要加分号,下面也一样
  19. }

  20. //汽车类的子类,后来又出现的新型汽车
  21. class Bus implements Car
  22. {
  23.         public void run(){
  24.                
  25.                 System.out.println("Bus is starting run...");
  26.         }
  27.         public void stop(){
  28.                
  29.                 System.out.println("Bus is stoping run...");
  30.         }
  31. }

  32. //汽车类的子类,后来又出现的新型汽车
  33. class MinCar implements Car
  34. {
  35.         public void run(){
  36.        
  37.                 System.out.println("MinCar is starting run...");
  38.         }
  39.         public void stop(){

  40.                 System.out.println("MinCar is stoping run...");
  41.         }
  42. }
  43. //汽车工厂
  44. class FactoryCar{
  45.        
  46.         //此处没有必要声明为静态的全局对象
  47.         /*
  48.         public static Car car=null;
  49.         public static Properties properties=new Properties();
  50.         public static File file=null;
  51.         */
  52.        
  53.         public static Car getCar(String carType) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  54.                
  55.                 Car car=null;
  56.                
  57.                 //创建properties对象为加载配置文件做准备
  58.                 Properties pro = new Properties();
  59.                
  60.                 File file=null;
  61.                
  62.                 //声明创建读取流文件的对象
  63.                 InputStream in=null;
  64.                
  65.                 try{
  66.                        
  67.                         in=new FileInputStream("d:\\car.properties");//这个文件应该放在软件的安装目录下面,前面的地址应该是get出来的,不是硬编码。我现在还不会弄。
  68.                         pro.load(in);
  69.                        
  70.                 }catch(FileNotFoundException e){
  71.                         file=new File("d:"+File.separator+"car.properties");//暂时没明白这句话得意思。
  72.                
  73.                 }catch(IOException e){
  74.                         //处理出现IO异常的代码
  75.                 }
  76.                 finally{
  77.                         //最后一定在finally记得要关闭流,因为finally是必会执行的所以要在finally里关闭流。
  78.                         try {
  79.                                
  80.                                 if(in != null){//关闭流之前一定要判断,流是否创建成功。
  81.                                         in.close();
  82.                                 }
  83.                                
  84.                         } catch (Exception e2) {
  85.                                 //如果流关闭失败时候要处理的代码
  86.                         }
  87.                        
  88.                 }
  89.        
  90.                 String carClass=pro.getProperty(carType);

  91.                 if(carClass==null){

  92.                         throw new RuntimeException("获得值为null");        

  93.                 }else{

  94.                         car=(Car)Class.forName(carClass).newInstance();//这句话好像也有在这个类中需要处理的异常

  95.                 }
  96.                 return car;
  97.         }
  98. }

  99. //汽车销售中心
  100. public class FactoryDemo
  101. {
  102.         public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
  103.                
  104.                 //此处的汽车名称应该也是在文件中加载会比较好些
  105.                 Car car = FactoryCar.getCar("minicar");
  106.                
  107.                 if(car == null){
  108.                        
  109.                         System.out.println("工厂没有为你造出车来....");
  110.                 }
  111.                 else{
  112.                        
  113.                         car.run();
  114.                         car.stop();
  115.                        
  116.                         }
  117.                 }
  118. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马