[英]Java program gives error
I am a beginner in java, I have tried to solve the below program, but getting error, can anyone tell me where I am doing mistake? 我是Java的初学者,我曾尝试解决以下程序,但遇到错误,有人可以告诉我我在哪里做错吗?
public class TestGlass
{
public static void main(String [] args)
{
Glass milk = new Glass(15); // 15 ounces of milk
Glass juice = new Glass(3); // 3 ources of juice
milk.drink(2);
milk.drink(1);
milk.report();
juice.fill(6); // went from 3 to 9 ounces
juice.drink(1); // now down to 8 ounces
juice.report();
juice.spill();
juice.report();
}
}
class Glass
{
int ounce;
public void spill()
{
ounce = 0;
}
public void drink(int x){
ounce = ounce-x;
}
public void fill(int x){
ounce = ounce+x;
}
public int getOunce()
{
return ounce;
}
public void report()
{
int x = getOunce();
System.out.println("Glass has " + x + " ounces");
}
}
Here is the error, 这是错误,
TestGlass.java:5: error: constructor Glass in class Glass cannot be applied to given types;
Glass milk = new Glass(15); // 15 ounces of milk
^
required: no arguments
found: int
reason: actual and formal argument lists differ in length
TestGlass.java:6: error: constructor Glass in class Glass cannot be applied to given types;
Glass juice = new Glass(3); // 3 ources of juice
^
required: no arguments
found: int
reason: actual and formal argument lists differ in length
2 errors
You need to add a constructor to Glass
that accepts your ounce
parameter. 您需要向
Glass
添加一个接受ounce
参数的构造函数。
class Glass {
....
public Glass (int ounce) {
this.ounce = ounce;
}
....
}
The constructor is the method that is called when you use the new
operator. 构造函数是使用
new
运算符时调用的方法。 Its job is to initialize - to create - the object instance of the class. 它的工作是初始化-创建-类的对象实例。 A constructor with one or more arguments, like this one, is set up to receive values to initialize the instance variables of the class.
具有一个或多个参数的构造函数(如该参数)被设置为接收值以初始化类的实例变量。
Notice how the error message already mentions a constructor. 注意错误消息中已经提到了构造函数。 That is because if you do not specify your own constructor, Java adds a default constructor that receives no arguments.
这是因为,如果您不指定自己的构造函数,则Java将添加一个默认构造函数,该构造函数不接收任何参数。 That default no-arg constructor was what was being called when you called
new
. 默认的无参数构造函数是您调用
new
时所调用的构造函数。 Since you were passing arguments to a no-arg constructor, you got the error. 由于您将参数传递给无参数构造函数,因此出现了错误。 Once you add your own constructor, the default no-arg constructor goes away.
一旦添加了自己的构造函数,默认的no-arg构造函数就会消失。 If you want to have a no-arg version as well (eg setting
ounce
to 0
or to a default value), you can bring it back by specifying it along with the one I gave you - that is you can overload the constructor (see links below). 如果您也想使用无参数版本(例如,将
ounce
设置为0
或默认值),则可以通过将其与我给您的版本一起指定来将其带回来-也就是说,您可以重载构造函数(请参见下面的链接)。
class Glass {
....
public Glass () {
this.ounce = 1;
/* In this setup, a glass always has at least 1 ounce */
/* If you want it be 0, you could say this.ounce = 0, or */
/* just leave everything inside {} blank, since ounce will */
/* default to 0 anyway */
}
public Glass (int ounce) {
this.ounce = ounce;
}
....
}
Calling new Glass()
would call that first no-arg constructor. 调用
new Glass()
将调用第一个无参数构造函数。 Calling new Glass(15)
would call the second constructor, the one that takes an argument. 调用
new Glass(15)
将调用第二个构造函数,该构造函数接受一个参数。
Here's a nice tutorial on constructors. 这是一个关于构造函数的不错的教程 。
Here's a nice tutorial on overloading constructors. 这是有关重载构造函数的不错的教程 。
You need to write a constructor for Glass that takes an argument. 您需要为Glass编写一个带有参数的构造函数。
class Glass {
int ounce;
public Glass(int ounce) {
this.ounce = ounce;
}
public void spill()
{
ounce = 0;
}
public void drink(int x){
ounce = ounce-x;
}
public void fill(int x){
ounce = ounce+x;
}
public int getOunce()
{
return ounce;
}
public void report()
{
int x = getOunce();
System.out.println("Glass has " + x + " ounces");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.