1. Trang chủ
  2. » Công Nghệ Thông Tin

Holding a Class Responsible

30 190 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 735,81 KB

Nội dung

Chapter 11 Holding a Class Responsible In This Chapter  Letting the class protect itself through access control  Allowing an object to initialize itself via the constructor  Defining multiple constructors for the same class  Constructing static or class members A class must be held responsible for its actions Just as a microwave oven shouldn’t burst into flames if you press the wrong key, a class shouldn’t allow itself to roll over and die when presented with incorrect data To be held responsible for its actions, a class must ensure that its initial state is correct, and control its subsequent state so that it remains valid C# provides both of these capabilities Restricting Access to Class Members Simple classes define all their members as public Consider a BankAccount program that maintains a balance data member to retain the balance in each account Making that data member public puts everyone on the honor system I don’t know about your bank, but my bank is not nearly so forthcoming as to leave a pile of money and a register for me to mark down every time I add money to or take money away from the pile After all, I may forget to mark my withdrawals in the register I’m not as young as I used to be — my memory is beginning to fade Controlling access avoids little mistakes like forgetting to mark a withdrawal here or there It also manages to avoid some really big mistakes with withdrawals 222 Part IV: Object-Oriented Programming I know exactly what you functional types out there are thinking: “Just make a rule that other classes can’t access the balance data member directly, and that’s that.” That approach may work in theory, but in practice it never does People start out with good intentions (like my intentions to work out every day), but those good intentions get crushed under the weight of schedule pressures to get the product out the door Speaking of weight A public example of public BankAccount The following example BankAccount class declares all its methods public but declares its data members, including nAccountNumber and dBalance, to be private Note that I’ve left it in an incorrect state to make a point The following code won’t compile correctly yet // BankAccount - create a bank account using a double variable // to store the account balance (keep the balance // in a private variable to hide its implementation // from the outside world) // Note: Until you correct it, this program fails to compile // because Main() refers to a private member of class BankAccount using System; namespace BankAccount { public class Program { public static void Main(string[] args) { Console.WriteLine(“This program doesn’t compile in its present state.”); // open a bank account Console.WriteLine(“Create a bank account object”); BankAccount ba = new BankAccount(); ba.InitBankAccount(); // accessing the balance via the Deposit() method is OK // Deposit() has access to all the data members ba.Deposit(10); // accessing the data member directly is a compile time error Console.WriteLine(“Just in case you get this far” + “\nThe following is supposed to “ + “generate a compile error”); ba.dBalance += 10; // wait for user to acknowledge the results Console.WriteLine(“Press Enter to terminate ”); Console.Read(); } } // BankAccount - define a class that represents a simple account public class BankAccount { private static int nNextAccountNumber = 1000; private int nAccountNumber; Chapter 11: Holding a Class Responsible // maintain the balance as a double variable private double dBalance; // Init - initialize a bank account with the next // account id and a balance of public void InitBankAccount() { nAccountNumber = ++nNextAccountNumber; dBalance = 0.0; } // GetBalance - return the current balance public double GetBalance() { return dBalance; } // AccountNumber public int GetAccountNumber() { return nAccountNumber; } public void SetAccountNumber(int nAccountNumber) { this.nAccountNumber = nAccountNumber; } // Deposit - any positive deposit is allowed public void Deposit(double dAmount) { if (dAmount > 0.0) { dBalance += dAmount; } } // Withdraw - you can withdraw any amount up to the // balance; return the amount withdrawn public double Withdraw(double dWithdrawal) { if (dBalance 0.0) { // round off the double to the nearest cent before depositing decimal mTemp = (decimal)dAmount; mTemp = Decimal.Round(mTemp, 2); mBalance += mTemp; } } // Withdraw - you can withdraw any amount up to the // balance; return the amount withdrawn public decimal Withdraw(decimal dWithdrawal) { if (mBalance

Ngày đăng: 04/10/2013, 21:20

w