Eiffel小抄

class


note
description: "Simple bank accounts"
 
class
ACCOUNT
 
feature -- Access
 
balance: INTEGER
-- Current balance
 
deposit_count: INTEGER
-- Number of deposits made since opening
do
if all_deposits /= Void then
Result := all_deposits.count
end
end
 
feature -- Element change
 
deposit (sum: INTEGER)
-- Add `sum' to account.
require
non_negative: sum >= 0
do
if all_deposits = Void then
create all_deposits
end
 
all_deposits.extend (sum)
balance := balance + sum
ensure
one_more_deposit: deposit_count = old deposit_count + 1
updated: balance = old balance + sum
end
 
feature {NONE} -- Implementation
 
all_deposits: DEPOSIT_LIST
-- List of deposits since account's opening.
 
invariant
consistent_balance:
(all_deposits /= Void) implies (balance = all_deposits.total)
zero_if_no_deposits:
(all_deposits = Void) implies (balance = 0)
 
end -- class ACCOUNT

inheritance


note
description: "Savings accounts"
 
class
SAVINGS_ACCOUNT
 
inherit
ACCOUNT
redefine
deposit
end
 
feature -- Element change
 
deposit (sum: INTEGER)
-- Add sum to account.
do
... New implementation (see below) ...
end
 
... Other features ...
 
end -- class SAVINGS_ACCOUNT

class D
inherit
A
rename
g as f
-- g was effective in A
undefine
f
export
{NONE} all
{ANY} capacity
end
end

class TEACHING_ASSISTANT
 
inherit
TEACHER
rename
computer_account as faculty_account
select
faculty_account
end
 
STUDENT
rename
computer_account as student_account
end
end

deferred class

deferred class
LIST [G]
 
feature -- Access
 
count: INTEGER
-- Number of items in list
do
... See below; this feature can be effective ...
end
 
feature -- Element change
 
extend (x: G)
-- Add `x' at end of list.
require
space_available: not full
deferred
ensure
one_more: count = old count + 1
end
 
... Other feature declarations and invariants ...
 
end -- class LIST

operator

    infix "<=" (other: like Current): BOOLEAN
-- Is current object less than or equal to `other'?
require
other_exists: other /= Void
do
Result := (Current < other) or is_equal (other)
ensure
definition: Result = (Current < other) or is_equal (other)
end

create

            create x.make (2000)
create x
            create {SAVINGS_ACCOUNT} x.make (2000)
some_routine (create {ACCOUNT}.make (2000))

if

            if ... then
...
elseif ... then
...
else
...
end

switch

            inspect
exp
when v1 then
inst
when v2 then
inst2
...
else
inst0
end

loop

            from
initialization
until
exit
invariant -- optional
inv
variant -- optional
var
loop
body
end

assert

            check
x_not_void: x /= Void end
-- Because x was obtained as a clone of y,
-- and y is not void because [etc.]
end

exception

        local
attempts: INTEGER
do
last_character := low_level_read_function (f)
rescue
attempts := attempts + 1
if attempts < Max_attempts then
retry
end
end

转载于:https://www.cnblogs.com/frankbadpot/archive/2009/10/14/1582787.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值