I'm writing a code in which in its main method I create an object. But I don't know how to call its constructor. here is my code:
我正在编写一个代码,在其主要方法中我创建了一个对象。但我不知道如何调用它的构造函数。这是我的代码:
//This class calculate the greatest common divisor of two number
import static java.lang.Math.min;
public class Calculate2
{
private int num1,num2;
public Calculate2(int num1 , int num2)
{
this.num1=num1;
this.num2=num2;
}
public static void main(String[] args)
{
Calculate2 gcd = new Calculate2();
gcd.GCD();
}
public void GCD()
{
int min_num;
int greatestcommondivisor=0;
min_num = min(num1,num2);
for(int commondivisor=1;commondivisor <= min_num;commondivisor++)
{
if(num1%commondivisor==0 && num2%commondivisor==0)
{
greatestcommondivisor=commondivisor;
}
commondivisor++;
}
System.out.println(greatestcommondivisor);
}//End of the method gcd
}//End of the class calculate2
how should I call the constructor?
我该如何调用构造函数?
4 个解决方案
#1
2
- Fact that you created your own constructor means that default one will not be added by compiler.
-
Also your constructor looks like
你的构造函数也是如此
public Calculate2(int num1, int num2) { }
which means it requires two
int
arguments, so you need to pass someint
values to it like这意味着它需要两个int参数,因此您需要将一些int值传递给它
new Calculate2(42,24);
您创建自己的构造函数的事实意味着编译器不会添加默认构造函数。
#2
2
Constructors in a class are called right when we declare the object. Thus declare your object like this
当我们声明对象时,类中的构造函数被调用。这样声明你的对象
Calculate2 obj = new Calculate2(1,2);
#3
0
Your Constructor is parameteris constructor
您的构造函数是参数构造函数
You must pass value to parameteris Construcor
You Can Call With differnt of type
int i=10;
int j=5;
Calculate2 gcd = new Calculate2(i,j);
or
new Calculate2(10,20);
or
Calculate2 gcd = new Calculate2(10,20);
or
Calculate2 gcd;
gcd = new Calculate2(15,20);
#4
0
A constructor shares the same name as the class, so when u create a new object (gcd
) like below
构造函数与类共享相同的名称,因此当您创建一个新对象(gcd)时,如下所示
Calculate2 gcd = new Calculate2();
You have already called the default constructor of the class Calculate2
您已经调用了Calculate2类的默认构造函数
But since, you have declared a constructor with parameters:
但是,因为,您已经声明了一个带参数的构造函数:
public Calculate2(int num1 , int num2)
This means that the constructor of the class requires 2 parameters in it num1
and num2
, so you have to call it like
这意味着该类的构造函数需要num1和num2中的2个参数,因此您必须将其称为
Calculate2 obj_gcd = new Calculate2(100,200);
#1
2
- Fact that you created your own constructor means that default one will not be added by compiler.
-
Also your constructor looks like
你的构造函数也是如此
public Calculate2(int num1, int num2) { }
which means it requires two
int
arguments, so you need to pass someint
values to it like这意味着它需要两个int参数,因此您需要将一些int值传递给它
new Calculate2(42,24);
您创建自己的构造函数的事实意味着编译器不会添加默认构造函数。
#2
2
Constructors in a class are called right when we declare the object. Thus declare your object like this
当我们声明对象时,类中的构造函数被调用。这样声明你的对象
Calculate2 obj = new Calculate2(1,2);
#3
0
Your Constructor is parameteris constructor
您的构造函数是参数构造函数
You must pass value to parameteris Construcor
You Can Call With differnt of type
int i=10;
int j=5;
Calculate2 gcd = new Calculate2(i,j);
or
new Calculate2(10,20);
or
Calculate2 gcd = new Calculate2(10,20);
or
Calculate2 gcd;
gcd = new Calculate2(15,20);
#4
0
A constructor shares the same name as the class, so when u create a new object (gcd
) like below
构造函数与类共享相同的名称,因此当您创建一个新对象(gcd)时,如下所示
Calculate2 gcd = new Calculate2();
You have already called the default constructor of the class Calculate2
您已经调用了Calculate2类的默认构造函数
But since, you have declared a constructor with parameters:
但是,因为,您已经声明了一个带参数的构造函数:
public Calculate2(int num1 , int num2)
This means that the constructor of the class requires 2 parameters in it num1
and num2
, so you have to call it like
这意味着该类的构造函数需要num1和num2中的2个参数,因此您必须将其称为
Calculate2 obj_gcd = new Calculate2(100,200);