0

我是新手程序员,学习使用面向对象的编程概念(继承和重载构造函数方法)

现在我已经创建(主要是复制)一个代码,它将从用户那里获取 5 个输入并显示一个输出。现在使用方法重载它也可以接受 3 个输入并显示它们相同的输出。

现在如何在 Swing GUI 中实现它。图形用户界面是这样的

图形用户界面图像

用户可以给出所有值,也可以只给出宽度、高度和深度中的一个。

主要代码是

`

class box
{
    private double width;
    private double height;
    private double depth;
    box(box ob)
    {
        width =ob.width;
        height=ob.height;
        depth =ob.depth;
    }
    box(double w,double h,double d)

    {
        width=w;
        height=h;
        depth=d;
    }
    box()
    {
        width=-1;
        height=-1;
        depth=-1;
    }
    box(double len)
    {
        width=height=depth=len;
    }
    double volume()
    {
        return width*depth*height;
    }

 }

class boxweight extends box
{
    private double weight;
    boxweight(boxweight ob)
    {
        super(ob);
        weight= ob.weight;
    }
    boxweight(double w,double h,double d,double m)
    {
        super(w,h,d);
        weight=m;
    }

    boxweight(double len,double m)
    {
        super(len);
        weight=m;

    }

    boxweight() 
    {
        super();
        weight=-1;
    }

    double weightcal()
    {
        return (volume()*weight);
    }


}

class shipment extends boxweight
{
    private double cost;

    shipment(shipment ob) 
    {
        super(ob);
        cost=ob.cost;

    }

    shipment(double w,double h,double d,double m,double c) 
    {
        super(w,h,d,m);
        cost=c;
    }
    shipment (double len,double m,double c)
    {
        super(len,m);
        cost=c;

    }

    shipment() 
    {
        super();
        cost=-1;
    }
    double costcal()
    {
        return(weightcal()*cost);
    }


}


public class Inheritanceallhere {

    public static void main(String[] args) {

        shipment o1=new shipment(10,20,30,40,50);
        shipment o3=new shipment(18.17122,40,50);
        double vol;
        vol = o1.volume();
        System.out.println("Volume of shipment1 is " + vol);
        System.out.println("Weight of shipment1 is "+ o1.weightcal());
        System.out.println("Shipping cost: $" + o1.costcal());
        System.out.println();
        vol =o3.volume();
        System.out.println("Volume of shipment2 is " + vol);
        System.out.println("Weight of shipment2 is "+ o3.weightcal());
        System.out.println("Shipping cost: $" + o3.costcal());

    }
}

`

现在我想要什么,当我按下计算按钮时,它将采用可用值并为其分配适当的方法

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       double a,b,c,d,e;
       firstsubclass o2=new firstsubclass();
       a=Double.parseDouble(jTextField1.getText());
       b=Double.parseDouble(jTextField2.getText());
       c=Double.parseDouble(jTextField4.getText());
       d=Double.parseDouble(jTextField5.getText());
       e=Double.parseDouble(jTextField6.getText());
       shipment o3=new shipment(a,b,c,d,e);




       jTextField3.setText(String.valueOf(o3.costcal()));

}

我做了这段代码来获取所有五个输入然后计算。但我不能对三个值做同样的事情(其中任何一个宽度高度或深度字段都可用)。我知道我可以使用一些控制语句来确定哪种方法可以工作,但我想知道 java 中是否有任何动态控制,以便我可以直接执行此操作而无需任何额外代码。

注意:两个代码都在同一个包中

4

2 回答 2

0

我如何制作一个可以理解有多少输入可用的程序并基于此进行计算?

您单独测试每个字段。

if (!jTextField1.getText().trim().equals("")) {
    a = Double.parseDouble(jTextField1.getText().trim());
}

然后测试 Double 值以查看已设置的值。

于 2013-03-04T18:31:28.333 回答
0

我建议您查看 Java 集合框架,特别ArrayList是作为一个用于一般用途的好集合。

http://docs.oracle.com/javase/7/docs/technotes/guides/collections/index.html

如果你的程序有 n 个东西,但在“编译时”你不知道有多少东西,你可以编写你的逻辑来期望一个东西列表,然后循环这个列表。

于 2013-03-04T17:06:34.153 回答