如何在Java中使用构造函数

如何在Java中使用构造函数,java,Java,有人能告诉我这个简单的程序有什么问题吗?我得到的输出为“0” 您没有设置length和width的值,默认情况下它们都是0。您可能必须这样做: Doconstructor(int x, int y){ int area; area = x * y; length = x; width = y; System.out.println("Area = "+area); } 您使用的不是传递给构造函数的变量值,而是初始化为0的长度和宽度值。你想要area=x*y。长度和

有人能告诉我这个简单的程序有什么问题吗?我得到的输出为“0”


您没有设置
length
width
的值,默认情况下它们都是
0
。您可能必须这样做:

Doconstructor(int x, int y){
   int area;
   area = x * y;
   length = x;
   width = y;
   System.out.println("Area = "+area);
}

您使用的不是传递给构造函数的变量值,而是初始化为0的长度和宽度值。你想要
area=x*y

长度和宽度字段隐式初始化为0。将它们相乘得到0

我想你想要的是

length = y ;
width = x ;
int area = length * width ;
System.out.println("area ="+area);
你有这个:

public class Doconstructor {

    int length,width;

    Doconstructor (int x,int y)
    {
        int area;
        area = length *width;
        System.out.println("area ="+area);
    }
}
在任何情况下都不能将
长度
宽度
设置为任何值。它们的初始值为0,并且您的程序正按照您的要求执行<代码>面积=长度*宽度=0*0=0


您也没有对传递给构造函数的
x
y
执行任何操作,但这可能不是您的意图。在编写程序时,您基本上需要清楚地指示计算机做您想做的事情。它不会猜到你想要什么。如果忽略
x
y
,并且没有为
length
width
指定任何值,那么这正是将发生的情况,当您看到看到结果时,您不会感到惊讶。

您正在类级别编写
int length,width
,因此默认情况下长度和宽度设置为0。 在此之后,在构造器中,您不会将任何值设置为“长度”和“宽度”,因此您可以将“长度”和“宽度”的值设置为0。因此,面积也为0
请检查此链接,查看用于创建对象和设置属性的构造函数列表。您没有在构造函数中设置属性。下面是构造函数的外观

Doconstructor(int x, int y){
   length = x;
   width = y;
}
其次,您正在混合构造函数和方法的逻辑。你正在计算面积,这似乎非常适合你班上的另一种方法。因此,最好用另一种方法来移动该逻辑:

public int calculateArea() {
   int area;
   area = x * y;
   return area;
}
最后,使用构造函数创建一个对象来设置属性length和width。然后调用calculateArea方法进行计算区域的业务逻辑

public static void main(String args[]){
    Doconstructor d1 = new Doconstructor(10, 15); // create object and set length & width
     d1.calculateArea();
}

您没有将x和y的值指定给变量width和length。宽度和长度的默认值为(int)0。这就是为什么要得到输出(0*0=0)。首先为变量赋值或使用“area=x*y;”

但是

您需要按如下方式更改代码

Doconstructor (int x,int y)
{
    int area;
    this.length=x;
    this.width=y;
    area = length *width;
    System.out.println("area ="+area);
}
编辑如下:-

 package myConst;

    public class Doconstructor
    {
        int length,width;

        Doconstructor(int x, int y)
        {
            int area;
    this.length=x;//Using this for current object
    this.width=y;//Using this for current object
            area = length * width;
            System.out.println("area ="+area);
        }
    }

    class work
    {
        public static void main(String args[])
        {
            Doconstructor d1 = new Doconstructor(10, 15);
        }
    }
您的输出将是:

面积=150

必须阅读以下内容:


您不设置长度或宽度。长度和宽度将始终为0,因为它们从未初始化。在这种情况下,设置length=x和width=y,然后同时使用它们。一个好的做法是在你可以依赖任何人的帮助之前先遵守代码。自助是有史以来最好的帮助:)如果您没有为
length
width
指定任何值,为什么您会对它们为0感到惊讶?程序完全按照您的指示执行。:)顺便说一下,我不知道为什么这个问题会被否决。虽然这是一个基本问题,但它是一个有效的问题,有可编译的代码(实际上是其他人无意中编辑了一个右大括号--@sashkello,请注意编辑),并且对问题和结果有一个相当清楚(尽管很简短)的描述。@JasonC哇,对不起,我的编辑不好。虽然我没有投反对票,但反对票是可以理解的,因为这是调试过程,所以-只要在每次代码迭代后打印内容,问题就消失了。
Doconstructor (int x,int y)
{ 
    int area;            // you are never use x and y values for calculation
    area = length *width; // so area remain 0 since current length and width is 0
    System.out.println("area ="+area);
}
Doconstructor (int x,int y)
{
    int area;
    this.length=x;
    this.width=y;
    area = length *width;
    System.out.println("area ="+area);
}
 package myConst;

    public class Doconstructor
    {
        int length,width;

        Doconstructor(int x, int y)
        {
            int area;
    this.length=x;//Using this for current object
    this.width=y;//Using this for current object
            area = length * width;
            System.out.println("area ="+area);
        }
    }

    class work
    {
        public static void main(String args[])
        {
            Doconstructor d1 = new Doconstructor(10, 15);
        }
    }