多态性和预验证类属性

时间:2011-11-29 17:02:49

标签: java

基本上我的任务是解决以下问题:

当您设计类/ es时,您必须决定要包含哪些属性和方法。例如,如果您决定以毫米为单位工作,则size变量(长度)可以是类型整数(但稍后,在计算成本时,您必须将体积转换为平方英寸,因为每立方英寸塑料的成本(课程表2和表3))。使用的塑料材料的体积将是管道的外部和内部体积之间的差异。如果您决定以米为单位提示长度,则类型应为双倍或浮动等。
验证用户订单后,您的程序应根据表1确定订购管道的类型 表1.可用塑料管的类型。

Type Plastic’s grade    Colour print    Inner insulation    Outer reinforcement Chemical resistance
        0     1 2           
I   1 – 3   YES NO  NO  NO  NO  YES/NO
II  2 – 4   NO  YES NO  NO  NO  YES/NO
III 2 – 5   NO  NO  YES NO  NO  YES/NO
IV  2 – 5   NO  NO  YES YES NO  YES/NO
V   3 – 5   NO  NO  YES YES YES YES/NO

这一切都很好,但是让我这一点的部分就在这里:

在您的主类中,您已确定客户端的订单是I类型的管道,然后您可以创建TypeI的对象,对于此对象,您可以调用cost()方法来计算成本和向用户显示。

基本上要求在确定需要实例化哪一个之前不实例化任何对象,这在将验证中的一个大if语句分类为“强力方法”时很难。

这是我到目前为止所拥有的。

主要

public class Cw1 {
/**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
    ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
    // TODO code application logic here
    Grade g1 = new Grade(1,3,true,false,false,false,false);
    Grade g2 = new Grade(2,4,false,true,false,false,false);
    Grade g3 = new Grade(2,5,false,false,true,false,false);
    Grade g4 = new Grade(2,5,false,false,true,true,false);
    Grade g5 = new Grade(3,5,false,false,true,true,true);
    pipeList.add(g1);
    pipeList.add(g2);
    pipeList.add(g3);
    pipeList.add(g4);
    pipeList.add(g5);

    for (Pipe p: pipeList)
    {
        p.setGrade(1);
        p.setColour0(false);
        p.setColour1(false);
        p.setColour2(true);
        p.setIns(true);
        p.setReinf(true);
        p.validate();
    }
}
}

等级(必须在解决方案中抽象)

public class Grade extends Pipe {

public Grade(int minGrade, int maxGrade, boolean hasColour0, boolean hasColour1, boolean hasColour2, boolean hasIns, boolean hasReinf) {
    super(minGrade, maxGrade, hasColour0, hasColour1, hasColour2, hasIns, hasReinf);
}    


}

和管道

public abstract class Pipe {

public boolean isChemRes() {
    return chemRes;
}

public void setChemRes(boolean chemRes) {
    this.chemRes = chemRes;
}

public boolean isColour0() {
    return colour0;
}

public void setColour0(boolean colour0) {
    this.colour0 = colour0;
}

public boolean isColour1() {
    return colour1;
}

public void setColour1(boolean colour1) {
    this.colour1 = colour1;
}

public boolean isColour2() {
    return colour2;
}

public void setColour2(boolean colour2) {
    this.colour2 = colour2;
}

public double getDiameter() {
    return diameter;
}

public void setDiameter(double diameter) {
    this.diameter = diameter;
}

public boolean isIns() {
    return ins;
}

public void setIns(boolean ins) {
    this.ins = ins;
}

public double getLength() {
    return length;
}

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

public boolean isReinf() {
    return reinf;
}

public void setReinf(boolean reinf) {
    this.reinf = reinf;
}

public Pipe(int minGrade, int maxGrade, boolean hasColour0, boolean hasColour1, boolean hasColour2, boolean hasIns, boolean hasReinf) {
    this.minGrade = minGrade;
    this.maxGrade = maxGrade;
    this.hasColour0 = hasColour0;
    this.hasColour1 = hasColour1;
    this.hasColour2 = hasColour2;
    this.hasIns = hasIns;
    this.hasReinf = hasReinf;
}



public Pipe() {
}
//<editor-fold desc="Class variables">
private int grade;
private double length, diameter;
private boolean colour0, colour1, colour2, ins, reinf, chemRes;
private int minGrade, maxGrade;
private boolean hasColour0, hasColour1, hasColour2, hasIns, hasReinf; 
// </editor-fold>
public int getGrade() {
    return grade;
}

public void setGrade(int grade) {
    this.grade = grade;
}    
//<editor-fold desc="Public Methods">

public double calcVol()
{
    return 0;
}

public double calcCost()
{
    return 0;
}
public void validate()
{
    if ((grade >= minGrade && grade <= maxGrade) & (colour0 == true && hasColour0 || colour1 == true && hasColour1 || colour2 == true && hasColour2) && (ins == hasIns) && (reinf == hasReinf))
    {
        System.out.print("True");
    }
    else
    {
        System.out.print("False");
    }
}
// </editor-fold>

}

所以基本上,我不明白如何在不事先实例化对象并验证它们的情况下实现相同的结果?

班级不高,我们刚刚学过多态。

1 个答案:

答案 0 :(得分:1)

通常,告诉您创建哪些对象的数据来自外部源:文件,套接字,另一个对象等。在您的情况下,您可以使用文本文件。创建将您读取的值传递给构造函数的Grade个实例,然后在每个实例上调用validatecost

public class PipeFactory(){
    public Pipe CreatePipe( int minGrade, int maxGrade, boolean hasColour0, boolean hasColour1, boolean hasColour2, boolean hasIns, boolean hasReinf ){
       if( (minGrade == 1 || maxGrade == 3) /* ... Complete this condition yourself */ )
           return new TypeIPipe();
       if( (minGrade == 2 || maxGrade == 4 /* ... Complete this condition yourself */ )
           return new TypeIIPipe();
       //If for other types...
       //If no pipe was created, parameters are invalid, so we throw an exception
       throw new InvalidArgumentException( "Can't create a pipe with these parameters" );
    } 
}