【问题标题】:C# How to test this code? BankAccount Class [closed]C#如何测试这段代码? BankAccount 类[关闭]
【发布时间】:2016-04-06 05:31:03
【问题描述】:

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

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();
    }
}
}

感谢您的帮助!

【问题讨论】:

  • 我建议您在构造函数中正确命名变量。这是一个很好的做法。如果您打算在一年内查看代码,那么如果项目变得很大,那将没有任何意义。
  • 也许你应该在你的 Main 方法中添加一个Console.WriteLine(savingsaccount.ToString());。您所做的只是创建字符串,但什么也不做。
  • @Booser 谢谢! Console.WriteLine 工作正常。

标签: c# class oop inheritance polymorphism


【解决方案1】:
namespace TestCode
{
class Program
{
    static void Main(string[] args)
    {
        SavingsAccount account = new SavingsAccount(put,init,values,here);
        account.CallAMethodLikeThis();
    }
}
}

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

编辑

既然您已经在这段代码中存根,请替换它:

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

用这个:

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

原因是SavingsAccount 是您的类的名称(如何创建新对象的“recipie”),而savingsaccount 是您在调用new ... 时创建的实例。 savingsaccount 是您从现在开始用来与此数据副本交互的对象。

【讨论】:

  • 感谢您的回复,但是,当我尝试它仍然不会输出任何内容时。如果你能看的话,我用我写的东西编辑了我的帖子。
  • 我的错误,你提到我应该做的是我已经拥有的。我只是在这里编辑我的代码时不小心把它大写了。由于某种原因它没有输出任何东西,只是说当我 ctrl + F5 运行代码时按任意键继续。
  • ToString() 是(或多或少)在 .NET 中具有特殊含义的保留方法名称。最好将此方法重命名为其他名称 - 可能是 ShowBalance 或 Describe,看看是否能提供更好的输出。
  • @robrich 这就是他使用覆盖来重新定义它的原因。应该没问题。
【解决方案2】:

由于此代码中没有“按任意键继续”消息,因此当您按 F5 运行时,您可能正在运行不同的项目。右键单击此项目,选择“作为启动项目运行”并再次尝试按 F5。如果这不起作用,请右键单击项目,选择属性,并确保 Startup 类为 TestCode.Program

【讨论】:

  • 我试过了,将启动对象设置为TestCode.Program。我什至添加了一个 Console.ReadLine();暂停屏幕,这样我就不必按 ctrl + F5。仍然没有输出。当我尝试将名称 ToString 更改为 Describe 之类的名称时,Account.cs 文件中出现错误,提示“找不到合适的方法来覆盖”。但是,在 SavingsAccount.cs 文件中,当我将 ToString 更改为 Describe 时,它​​不会给我任何错误。
  • 将 ToString 方法重命名为 Describe 时,必须删除 override 关键字。因为有一个 ToString 方法可以覆盖,在那里应该很好。但是基类中没有Describe方法。
  • @Booser 好的,所以我尝试删除 Account.cs 基类中的 override 关键字,但仍然没有输出。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多