第2天:移动方法

我觉得这个其实没什么就是说把某些方法移动到常用的类里面去

这个应该在我们写程序的时候潜意识里面就会做到的

例子如下

错误的代码:

 

Code

 

正确的代码应该是:

public class BankAccount
  {
      public BankAccount(int accountAge, int creditScore,AccountInterest accountInterest)
      {
          AccountAge = accountAge;
          CreditScore = creditScore;
          AccountInterest = accountInterest;
      }
      public int AccountAge { get; private set; }
      public int CreditScore { get; private set; }
      public AccountInterest AccountInterest { get; private set; }
  }

public class AccountInterest
   {
       public BankAccount Account { get; private set; }
       public AccountInterest(BankAccount account)
       {
           Account = account;
       }
       public double InterestRate
       {
           get { return CalculateInterestRate(); }
       }
       public bool IntroductoryRate
       {
           get { return CalculateInterestRate() < 0.05; }
       }

       public double CalculateInterestRate()
       {
           if (Account.CreditScore > 800)
               return 0.02;
           if (Account.AccountAge > 10)
               return 0.03;
           return 0.05;
       }
   }

 

不同之处是:CalculateInterestRate()方法放在了AccountInterest类里面

posted @ 2010-01-03 20:28  双击  阅读(173)  评论(0编辑  收藏  举报