Refactor class structure: redefine BankAccount and PremiumAccount classes, remove Vault and SubVault implementations

This commit is contained in:
Chipperfluff 2026-01-01 17:48:03 +01:00
parent 1a492d9b9a
commit 83fde73df8

View File

@ -1,48 +1,59 @@
oop = require("oop/oop")
function Vault(cls)
cls.setattr(cls, "public_note", nil, "public")
cls.setattr(cls, "protected_note", nil, "protected")
cls.setattr(cls, "private_note", nil, "private")
function BankAccount(cls)
cls.setattr(cls, "owner", nil, cls.visibility.PUBLIC)
cls.setattr(cls, "balance", nil, cls.visibility.PUBLIC)
cls.setattr(cls, "pin", nil, cls.visibility.PROTECTED)
cls.setattr(cls, "token", nil, cls.visibility.PRIVATE)
cls.method("public_method", function(this)
print("public_method:", this.public_note)
end, "public")
cls.method("deposit", function(this, amount)
this.balance = this.balance + amount
print("deposit:", amount, "balance:", this.balance)
end, cls.visibility.PUBLIC)
cls.method("protected_method", function(this)
print("protected_method:", this.protected_note)
end, "protected")
cls.method("withdraw", function(this, amount, pin)
if pin ~= this.pin then
print("bad pin")
return
end
this.balance = this.balance - amount
print("withdraw:", amount, "balance:", this.balance)
end, cls.visibility.PUBLIC)
cls.method("private_method", function(this)
print("private_method:", this.private_note)
end, "private")
cls.method("audit", function(this)
print("audit:", this.owner, "balance:", this.balance)
end, cls.visibility.PROTECTED)
function cls.__init(this)
this.public_note = "hello"
this.protected_note = "shielded"
this.private_note = "secret"
cls.method("rotate_token", function(this)
this.token = "t-" .. tostring(os.time())
print("token rotated")
end, cls.visibility.PRIVATE)
function cls.__init(this, owner, pin)
this.owner = owner
this.balance = 0
this.pin = pin
this.token = "t-" .. tostring(os.time())
end
end
function SubVault(cls)
cls.inherit(Vault)
function PremiumAccount(cls)
cls.inherit(BankAccount)
function cls.show_all(this)
print("inside SubVault:")
this.public_method()
this.protected_method()
this.private_method()
function cls.monthly_bonus(this)
this.deposit(25)
this.audit()
end
end
local v = oop.new(Vault)
print("outside Vault:")
print("public_note:", v.public_note)
v.public_method()
print("protected_note:", v.protected_note)
print("private_note:", v.private_note)
v.protected_method()
v.private_method()
local acct = oop.new(BankAccount, "Kim", 1234)
acct.deposit(100)
acct.withdraw(30, 1234)
local s = oop.new(SubVault)
s.show_all()
local vip = oop.new(PremiumAccount, "Sam", 9999)
vip.monthly_bonus()
local ok, err = pcall(function()
vip.rotate_token()
end)
print("rotate_token from outside:", ok, err)