002Visual Basic 策略模式

2021-10-06 11:33:07 字數 3079 閱讀 8088

1.窗體

2.上下文

public class cashcontext

private mcashsuper as cashsuper

public sub new(byval type as string)

select case type

case "正常收費"

dim cs0 as cashnormal = new cashnormal("正常收費")

mcashsuper = cs0

case "滿300返100"

dim cs1 as cashreturn = new cashreturn("300", "100")

mcashsuper = cs1

case "打8折"

dim cs2 as cashrebate = new cashrebate("0.8")

mcashsuper = cs2

end select

end sub

public function getresult(byval money as double) as double

return mcashsuper.acceptcash(money)

end function

end class

3.抽象策略和具體策略 

'現金收費抽象類

public class cashsuper

public overridable function acceptcash(byval money as double) as double

return money

end function

end class

'正常收費

public class cashnormal

inherits cashsuper

dim mymoneyrebate as double

public sub new(byval moneyrebate as string)

mymoneyrebate = 1

end sub

public overrides function acceptcash(byval money as double) as double

return money * mymoneyrebate

end function

end class

'打折收費

public class cashrebate

inherits cashsuper

dim mymoneyrebate as double

public sub new(byval moneyrebate as string)

mymoneyrebate = double.parse(moneyrebate)

end sub

public overrides function acceptcash(byval money as double) as double

return money * mymoneyrebate

end function

end class

public class cashreturn

inherits cashsuper

private mymoneycondition as double

private mymoneyreturn as double

public sub new(byval moneycondition as string, byval moneyreturn as string)

mymoneycondition = double.parse(moneycondition)

mymoneyreturn = double.parse(moneyreturn)

end sub

public overrides function acceptcash(byval money as double) as double

dim result as double

result = money

if money >= mymoneycondition then

result = money - math.floor(money / mymoneycondition) * mymoneyreturn

end if

return result

end function

end class

4.客戶端**

public class form1

dim total as double = 0

private sub txtok_click(byval sender as system.object, byval e as system.eventargs) handles txtok.click

dim mycashcontext as new cashcontext(combobox1.selecteditem.tostring)

dim totalprice as double

dim tmp as double = convert.todouble(txtprice.text) * convert.todouble(txtcount.text)

totalprice = mycashcontext.getresult(tmp)

listbox1.items.add("單價:" + txtprice.text + "數量:" + txtcount.text + "合計:" + totalprice.tostring)

total += totalprice

lbltotleprice.text = total.tostring

end sub

end class

shell程式設計002

shell的語法 變數 變數名前面加乙個 符號來訪問它的內容,再用echo命令將它的內容輸出到終端上 read命令將使用者的輸入命令賦給乙個變數,再有echo輸出 引號的使用 新建乙個指令碼 vim variables bin sh variables執行 我們可以看出使用雙引號並不影響變數的內容,...

002 編譯核心

為了後續的學習研究和測試教科書上的例子程式,重新編譯一遍kernel是個不錯的選擇。下面是重新編譯kernel的步驟 root root apt get install linux source 2.6.24 root usr src linux source 2.6.24 make mrpoper...

002 浮點型別

浮點型別表示數字包含小數部分。共兩種浮點型別 型別儲存要求 表示範圍 float 4 byte 大約 3.40282347e 38f 有效小數字數6 7位 double 8 byte 大約 1.79769313486231570e 308 有效小數為15位 double的數字的精度是float的兩倍...