【问题标题】:Java Exercise....need help without using getters and settersJava 练习....在不使用 getter 和 setter 的情况下需要帮助
【发布时间】:2014-05-06 16:30:32
【问题描述】:

我有:

public class Circle {

//private instance variable
private double radius = 1;//Declaring "1" as the default value
private String color = "red";//Declaring "red" as the default color and as a string.
// default constructor w/out an argument
public Circle() {
}

public Circle(double r){ //constructor that uses double argument which is assigned to radius
    radius = r;
    color = "red";
}
//public method "getRadius"
public double getRadius() {
    return radius;
}
//public method "getArea", used to get the area of the circle.
public double getArea() {
    //method returns the Area of a circle using the below formula
    return Math.PI * radius * radius;
}
}

public class TestCircle {
// Testing function
public static void main(String[] args) {
    Circle c1 = new Circle(); // initialize with default constructor
    Circle c2 = new Circle(5); // initialize with constructor that takes radius as argument

    //prints the results of the program.
    System.out.println("*****************************************");
    System.out.println("Details of circle 1: ");
    System.out.println("Radius: " + c1.getRadius());
    System.out.println("Area: " + c1.getArea());
    System.out.println("Color: " + color);
    System.out.println("*****************************************");
    System.out.println("Details of circle 2: ");
    System.out.println("Radius: " + c2.getRadius());
    System.out.println("Area: " + c2.getArea());
    System.out.println("Color: " + c2.getColor());
    System.out.println("*****************************************");

我也试图让“红色”圆圈的颜色也打印出来。现在最重要的是我的代码中有以下内容,她说他们是另一种方式。

//Constructor that uses a string argument which is assigned to color
public Circle(String c) {
    color = C;
}
//public method "getColor", used to get the color of the circle.
public String getColor() {
    return color;
}
}

仅供参考....我问她是否应该这样做 System.out.println("红色"); 她说不。

【问题讨论】:

  • 如果你没有设置器也没有默认值,那么使用无参数构造函数有什么用?
  • 如果你想打破一切,请使用反射。
  • @n1234 哦不,反射不! :P
  • @LuiggiMendoza 哦,是的! :P

标签: java setter getter


【解决方案1】:

您需要在Circle 类中为您的String color 属性添加一个getter,然后像使用radiusarea 一样使用它。

除此之外,我建议您为 Circle 类中的字段创建设置器,以便更改每个实例的属性值。

(因为这是作业,所以不会给出代码)。


在奇怪的情况下,您根本不想使用任何 getter/setter(这在现实世界的应用程序中确实很奇怪),您可以更改属性的修饰符以允许直接从其他类访问它们。这是 Java 修饰符访问级别:

Modifier    Class Package Subclass   World
public      Y     Y       Y          Y
protected   Y     Y       Y          N
no modifier Y     Y       N          N
private     Y     N       N          N

因此,您可以将String colorprivate 更改为public,并且任何类都可以访问该属性并使用它或更改其值而没有问题。请注意,这样做会破坏您班级的encapsulation

更多信息:

【讨论】:

  • 这不是家庭作业。我不在学校。我已经获得了计算机科学学士学位。
  • “她”指的是一个朋友。我们都在互相挑战,以使自己在编程方面做得更好。我们想成为程序员,有基础
  • @LuiggiMendoza 你显然不知道答案,这就是为什么你会说这样的话。这只是我被困在哪里的一个问题。我敢肯定,当它弄清楚时,这将是一个“哦”的时刻。
  • 字符串颜色是私有的,Circle() 中的变量不是私有的(你应该真正指定它而不是默认值)。
  • 而且,顺便说一句,它应该真的是... private static final String COLOR = "red"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
相关资源
最近更新 更多