python從入門到實踐chapter09

from dog import Dog as dg

his_dog = dg('Da Huang', 8)

print(his_dog.name, his_dog.age)

his_dog.sit()
his_dog.roll_over()

print('~'*100)

class Restaurant():
	def __init__(self, restaurant_name, cuisine_type):
		self.restaurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = 0

	def describe_restaurant(self):
		print('This restaurant named as : ' + self.restaurant_name 
			+ ' with cuisin type as ' + self.cuisine_type +'.')

	def open_restaurant(self):
		print('The ' + self.restaurant_name + ' is open !')

	def served_number(self):
		print('There are ' + str(self.number_served) + ' customers are served in '+ self.restaurant_name +' restaurant!')

	def update_served_number(self, update_number):
		self.number_served += update_number


A_restaurant = Restaurant('NA-LA', 'Chinese')
print(A_restaurant.restaurant_name, A_restaurant.cuisine_type)
A_restaurant.describe_restaurant()
A_restaurant.open_restaurant()


B_restaurant = Restaurant('Ta-ta', 'Indian')
B_restaurant.describe_restaurant()
B_restaurant.open_restaurant()

C_restaurant = Restaurant('PA-po', 'American')
C_restaurant.describe_restaurant()
C_restaurant.open_restaurant()

C_restaurant.update_served_number(33)
C_restaurant.update_served_number(22)
C_restaurant.served_number()


print('~'*100)

class IceCreamStand(Restaurant):
	def __init__(self, restaurant_name, cuisine_type):
		super().__init__(restaurant_name, cuisine_type)
		self.flavors = ['aa','bb']

	def show(self):
		print(self.flavors)

restaurant = IceCreamStand('iuiui','popo')
restaurant.describe_restaurant()
restaurant.show()

print('~'*100)

class User():
	def __init__(self, first_name, last_name, age , sex, job):
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		self.sex = sex
		self.job = job
		self.loging_attempts = 0

	def describe_user(self):
		print( self.first_name, self.last_name + ' is a ' + 
			self.sex +' '+self.age +' years old friend, ' + ' who works in ' + self.job +' .')

	def increment_login_attempts(self, increment):
		self.loging_attempts += increment

	def print_times(self):
		print(self.first_name + ' ' + self.last_name + ' has tried ' + str(self.loging_attempts) + ' times!')


	def reset_login_attempts(self):
		self.loging_attempts = 0


A_user = User('zou', 'yi', '33', 'male', 'bank')
A_user.describe_user()

B_user = User('wang', 'tao', '23', 'female', 'shop')
B_user.describe_user()

C_user = User('huang', 'juan', '28', 'female', 'mall')
C_user.describe_user()

C_user.increment_login_attempts(6)
C_user.increment_login_attempts(5)
C_user.increment_login_attempts(2)
C_user.print_times()
C_user.reset_login_attempts()
C_user.print_times()

print('~'*100)


class Admin(User):
	def __init__(self, first_name, last_name, age , sex, job):
		super().__init__(first_name, last_name, age , sex, job)
		self.privileges = ['can add post', 'can delete post', 'can ban user']

	def show_privileges(self):
		print(self.privileges)


A_Admin = Admin('zou', 'yi', '33', 'male', 'bank')
A_Admin.describe_user()
A_Admin.show_privileges()

print('~'*100)

Da Huang 8
Da Huang is now sitting.
Da Huang rolled over!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NA-LA Chinese
This restaurant named as : NA-LA with cuisin type as Chinese.
The NA-LA is open !
This restaurant named as : Ta-ta with cuisin type as Indian.
The Ta-ta is open !
This restaurant named as : PA-po with cuisin type as American.
The PA-po is open !
There are 55 customers are served in PA-po restaurant!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This restaurant named as : iuiui with cuisin type as popo.
['aa', 'bb']
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zou yi is a male 33 years old friend,  who works in bank .
wang tao is a female 23 years old friend,  who works in shop .
huang juan is a female 28 years old friend,  who works in mall .
huang juan has tried 13 times!
huang juan has tried 0 times!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zou yi is a male 33 years old friend,  who works in bank .
['can add post', 'can delete post', 'can ban user']
 

class User():
	def __init__(self, first_name, last_name, age , sex, job):
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		self.sex = sex
		self.job = job
		self.loging_attempts = 0

	def describe_user(self):
		print( self.first_name, self.last_name + ' is a ' + 
			self.sex +' '+self.age +' years old friend, ' + ' who works in ' + self.job +' .')

	def increment_login_attempts(self, increment):
		self.loging_attempts += increment

	def print_times(self):
		print(self.first_name + ' ' + self.last_name + ' has tried ' + str(self.loging_attempts) + ' times!')


	def reset_login_attempts(self):
		self.loging_attempts = 0


A_user = User('zou', 'yi', '33', 'male', 'bank')
A_user.describe_user()

B_user = User('wang', 'tao', '23', 'female', 'shop')
B_user.describe_user()

C_user = User('huang', 'juan', '28', 'female', 'mall')
C_user.describe_user()

C_user.increment_login_attempts(6)
C_user.increment_login_attempts(5)
C_user.increment_login_attempts(2)
C_user.print_times()
C_user.reset_login_attempts()
C_user.print_times()

print('~'*100)


class Admin(User):
	def __init__(self, first_name, last_name, age , sex, job):
		super().__init__(first_name, last_name, age , sex, job)
		self.privileges = Privileges()


class Privileges():
	def __init__(self):
		self.privileges = ['can add post', 'can delete post', 'can ban user']

	def show_privileges(self):
		print(self.privileges)




A_Admin = Admin('zou', 'yi', '33', 'male', 'bank')
A_Admin.describe_user()
A_Admin.privileges.show_privileges()

print('~'*100)

from car import Car

class Battery():

    def __init__(self, battery_size=60):
        self.battery_size = battery_size

    def describe_battery(self):
        print("This car has a " + str(self.battery_size) + "-kWh battery.")  
        
    def get_range(self):
        if self.battery_size == 60:
            range = 140
        elif self.battery_size == 85:
            range = 185            
        message = "This car can go approximately " + str(range)
        message += " miles on a full charge."
        print(message)

    def upgrade_battery(self):
    	if self.battery_size != 85:
    		self.battery_size = 85    
        
class ElectricCar(Car):
    def __init__(self, manufacturer, model, year):        
        super().__init__(manufacturer, model, year)
        self.battery = Battery()

A_car = Battery()
A_car.describe_battery()

A_car.upgrade_battery()
A_car.describe_battery()

zou yi is a male 33 years old friend,  who works in bank .
wang tao is a female 23 years old friend,  who works in shop .
huang juan is a female 28 years old friend,  who works in mall .
huang juan has tried 13 times!
huang juan has tried 0 times!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zou yi is a male 33 years old friend,  who works in bank .
['can add post', 'can delete post', 'can ban user']
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This car has a 60-kWh battery.
This car has a 85-kWh battery.
 

發佈了21 篇原創文章 · 獲贊 2 · 訪問量 5566
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章