半径变量未从构造函数传递
Posted
技术标签:
【中文标题】半径变量未从构造函数传递【英文标题】:Radius Variable Isn't Being Passed From Constructor 【发布时间】:2021-06-04 21:07:36 【问题描述】:我应该使用构造函数来接受圆的半径作为参数,然后使用各种访问器和修改器方法来显示面积、周长和直径。当我输入半径值时,比如 4,它表示半径和所有其他值(面积、周长、直径)都是 0.0。我是 java 新手,想知道我缺少什么。
// import scanner to read keyboard input
import java.util.Scanner;
class CircleCalculator
private double radius;
public CircleCalculator(double r)
radius = r;
public static void main(String[] args)
// declare the variable keyboard that can reference an object of the scanner class
// create a new Scanner object in the memory that reads from the input System.in
Scanner keyboard = new Scanner(System.in);
System.out.println("\nWelcome to Cam's Magic Circle Calculator!");
System.out.println("\nEnter the radius of your circle");
double r, a, d, c;
CircleData circledata = new CircleData();
System.out.println("\nThe measurements of a circle with the radius " + circledata.getRadius() + " are:\n" +
"\tArea: " + circledata.getArea() +" units squared\n" +
"\tDiameter: " + circledata.getDiameter() +" units\n" +
"\tCircumference: " + circledata.getCircumference() +" units\n");
public static class CircleData //create class for mutator and accessor methods
Scanner keyboard = new Scanner(System.in);
double r = keyboard.nextDouble();
CircleCalculator setRadius = new CircleCalculator(r);
final double pi = 3.1415;
private double radius, area, diameter, circumference;
private double getRadius()
return radius;
private double getArea()
area = pi * radius * radius;
return area;
private double getDiameter()
diameter = 2 * radius;
return diameter;
private double getCircumference()
circumference = 2 * pi * radius;
return circumference;
给予:
Welcome to Cam's Magic Circle Calculator!
Enter the radius of your circle
4
The measurements of a circle with the radius 0.0 are:
Area: 0.0 units squared
Diameter: 0.0 units
Circumference: 0.0 units
【问题讨论】:
在我看来,您将CircleCalculator.radius
与 CircleData.radius
混淆了。
【参考方案1】:
由于类的嵌套,您当前的代码很混乱。我建议遵循良好的代码实践,将它们分离到自己的类文件中。让CircleData
处理实际的圆数据和计算,让CircleCalculator
成为你的整体程序序列逻辑。
圆形计算器
import java.util.Scanner;
public class CircleCalculator
public static void main(String[] args)
System.out.println("\nWelcome to Cam's Magic Circle Calculator!");
System.out.println("\nEnter the radius of your circle");
Scanner keyboard = new Scanner(System.in);
CircleData circledata = new CircleData(keyboard.nextDouble());
keyboard.close();
System.out.println("\nThe measurements of a circle with the radius " + circledata.getRadius() + " are:\n" +
"\tArea: " + circledata.getArea() +" units squared\n" +
"\tDiameter: " + circledata.getDiameter() +" units\n" +
"\tCircumference: " + circledata.getCircumference() +" units\n");
CircleData(在单独的文件中,因此方法现在是公开的)
public class CircleData
final double pi = 3.1415;
private double radius, area, diameter, circumference;
public CircleData(double radius)
this.radius = radius;
public double getRadius()
return radius;
public double getArea()
area = pi * radius * radius;
return area;
public double getDiameter()
diameter = 2 * radius;
return diameter;
public double getCircumference()
circumference = 2 * pi * radius;
return circumference;
【讨论】:
【参考方案2】:-
因为来自
CircleCalculator
和CircleData
类的半径字段是两个不同的字段。因此,您不能使用CircleData
中的CircleCalculator
的半径,因为CircleData
已经包含此类字段。
CircleData
实际上不应该有 Scanner
字段,它破坏了 SRP https://en.wikipedia.org/wiki/Single-responsibility_principle。
我建议这样写:
class CircleCalculator
public static void main(String[] args)
// declare the variable keyboard that can reference an object of the scanner class
// create a new Scanner object in the memory that reads from the input System.in
Scanner keyboard = new Scanner(System.in);
System.out.println("\nWelcome to Cam's Magic Circle Calculator!");
System.out.println("\nEnter the radius of your circle");
CircleData circledata = new CircleData(keyboard.nextDouble());
System.out.println("\nThe measurements of a circle with the radius " + circledata.getRadius() + " are:\n" +
"\tArea: " + circledata.getArea() + " units squared\n" +
"\tDiameter: " + circledata.getDiameter() + " units\n" +
"\tCircumference: " + circledata.getCircumference() + " units\n");
public static class CircleData //create class for mutator and accessor methods
final double pi = 3.1415;
private final double radius;
public CircleData(double radius)
this.radius = radius;
private double getRadius()
return radius;
private double getArea()
return pi * radius * radius;
private double getDiameter()
return 2 * radius;
private double getCircumference()
return 2 * pi * radius;
【讨论】:
【参考方案3】:没有从构造函数传递半径变量,因为您已经在CircleCalculator
类中定义了构造函数,但是使用半径变量值计算面积、周长等的方法在不同的类中。
CircleCalculator setRadius = new CircleCalculator(r);
在这里,虽然setRadius
有用户输入的值,但它没有被使用。
CircleData circledata = new CircleData();
在这里, circledata 没有引用任何内容。方法调用
circledata.getRadius() , circledata.getArea() , circledata.getDiameter() , circledata.getCircumference()
实际上返回 double 的默认值 0.0,因为 radius
变量的默认值是 0.0
此外,类文件中应该只有一个公共类,其名称应与类文件名匹配,并且该公共类应具有public static void main(String[] args)
方法。
【讨论】:
以上是关于半径变量未从构造函数传递的主要内容,如果未能解决你的问题,请参考以下文章
使用 python socketserver 如何将变量传递给处理程序类的构造函数
当映射变量传递给不同实例的构造函数时,所有实例成员变量都会更新为映射的最新值[重复]