问题
So as an assignment for a class i'm trying to make a simple circle class and then use it as an constructor. Here is my class code:
public class Circle
{
private double radius;
private double pi;
public void setRadius(double rad)
{
radius = rad;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return pi * radius * radius;
}
public double getDiameter()
{
return radius * 2;
}
public double getCircumference()
{
return 2 * pi * radius;
}
}
Here's the program that uses the class to make a constructor:
import java.util.Scanner; //scanner class for input
public class CircleDemo
{
public static void main(String[]args)
{
double radiusIn; //gets radius from input
Scanner keyboard=new Scanner(System.in); //activates scanner class in program
System.out.print("Enter the radius of a circle: ");
radiusIn=keyboard.nextDouble();
Circle circularObject= new Circle(radiusIn);
System.out.println("The circle's area is" + circularObject.getArea());
System.out.println("The circle's diameter is" + circularObject.getDiameter());
System.out.println("The circle's circumference is" + circularObject.getCircumference());
}
}
and i get the error: Error: constructor Circle in class Circle cannot be applied to given types; required: no arguments found: double reason: actual and formal argument lists differ in length
I don't see anything wrong with my code, but then again i'm using the sample that my teacher gave us.
回答1:
You are calling the constructor here
Circle circularObject= new Circle(radiusIn);
But there is no constructor defined in your Circle class which matches the argument you are passing in. Return to your Circle class and define the constructor which takes a double argument:
public Circle(double val)
{
//Implementation
}
Note that if you do not provide an explicit constructor for a class, Java will automatically provide you with an empty-implementation, no argument, public constructor for you to use. Once you define your first explicit constructor, that default constructor is gone, meaning you would have to re-define it manually.
回答2:
You don't have a constructor in your circle class
public Circle (something here) {}
回答3:
Java provides a default constructor for every class you write. You cannot see it, but it is there and it will be called unless you write your own with any desired parameters.
回答4:
One of the most used implementations of a parametered constructor is like this:
double radius;
public Circle(double radius){
this.radius = radius;
}
you do not need to have the same name:
double radius;
public Circle(double i){
radius = i;
}
but the first version is considered best practice and should be the one used. This is of course valid for any form of paramatered constructors:
String name;
int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
回答5:
Please add the constructor for Circle. The modified code will be:
public class Circle
{
private double radius;
private double pi;
Circle(double x)
{
this.radius = x;
}
public void setRadius(double rad)
{
radius = rad;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return pi * radius * radius;
}
public double getDiameter()
{
return radius * 2;
}
public double getCircumference()
{
return 2 * pi * radius;
}
}
来源:https://stackoverflow.com/questions/28266913/beginner-java-required-no-arguments-error-on-constructor