用配置文件 获取计算面积

mmmm0303 2020-06-02 10:11:19


如图 我有个问题就是 这矩形有长和宽 圆形只有半径 那配置文件要怎么读取 正常来说配置文件里面的不应该是通用的吗?有大佬教一下应该怎么写
...全文
241 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmmm0303 2020-06-02
  • 打赏
  • 举报
回复
package Test; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Volume { public static void main(String[] args) { Pillar pillar=new Pillar(); double height; String bottom=null; String length=null; String width=null; Properties prop=new Properties(); try { InputStream InputStream= new BufferedInputStream(new FileInputStream(new File("D:\\JAVAPROJECT\\TEMPWORKSPACE\\sss\\src\\Test\\Test.properties"))); prop.load(InputStream); bottom=prop.getProperty("shape"); length=prop.getProperty("length"); width=prop.getProperty("width"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Class c=null; Geometry geometry=null; try { c=Class.forName(bottom); geometry=(Geometry)c.newInstance(); geometry.setLength(Double.valueOf(length)); geometry.setWidth(Double.valueOf(width)); }catch(ClassNotFoundException e){ e.printStackTrace(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } pillar.calPillar(geometry,5); System.out.println("体积是:"+pillar.getVolume()); } } /*接口Geomotry*/ abstract class Geometry{ public abstract double getArea(); public abstract double setLength(double a); public abstract double setWidth(double b); } /*定义Pillar类*/ class Pillar{ private Geometry bottom; private double height; public void calPillar(Geometry bottom,double height) { this.bottom=bottom; this.height=height; } public Pillar() { } public double getVolume() { return bottom.getArea()*height; } } /*定义Circle类实现接口*/ class Circle extends Geometry{ private double r; private double area; public double getArea() { area=r*r*Math.PI; return area; } @Override public double setLength(double r) { this.r=r; return r; } @Override public double setWidth(double r) { this.r=r; return r; } } /*定义Rectangle类*/ class Rectangle extends Geometry{ private double a; private double b; private double area; @Override public double getArea() { area=a*b; return area; } @Override public double setLength(double a) { this.a=a; return a; } @Override public double setWidth(double b) { this.b=b; return b; } } 这怎么解决???
tianfang 2020-06-02
  • 打赏
  • 举报
回复
输入值放在配置文件里 运行时从配置读输入值,计算,输出 配置文件可以是: width=100 height=60 r=20 配置文件可以修改,代替键盘输入
冰思雨 2020-06-02
  • 打赏
  • 举报
回复
Volume.java

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;

public class Volume {
    // 测试用例
    public static void main(String[] args)  {
        // 1. 读取配置文件
        Properties prop = new Properties();
        try {
            prop.load(Volume.class.getClassLoader().getResourceAsStream("Test.properties"));
//            prop.load(new FileInputStream("src/test/resources/Test.properties"));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        // 2. 读取配置信息
        final String KEY_IMPL_CLASS = "Geometry.implementation";
        String implClassName = prop.getProperty(KEY_IMPL_CLASS);
        Geometry implOjbect = null;
        try {
            // 3. 找到配置的实现类
            Class<? extends Geometry> implClass = (Class<? extends Geometry>) Class.forName(implClassName);
            implOjbect = implClass.newInstance();
            for (Field field : implClass.getDeclaredFields()) {
                // 4. 分析实现类的属性结构,查找属性的配置信息。
                String keyFieldName  = implClassName + "." + field.getName();
                String strValueField = prop.getProperty(keyFieldName);
                // 5. 设置属性值
                setFieldValue(implOjbect, field, strValueField);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        double height = 5;
        Pillar pillar = new Pillar(implOjbect, height);
        System.out.println("体积是:"+pillar.getVolume());
    }

    private static void setFieldValue(Geometry implOjbect, Field field, String strValueField) throws IllegalAccessException {
        // 将配置文件中的配置信息设置到对象当中
        // 由于本例中,所有的属性类型都是double类型,就没有对属性类型进行区分转换,统一将string转成double
        field.setAccessible(true);
        field.set(implOjbect, Double.parseDouble(strValueField));
    }

}


/*接口Geomotry*/
interface Geometry{
    double getArea();
}

/*定义Pillar类*/
class Pillar{
    private final Geometry bottom;
    private final double height;

    public Pillar(Geometry bottom,double height) {
        this.bottom=bottom;
        this.height=height;
    }

    public double getVolume() {
        return bottom.getArea()*height;

    }
}

/*定义Circle类实现接口*/
class Circle implements Geometry{

    private double radius;

    public double getArea() {
        return radius * radius * Math.PI;
    }

    public void setRadius(double r) {
        this.radius=r;
    }

}

/*定义Rectangle类*/
class Rectangle implements Geometry{

    private double length;
    private double width;

    public double getArea() {
        return length * width;
    }

    public void setLength(double a) {
        this.length = a;
    }

    public void setWidth(double b) {
        this.width=b;
    }

}

Test.properties
## 配置文件的使用:
## Geometry.implementation 指定实现类的名字
## 实现类的名字作为前缀,加上点号,加上属性名 整体作为key,设置属性值
## 一个配置文件只允许出现一个 Geometry.implementation ,即只能有一个实现类的配置会生效。

Geometry.implementation=Circle
Circle.radius=2

#Geometry.implementation=Rectangle
Rectangle.length=3
Rectangle.width=4
qybao 2020-06-02
  • 打赏
  • 举报
回复
通过反射的方式,类名的信息一定要完整,也就是包括包名信息
即shape=package.class,对于你的例子,就是shape=Test.Rectangle,你的package是Test
qq_39936465 2020-06-02
  • 打赏
  • 举报
回复
引用 2 楼 mmmm0303 的回复:
这怎么解决???
测试了一下,改为下面应该也可以,主要是找不到类。 shape=test.Rectangle length=5 width=3
qq_39936465 2020-06-02
  • 打赏
  • 举报
回复
引用 2 楼 mmmm0303 的回复:
这怎么解决???
为什么不用springbean

public interface Geometry {
	public double getArea();
}

public class Circle implements Geometry {
	private double r;
	final double π=3.1415926;
	
	public double getArea() {
		return π*r*r;
	}

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}

}

public class Rectangle implements Geometry {
	private double height;
	private double weight;
	
	public double getArea() {
		return height*weight;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

}

public class BeanTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		Geometry g1=ctx.getBean("circle", Geometry.class);
		System.out.println("圆面积为:"+g1.getArea());
		Geometry g2=ctx.getBean("rectangle", Geometry.class);
		System.out.println("长方形面积为:"+g2.getArea());
	}

}


<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/util
	http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<bean id="circle" class="domain.Circle">
		<property name="r" value="3.0" />
	</bean>
	<bean id="rectangle" class="domain.Rectangle">
		<property name="height" value="4.0"  />
		<property name="weight" value="5.0" />
	</bean>
</beans>

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧