[英]Circle class multiple constructors
I am trying modify the class Circle to include a third constructor for constructing a Circle instance with two arguments - a double for radius and a String for color. 我正在尝试将Circle类修改为包括第三个构造函数,该构造函数用于构造带有两个参数的Circle实例-用于半径的double和用于颜色的String。 Also Modify the main class to construct an instance of Circle using this constructor. 还要修改主类以使用此构造函数构造Circle的实例。 I am having trouble with this, i keep getting the message that constructor Circle is never used. 我对此感到麻烦,我不断收到消息,从未使用过构造函数Circle。 Please have a look at the code. 请看一下代码。
public class Circle {
private double radius;
private String color;
public Circle() {
radius = 1.0;
color = "red";
}
public Circle(double r) {
radius = r;
color = "Blue";
}
public Circle(double r, String c) {
radius = r;
color =c;
}
public double getRadius() {
return radius;
}
public void setRadius(double newRadius) {
radius = newRadius;
}
public String getColor()
{
return color;
}
public void setColor(String newColor) {
color=newColor;
}
public double getArea() {
return radius*radius*Math.PI;
}
}
public class Main {
public static void main(String[] args) {
Circle c1 = new Circle();
System.out.println("The circle has radius of " + c1.getRadius());
System.out.println("and area of " + c1.getArea());
Circle c2 = new Circle(2.5);
System.out.println("The circle has radius of " + c2.getRadius());
System.out.println("and area of " + c2.getArea());
Circle c3 = new Circle(0.5);
c3.setColor("Green");
System.out.println("The circle has radius of "
+ c3.getRadius());
System.out.println("and area of " + c3.getArea());
System.out.println("color is: " + c3.getColor());
Circle c5 = new Circle();
c5.setRadius(500.0);
System.out.println("radius is: " + c5.getRadius());
c5.setColor("Yellow");
System.out.println("color is: " + c5.getColor());
}
}
Well, you aren't using it, so the message should hardly be a surprise. 好吧,您没有使用它,因此该消息不足为奇。 Just stick in a call to the two-argument constructor somewhere (eg, Circle c3 = new Circle(0.5, "Green");
), and the message should go away. 只需在某处(例如, Circle c3 = new Circle(0.5, "Green");
)中调用两个参数的构造函数,该消息就会消失。 Of course, if you change all the instance creations to the two-argument constructor, you'll then get the warning for the zero- and one-argument versions. 当然,如果将所有实例创建都更改为二参数构造函数,则将收到零参数和一参数版本的警告。
Alternatively, you can change your constructor definitions: 另外,您可以更改构造函数定义:
public class Circle {
public Circle() {
this(1.0, "red");
}
public Circle(double r) {
this(r, "Blue");
}
public Circle(double r, String c) {
radius = r;
color = c;
}
...
}
I have to say, though, that it's weird to have the default value for the color be "red" when you use a default radius and "Blue" when you specify a radius. 不过,我不得不说,使用默认半径时将颜色的默认值设置为“红色”,而指定半径时将颜色的默认值设置为“蓝色”,这很奇怪。 I would recommend against that, in which case I'd change the first constructor to: 我建议不要这样做,在这种情况下,我会将第一个构造函数更改为:
public Circle() {
this(1.0);
}
You might also want to look at using an enum
for your colors, to avoid problems with case differences (like "red" vs. "Red"). 您可能还希望使用颜色enum
,以避免出现大小写差异(例如“红色”与“红色”)的问题。 You can always convert between an enum
value and a String
using the built-in enum
methods name()
and valueOf(String)
. 您始终可以使用内置的enum
方法name()
和valueOf(String)
在enum
值和String
之间进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.