类的小笔记 - 书344页例子13.1(hotel.py)- 使用缺省参数进行实例化,稍稍有修改

class HotelRoomCalc(object):
    'Hotel room rate calculator'
    
    def __init__(self, rt, sales = 0.085, rm = 0.1):
        '''HotelRoomCalc default arguements:
        sales tax == 8.5% and room tax == 10% '''
        self.salesTax = sales
        self.roomTax = rm
        self.roomRate = rt
        
    def calcTotal(self, days =1):
        'Calcultate total; default to daily rate'
        daily = round((self.roomRate * 
                       (1 + self.roomTax + self.salesTax)), 2)
        return float(days) * daily
    
sfo = HotelRoomCalc(299)
print 'Instance San Francisco'
print 'Daily rate $', sfo.calcTotal()
print '2-day rate $', sfo.calcTotal(2)

sea = HotelRoomCalc(189, 0.086, 0.058)
print '\nInstance Seattle'
print 'Daily rate $', sea.calcTotal()
print '2-day rate $', sea.calcTotal(4)

wasWkDay = HotelRoomCalc(169, 0.045, 0.02)
wasWkEnd = HotelRoomCalc(119, 0.045, 0.02)
print '\nWashington, D.C. '
print '$', wasWkDay.calcTotal(5) + wasWkEnd.calcTotal()
    

运行结果

Instance San Francisco
Daily rate $ 354.31
2-day rate $ 708.62

Instance Seattle
Daily rate $ 216.22
2-day rate $ 864.88

Washington, D.C. 
$ 1026.63

 

你可能感兴趣的:(EL)