zoukankan      html  css  js  c++  java
  • Python从入门到实践第九章-类

    9-1

    class Restaurant():
        def __init__(self,restaurant_name,cuisine_type):
            self.restaurant_name = restaurant_name
            self.cuisine_type = cuisine_type
        def describe_restaurant(self):
            print(self.restaurant_name.title())
            print(self.cuisine_type.title())
        def open_restaurant(self):
            print("The restaurant is opening")
    restaurant = Restaurant('quanjude','kaoya')
    restaurant.describe_restaurant()
    restaurant.open_restaurant()

    9-2略

    9-3      数字实参不加引号   好像是这样的。。因为报了错

    class User():
        def __init__(self, first_name, last_name,age):
            self.first_name = first_name
            self.last_name = last_name
            self.age = age
            
        def describe_user(self):
            print('Name: ' +self.last_name + self.first_name+ ' age : ' + str(self.age) )
    
        def greet_user(self):
            print('Hello! ' + self.last_name + self.first_name)
    user_a = User('xiangnai', 'taonaimu','22')
    user_b = User('yongmei', 'shentian','23')
    user_a.describe_user()
    user_b.describe_user()
    user_a.greet_user()
    user_b.greet_user()

    9-4 写95的时候发现这个题没看到后面的  不过和95差不多不补了

    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(self.restaurant_name.title())
            print(self.cuisine_type.title())
        def open_restaurant(self):
            print("The restaurant is opening")
        def read_serve_number(self):
            print("The numer restanrant serverd is " + 
                    str(self.number_served))
    restaurant = Restaurant('restaurant','aaa')
    restaurant.describe_restaurant()
    restaurant.read_serve_number()
    restaurant.number_served = 20
    restaurant.read_serve_number()
    输出:
    Restaurant
    Aaa
    The numer restanrant serverd is 0
    The numer restanrant serverd is 20

    9-5

    class User():
        def __init__(self, first_name, last_name,age):
            self.first_name = first_name
            self.last_name = last_name
            self.age = age
            self.login_attemps = 0
        def describe_user(self):
            print('Name: ' +self.last_name + self.first_name+ ' age : ' + str(self.age) )
    
        def greet_user(self):
            print('Hello! ' + self.last_name + self.first_name)
            
        def increment_login_attemps(self):
            self.login_attemps += 1
    
        def read_login(self):
            print("Now the number of people logining is " + str(self.login_attemps))
            
        def reset_login(self):
            self.login_attemps = 0
    user_a = User('san','zhang','20')
    user_a.increment_login_attemps()
    user_a.increment_login_attemps()
    user_a.increment_login_attemps()
    user_a.increment_login_attemps()
    user_a.read_login()
    user_a.reset_login()
    user_a.read_login()
    输出:
    Now the number of people logining is 4
    Now the number of people logining is 0

     9-6

    class Restaurant():
        def __init__(self,restaurant_name,cuisine_type):
            self.restaurant_name = restaurant_name
            self.cuisine_type = cuisine_type
        def describe_restaurant(self):
            print(self.restaurant_name.title())
            print(self.cuisine_type.title())
        def open_restaurant(self):
            print("The restaurant is opening")
    class IceCreamStand(Restaurant):
        def __init__(self,restaurant_name,cuisine_type):
            super().__init__(restaurant_name,cuisine_type)
            self.flavors = []
        def set_flavors(self,flavors):
            self.flavors = flavors 
        def describe_flavors(self):
            print("This is the flavors:" + str(self.flavors))#这里一开始没加str报错 can only concatenate str (not "list") to str  不懂为啥
    Newres = IceCreamStand('quanjude','kaoya')
    Newres.set_flavors(['apple','orange','banana'])
    Newres.describe_flavors()
    输出:
    This is the flavors:['apple', 'orange', 'banana']

    9-7  看别人的学了一手 end='', 可以继续后面print的内容 挺好用  

    class User():
        def __init__(self, first_name, last_name,age):
            self.first_name = first_name
            self.last_name = last_name
            self.age = age
            self.login_attemps = 0
        def describe_user(self):
            print('Name: ' +self.last_name + self.first_name+ ' age : ' + str(self.age) )
    
        def greet_user(self):
            print('Hello! ' + self.last_name + self.first_name)
            
        def increment_login_attemps(self):
            self.login_attemps += 1
    
        def read_login(self):
            print("Now the number of people logining is " + str(self.login_attemps))
            
        def reset_login(self):
            self.login_attemps = 0
    class Admin(User):
        def __init__(self, first_name, last_name, age):
            super().__init__(first_name, last_name, age)  
     
            self.privileges = ['can add post', 'can delete post', 'can ban user']  
     
        def show_privileges(self):
            print('管理员权限有:', end='')
            for privilege in self.privileges:
                print(privilege + ',', end=' ')
     
     
    admin = Admin('San', 'Zhang', '20')
    admin.show_privileges()
    输出:
    管理员权限有:can add post, can delete post, can ban user,

    9-8 略 编写一个类,然后admin中 self.pr... = pr...()

    9-9

        def upgrade_battery(self):
            if self.battery_size!=85:
                self.battery_size=85

    9-14

    from random import randint
    
    class Die():
        def __init__(self,sides=6):
            self.sides = sides
        def roll_die(self):
            print(randint(1,self.sides))
    die_six = Die(6)
    for i in range(10):
        die_six.roll_die()
    die_ten = Die(10)
    for i in range(10):
        die_ten.roll_die()
    die_20 = Die(20)
    for i in range(10):
        die_20.roll_die()

    有点3分钟的感觉了。。要认真一点。

  • 相关阅读:
    list tuple dict 方法
    字典的方法
    看不懂的缩写
    canvas画图
    DOM2和DOM3
    表单脚本
    事件
    DOM扩展
    DOM
    BOM
  • 原文地址:https://www.cnblogs.com/zhangyueba/p/12249247.html
Copyright © 2011-2022 走看看