C#如何测试这段代码? BankAccount类

时间:2016-04-06 05:31:04

标签: c# class oop inheritance polymorphism

我是C#的新手,我想知道如何测试这个示例代码。换句话说,为了测试Account.cs(基类)和SavingsAccount.cs(派生类)中的代码,我应该在main(Program.cs)文件中放置什么?我想传递数字并让程序输出ToString,如果有意义的话,它会出现在Account.cs和SavingsAccount.cs文件中。

Account.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCode
{
public class Account
{

        protected double interestRate;
        protected string owner;
        protected decimal balance;

        public Account(string o, decimal b, double ir)
        {
            this.interestRate = ir;
            this.owner = o;
            this.balance = b;
        }

        public Account(string o, double ir) :
          this(o, 0.0M, ir)
        {
        }

        public virtual decimal Balance
        {
            get { return balance; }
        }

        public virtual void Withdraw(decimal amount)
        {
            balance -= amount;
        }

        public virtual void Deposit(decimal amount)
        {
            balance += amount;
        }

        public virtual void AddInterests()
        {
            balance += balance * (Decimal)interestRate;
        }

        public override string ToString()
        {
            return owner + "'s account holds " +
                  +balance + " kroner";
        }
    }
}

SavingsAccount.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCode
{
 public class SavingsAccount : Account
    {

        public SavingsAccount(string o, double ir) :
          base(o, 0.0M, ir)
        {
        }

        public SavingsAccount(string o, decimal b, double ir) :
          base(o, b, ir)
        {
        }

        public override void Withdraw(decimal amount)
        {
            if (amount < balance)
                balance -= amount;
            else
                throw new Exception("Cannot withdraw");
        }

        public override void AddInterests()
        {
            balance = balance + balance * (decimal)interestRate
                              - 100.0M;
        }

        public override string ToString()
        {
            return owner + "'s savings account holds " +
                  +balance + " kroner";
        }
    }
}

Program.cs文件(我在这里写什么?):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCode
{
class Program
{
    static void Main(string[] args)
    {
     SavingsAccount savingsaccount = new SavingsAccount("John", 0.0M, 0.70);
     savingsaccount.ToString();
    }
}
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

namespace TestCode
{
class Program
{
    static void Main(string[] args)
    {
        SavingsAccount account = new SavingsAccount(put,init,values,here);
        account.CallAMethodLikeThis();
    }
}
}

显然,将put,init,values,hereCallAMethodLikeThis替换为实际值和方法名称。

修改

现在您已经在此代码中存根了,请替换为:

 SavingsAccount savingsaccount = new SavingsAccount("John", 0.0M, 0.70);
 Savingsaccount.ToString();

用这个:

 SavingsAccount savingsaccount = new SavingsAccount("John", 0.0M, 0.70);
 savingsaccount.ToString();

原因是SavingsAccount是您的类的名称(&#34; recipie&#34;关于如何制作新对象),而savingsaccount是您在调用时创建的实例new ...savingsaccount是您从现在开始使用的对象,用于与此数据副本进行交互。

答案 1 :(得分:0)

由于此代码中没有任何地方是&#34;按任意键继续&#34;消息,当您按F5运行时,您可能正在运行另一个项目。右键单击该项目,选择&#34; Run as startup project&#34;并尝试再次推动F5。如果这不起作用,请右键单击项目,选择属性,并确保启动类为TestCode.Program

答案 2 :(得分:0)

在您的代码中,您不使用任何内容来输出字符串。在Main方法中添加Console.WriteLine(savingsaccount.ToString());,不仅可以创建字符串,还可以将其写入某处。

然后覆盖ToString方法也应该像它们应该使用一样。