English 中文(简体)
在你确定财产后,如何检验财产并允许其使用? [闭门]
原标题:Creating a Class, after you set a property, How do you test the property and enable it? [closed]

我知道这一点很简单,但我可以说出这一点。 我创造一等,我的职责是给我带来问题。 I m将O Reillys C# 3.0作为参考。

我设立了一个班子:

class Runner
{
    public double miles = 0;

    public double RunMiles
    {
        get { return miles; }
        set
        {
            if ((value > 2)&&(value <7))
            {
                this.miles = value;
            } 
        }
    }

    public void StartRun ()
    {      
        // here, I want to enable the runner to start running, Make him start running//if that makes any sense.
    }

    public void VerifyMiles ()
    {
        // enter code here//I want to verify the miles are set.
    }
}
最佳回答

对于OP的评论,有些是初步的。

Runner r = new Runner(); //you created an instance of type Runner in memory now
int i = r.RunMile; //i is now 0 since variable miles is 0 initially, so RunMiles is also 0

r.RunMiles = 34;
i = r.RunMile; //i is still 0 since variable miles is not affected  cos of your "if ((value > 2)&&(value <7))" in setter logic

r.RunMiles = 3;
i = r.RunMile; //i is 3 now

你们应该这样做:

r.StartRun();

并核实海里

r.VerifyMiles();


public void StartRun ()
{     
    miles++; //your logic here
}

public void VerifyMiles ()
{
    if (miles > 26.22)
       //print successfully completed one round of marathon
    else
       //he died in the meanwhile :(..
}

这正是你在欧佩组织中的基本表现。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签