本文介绍了为什么我的圈子的值为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 Circle 课程代码.

Here is my Circle class code.

class Circle
{
    private double radius;
    private double area;

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

    public double Area
    {
        set { area = Math.PI * Math.Pow(radius, 2); }
        get { return area; }

    }
}

这是测试代码.

    Circle circle1 = new Circle(3);

    MessageBox.Show("Circle 1 Area: " + circle1.Area);

因此,出于某种原因,当我使用 MessageBox.Show() 时,它似乎给我的值为零.我给圆的值 3,所以我的构造函数不应该设置半径的值吗?

So for some reason, when I use the MessageBox.Show(), it seems to give me values of zero instead. I gave the circle a value of 3 so shouldn't my constructor set the value of the radius that?

推荐答案

因为你从来没有在 Area 上调用过 setter.也许你想要这样的东西?

Because you haven't ever called the setter on Area. Perhaps you want something like this instead?

class Circle
{
    private double radius;

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

    public double Area
    {
        get { return Math.PI * Math.Pow(radius, 2); }
    }
}

这将在每次请求时计算面积.

This will compute the Area every time it is requested.

这篇关于为什么我的圈子的值为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:13