aaa.swift
· 544 B · Swift
Raw
class BankAccount {
var balance: Int
// Initializer to set the initial state of the object
init(balance: Int) {
self.balance = balance
self.cpuCard = "gg"
}
// Method to modify the balance
func deposit(_ amount: Int) {
balance += amount
}
}
// Create an instance (object) of the BankAccount class
let account = BankAccount(balance: 100)
// Call a method to interact with the object
account.deposit(25)
// Access a property using dot syntax
print(account.balance) // Prints "125"
| 1 | class BankAccount { |
| 2 | var balance: Int |
| 3 | |
| 4 | // Initializer to set the initial state of the object |
| 5 | init(balance: Int) { |
| 6 | self.balance = balance |
| 7 | self.cpuCard = "gg" |
| 8 | } |
| 9 | |
| 10 | // Method to modify the balance |
| 11 | func deposit(_ amount: Int) { |
| 12 | balance += amount |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | // Create an instance (object) of the BankAccount class |
| 17 | let account = BankAccount(balance: 100) |
| 18 | |
| 19 | // Call a method to interact with the object |
| 20 | account.deposit(25) |
| 21 | |
| 22 | // Access a property using dot syntax |
| 23 | print(account.balance) // Prints "125" |
| 24 |