黑马程序员技术交流社区
标题:
异常连篇,求此代码优化改进.....
[打印本页]
作者:
jonn
时间:
2013-1-24 14:45
标题:
异常连篇,求此代码优化改进.....
本帖最后由 张向辉 于 2013-1-25 09:48 编辑
import java.util.*;
import java.io.*;
import java.io.File;
interface Car
{
public void run();
public void stop();
};
class Bus implements Car
{
public void run(){
System.out.println("Bus is starting run...");
}
public void stop(){
System.out.println("Bus is stoping run...");
}
};
class MinCar implements Car
{
public void run(){
System.out.println("MinCar is starting run...");
}
public void stop(){
System.out.println("MinCar is stoping run...");
}
};
/*
class Factory
{
public static Car car=null;
public static Car getCar(int i){
switch(i){
case 1: car=new Bus();
break;
case 2: car=new MinCar();
break;
}
return car;
}
};
*/
class FactoryCar{
public static Car car=null;
public static Properties properties=new Properties();
public static File file=null;
public static Car getCar(String carType) throws Exception{
InputStream in=null;
try{
in=new FileInputStream("d:\\car.properties");
}catch(FileNotFoundException e){
file=new File("d:"+File.separator+"car.properties");
}
properties.load(in);
String carClass=properties.getProperty(carType);
if(carClass==null){
throw new RuntimeException("获得值为null");
}else{
car=(Car)Class.forName(carClass).newInstance();
}
return car;
}
};
class FactoryDemo
{
public static void main(String[] args) throws Exception{
Object obj=FactoryCar.getCar("minicar");
if(obj==null){
System.out.println("工厂没有为你造出车来....");
}
else{
Car car=(Car)obj;
car.run();
car.stop();
}
}
};
复制代码
作者:
vmvm555
时间:
2013-1-24 16:25
改了好多地方,总算能运行了,你对着看吧
import java.util.*;
import java.io.*;
import java.io.File;
interface Car
{
public void run();
public void stop();
};
class Bus implements Car
{
public void run(){
System.out.println("Bus is starting run...");
}
public void stop(){
System.out.println("Bus is stoping run...");
}
};
class MinCar implements Car
{
public MinCar() {
}
public void run(){
System.out.println("MinCar is starting run...");
}
public void stop(){
System.out.println("MinCar is stoping run...");
}
};
/*
class Factory
{
public static Car car=null;
public static Car getCar(int i){
switch(i){
case 1: car=new Bus();
break;
case 2: car=new MinCar();
break;
}
return car;
}
};
*/
class FactoryCar{
public static Car car=null;
public static Properties properties=new Properties();
public static File file=null;
public static Car getCar(String carType) throws Exception{
InputStream in=null;
try{
in=new FileInputStream("d:\\car.properties");
} catch(FileNotFoundException e){
file=new File("d:"+File.separator+"car.properties");
// file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write((carType + "=bmw").getBytes());
fos.flush();
in = new FileInputStream(file);
fos.close();
}
properties.load(in);
String carClass=properties.getProperty(carType);
if(carClass==null){
throw new RuntimeException("获得值为null");
}else{
car=(Car)Class.forName("MinCar").getConstructor(null).newInstance(); //没有carClass这个类,帮你改成minicar
}
return car;
}
};
public class FactoryDemo
{
public static void main(String[] args) throws Exception{
Object obj=FactoryCar.getCar("minicar");
if(obj==null){
System.out.println("工厂没有为你造出车来....");
}
else{
Car car=(Car)obj;
car.run();
car.stop();
}
}
};
复制代码
作者:
jonn
时间:
2013-1-24 17:44
vmvm555 发表于 2013-1-24 16:25
改了好多地方,总算能运行了,你对着看吧
谢谢了....:handshake
作者:
刘军亭
时间:
2013-1-24 18:38
/*
* 异常的处理我还没有学好,我只是做了注释,异常的处理我还不知道怎么能更好的处理,只听老师说过要是在真实的项目中出现的异常要提示用户的。希望能给分啊,哈哈
*
* */
package cn.liu.javaenhance;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//汽车的接口类,
interface Car
{
public void run();
public void stop();
//}; //这里没必要加分号,下面也一样
}
//汽车类的子类,后来又出现的新型汽车
class Bus implements Car
{
public void run(){
System.out.println("Bus is starting run...");
}
public void stop(){
System.out.println("Bus is stoping run...");
}
}
//汽车类的子类,后来又出现的新型汽车
class MinCar implements Car
{
public void run(){
System.out.println("MinCar is starting run...");
}
public void stop(){
System.out.println("MinCar is stoping run...");
}
}
//汽车工厂
class FactoryCar{
//此处没有必要声明为静态的全局对象
/*
public static Car car=null;
public static Properties properties=new Properties();
public static File file=null;
*/
public static Car getCar(String carType) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Car car=null;
//创建properties对象为加载配置文件做准备
Properties pro = new Properties();
File file=null;
//声明创建读取流文件的对象
InputStream in=null;
try{
in=new FileInputStream("d:\\car.properties");//这个文件应该放在软件的安装目录下面,前面的地址应该是get出来的,不是硬编码。我现在还不会弄。
pro.load(in);
}catch(FileNotFoundException e){
file=new File("d:"+File.separator+"car.properties");//暂时没明白这句话得意思。
}catch(IOException e){
//处理出现IO异常的代码
}
finally{
//最后一定在finally记得要关闭流,因为finally是必会执行的所以要在finally里关闭流。
try {
if(in != null){//关闭流之前一定要判断,流是否创建成功。
in.close();
}
} catch (Exception e2) {
//如果流关闭失败时候要处理的代码
}
}
String carClass=pro.getProperty(carType);
if(carClass==null){
throw new RuntimeException("获得值为null");
}else{
car=(Car)Class.forName(carClass).newInstance();//这句话好像也有在这个类中需要处理的异常
}
return car;
}
}
//汽车销售中心
public class FactoryDemo
{
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
//此处的汽车名称应该也是在文件中加载会比较好些
Car car = FactoryCar.getCar("minicar");
if(car == null){
System.out.println("工厂没有为你造出车来....");
}
else{
car.run();
car.stop();
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2