我定义了继承自 Account 类的 Saving 和 CreditCard 类,对于我要定义的最后一个类(Client 类),我需要一个列表,将所有客户的银行账户存储为 Saving 或 CreditCard 对象(并且被初始化为空列表)。我在制作这个存储不同类对象的“帐户”列表时遇到了麻烦。尝试像 var accounts:[AnyObject] 这样的东西是行不通的……
//Custom Exceptions:
enum MyError: Error {
case transactionLimitExceeded
case overdraftError
case creditLimitExceeded
case deleteError
}
//Account Class:
class Account {
var account_number:String
var transaction_limit:Int
var balance:Int
//Initializer
init(_ accNum:String, _ transLim:Int ) {
self.account_number = accNum
self.transaction_limit = transLim
self.balance = 0
}
//Functions
func getAccountNumber() -> String {
return account_number
}
func getBalance() -> Int {
return balance
}
}
//Saving Class
class Saving : Account {
func deposit(_ num:Int) {
self.balance = self.getBalance() + num
}
func withdraw(_ num:Int) throws {
//raise TransactionLimitExceeded if num is more than transaction_limit
//raise OverdraftError if balance is less than num
//otherwise, deduct num from balance
if num > self.transaction_limit {
throw MyError.transactionLimitExceeded
}
else if self.getBalance() < num {
throw MyError.overdraftError
}
else {
self.balance = self.getBalance() - num
}
}
}
//CreditCard Class
class CreditCard : Account {
var credit_limit:Int
init (_ accNum:String, _ transLim:Int, _ credLim:Int) {
self.credit_limit = credLim
super.init(accNum, transLim)
}
func getCreditLimit() -> Int {
return self.credit_limit
}
func getRemainingAvailableCredit() -> Int {
//Return credit - balance
let balance = self.getBalance()
let credit = self.getCreditLimit()
return credit - balance
}
func deposit(_ num:Int) {
//set balance to balance minus num
let balance = self.getBalance()
self.balance = balance - num
}
func withdraw(_ num:Int) throws {
//Raise transactionLimitExceeded if num is more than transaction limit
//raise creditLimitExceeded if remaining credit available is less than num (you might want to call the method getRemainingAvailableCredit)
//Otherwise, add num to balance
if num > self.transaction_limit {
throw MyError.transactionLimitExceeded
}
else if self.getRemainingAvailableCredit() < num {
throw MyError.creditLimitExceeded
}
else {
self.balance = self.getBalance() + num
}
}
}
//Client Class
class Client {
var name:String
var accounts:[Saving,CreditCard]() //<- ???
init(_ name:String) {
self.name = name
self.accounts = []
}
func createSavingAccount(_ accNum:String, _ transLim:Int) {
//Creates a Saving Account and adds it to the accounts list
let s = Saving(accNum, transLim)
accounts += [s]
}
func createCreditCardAccount(_ accNum:String, _ transLim:Int, _ credLim:Int) {
//Creates a Credit Card Account and adds it to the accounts list
let c = CreditCard(accNum, transLim, credLim)
accounts += [c]
}
func transferBetweenAccounts(_ account1:Account, _ account2:Account, _ num:Int) {
//Withdraw num from account1 and deposit it to account2
//Do this by calling the method withdraw from account1 and deposit from account2
var account1Index = 0
var account2Index = 0
for i in 0...accounts.count-1 {
let account = accounts[i]
if account.getAccountNumber() == account1.getAccountNumber() {
account1Index = i
}
else if account.getAccountNumber() == account2.getAccountNumber() {
account2Index = i
}
}
self.accounts[account1Index].withdraw(num)
self.accounts[account2Index].deposit(num)
}
}
最佳答案
制作父类(super class)的数组。
var accounts = [Account]()
关于arrays - Swift中存放不同类对象的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573752/