Simple question on using constructers and methods in java and why the constructor isn't working

huangapple go评论53阅读模式
英文:

Simple question on using constructers and methods in java and why the constructor isn't working

问题

我刚刚开始学习面向对象编程(OOP),构造器和类方法。我的问题是,当调用我的构造器并传递值时,它不起作用。一个对象将被创建,但似乎创建它的值不起作用或"应用"。也许代码会更好地解释它。我的圆类是这样的:

public class MyCircle {
    double radius;
    
    public MyCircle() {
        
    }
    
    public MyCircle(double radius) {
        
    }

    final double pi = 3.14159;

    public double getRadius() {
        return radius;
    }
    
    public double getCircumference() {
        return radius * pi;
    }
    
    public double getArea() {
        return radius * radius * pi;
    }
}

在我的测试类中尝试使用它时,代码如下:

public class MyCircleTest {

    public static void main(String[] args) {
        double radius = 26.5;
        MyCircle myCoolCircle = new MyCircle(radius);
        System.out.println("Circumference of myCoolCircle: " + myCoolCircle.getCircumference());
        System.out.println("Area of myCoolCircle: " + myCoolCircle.getArea());
    }
}

输出结果如下:

Circumference of myCoolCircle: 0.0
Area of myCoolCircle: 0.0
好像它没有正确构建,我不知道为什么。任何帮助都将不胜感激。
英文:

I am just starting to learn OOP, constructers and class methods. My problem is that when my constructer is called and the value given it doesn't work. An object will be created but the value it is created with doesn't seem to work or 'apply'. Maybe the code will explain it better. My circle class is this:

public class MyCircle {
	double radius;
	
	public MyCircle() {
		
	}
	public MyCircle(double radius) {
		

    }
	
	final double pi = 3.14159;
	public double getRadius() {
		return radius;
	}
	
	public double getCircumference() {
		return radius*pi;
	}
	
	public double getArea() {
		return radius*radius*pi;
	}
	
}

It seems ok to me but when i try to use it from a testing class like this:

public class MyCircleTest {

public static void main(String[] args) {
	double radius = 26.5;
	MyCircle myCoolCircle = new MyCircle(radius);
	System.out.println("Circumference of myCoolCircle: " + myCoolCircle.getCircumference() );
	System.out.println("Area of myCoolCircle: " + myCoolCircle.getArea());

The output reads:
Circumference of myCoolCircle: 0.0
Area of myCoolCircle: 0.0
Its like it isn't constructing properly and i dont know why.
Any help is appreciated.

答案1

得分: 5

构造函数只是一个方法,实际上。这不是巫术; 方法有参数。参数除非您实际使用它们,否则不是特别有用。您的字段(命名为 radius)与构造函数的参数(同样命名为 radius)之间绝对没有任何联系。

如果您希望构造函数的操作是:“获取此参数的值,并将其分配给半径字段”,那么您需要编写如下代码:

public MyCircle(double radius) {
    this.radius = radius;
}

请注意,我们必须使用 this.radius 来引用字段,因为参数名称 radius '遮蔽' 了(使其无法访问)也命名为 radius 的字段。尽管如此,这是典型的 Java 用法。

英文:

A constructor is just a method, really. It's not voodoo magic; methods have parameters. Parameters aren't particularly useful unless you actually use them. There is absolutely no link whatsoever between your field (named radius) and the parameter of your constructor (also named radius).

If you want the action of the constructor to be: "Take the value of this parameter, and assign it to the radius field", then you'd have to write that:

public MyCircle(double radius) {
    this.radius = radius;
}

Note that we have to use this.radius to refer to the field, as the param name radius 'shadows' (makes it otherwise impossible to access) the field which is also named radius. Nevertheless, this is idiomatic java.

答案2

得分: 3

似乎你没有将传递的radius值分配给构造函数参数。Java不会自动将变量分配给构造函数参数。
尝试像这样分配它,使用this关键字。

public MyCircle(double radius) {
    this.radius = radius;
}
英文:

Seems like you are not assigning the passed value of radius to the constructor parameter. Java does not assign the variable to the constructor parameter itself.
Try assigning it like doing this keyword.

public MyCircle(double radius) {
    this.radius = radius;
}

答案3

得分: 0

使用这个。

public MyCircle(double radius) {
    this.radius = radius;
}
英文:

Use this.

public MyCircle(double radius) {
   this.radius = radius
}

huangapple
  • 本文由 发表于 2020年7月29日 20:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63153373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定