为什么我会得到我的圈子值零? [英] Why am I getting a value of zero for my circle?

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

问题描述

下面是我的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?

推荐答案

由于你的避风港的价值T曾经呼吁区二传手。也许你想这样的事情,而不是

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆