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() + " is " + self.cuisine_type + ".")
#
#     def open_restaurant(self):
#         print("The restaurant is open.")


# restaurantA = Restaurant('haoweidao', 'open')
# restaurantA.describe_restaurant()
# restaurantA.cuisine_type
# 9-2
# restaurantB = Restaurant('huaiweidao', 'close')
# restaurantC = Restaurant('zhenmeiwei', 'open')


# restaurantA.describe_restaurant()
# restaurantB.describe_restaurant()
# restaurantC.describe_restaurant()
# 9-3
# class User:
#     def __init__(self, first_name, last_name, profile):
#         self.first_name = first_name
#         self.last_name = last_name
#         self.profile = profile
#
#     def describe_user(self):
#         print("first_name: " + self.first_name + " last_name: " + self.last_name + " profile: " + str(self.profile))
#
#     def greet_user(self):
#         print("Hello, " + self.first_name + self.last_name + "!")
#
#
# user1 = User('h', 'zx', 22)
# user1.describe_user()
# user1.greet_user()
# user1 = User('x', 'vv', 22)
# user1.describe_user()
# user1.greet_user()
# 9-4
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() + " is " + self.cuisine_type + ".")

    def open_restaurant(self):
        print("The restaurant is open.")
# restaurantA = Restaurant('haoweidao', 'open')
# print(str(restaurantA.number_served) + " people have eaten in this restaurant.")
# restaurantA.number_served = 100
# print(str(restaurantA.number_served) + " people have eaten in this restaurant.")
# 9-5
class User:
    def __init__(self, first_name, last_name, profile):
        self.first_name = first_name
        self.last_name = last_name
        self.profile = profile
        self.login_attempts = 0

    def describe_user(self):
        print("first_name: " + self.first_name + " last_name: " + self.last_name + " profile: " + str(self.profile))

    def increment_login_attempts(self):
        self.login_attempts += 1

    def reset_login_attempts(self):
        self.login_attempts = 0

    def greet_user(self):
        print("Hello, " + self.first_name + self.last_name + "!")
# user1 = User('h', 'zx', '22')
# print(user1.login_attempts)
# user1.increment_login_attempts()
# print(user1.login_attempts)
# user1.increment_login_attempts()
# print(user1.login_attempts)
# user1.increment_login_attempts()
# print(user1.login_attempts)
# user1.reset_login_attempts()
# print(user1.login_attempts)
# 9-6
class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name, cuisine_type):
        super().__init__(restaurant_name, cuisine_type)
        self.flavors = {'BaiVanilla', 'Chocolate', 'Strawberry'}

    def show_flavors(self):
        print("IceCream flavors include:")
        for flavor in self.flavors:
            print(flavor)

# iceCreamStand1 = IceCreamStand('mixuebingcheng', 'open')
# iceCreamStand1.describe_restaurant()
# iceCreamStand1.show_flavors()

# 9-7
# class Adimin(User):
#     def __init__(self, first_name, last_name, profile, privileges):
#         super().__init__(first_name, last_name, profile)
#         self.privileges = privileges
#
#     def show_privileges(self):
#         print("Administrator's rights:")
#         for privilege in self.privileges:
#             print(privilege)

# privileges = {'can add post', 'can delete post'}
# hzx = Adimin('h', 'zx', '22', privileges)
# hzx.show_privileges()
# 9-8
class Privileges:
    def __init__(self, privileges):
        self.privileges = privileges

    def show_privileges(self):
        print("Administrator's rights:")
        for privilege in self.privileges:
            print(privilege)

class Adimin(User):
    def __init__(self, first_name, last_name, profile):
        super().__init__(first_name, last_name, profile)
        self.privileges = Privileges(privileges={'can add post'})


# hzx = Adimin('h', 'zx', '22')
# hzx.privileges.show_privileges()
# 9-9
class Battery():
    """一次模拟电动汽车电瓶的简单尝试"""

    def __init__(self, battery_size=70):
        """初始化电瓶的属性"""
        self.battery_size = battery_size

    def describe_battery(self):
        """打印一条描述电瓶容量的消息"""
        print("This car has a " + str(self.battery_size) + "-kWh battery.")

    def upgrade_battery(self):
        """检查电瓶容量"""
        if self.battery_size != 85:
            self.battery_size = 85


class Car:
    """一次模拟汽车的简单尝试"""

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def read_odometer(self):
        """打印一条指出汽车里程的信息"""
        print("This car has " + str(self.odometer_reading) + " miles on it.")

    def update_odometer(self, mileage):
        """
        将里程表读数设置为指定的值
        禁止将里程表读数往回调
        """
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self, miles):
        """将里程表读数增加指定的量"""
        self.odometer_reading += miles

    def get_descriptive_name(self):
        """返回整洁的描述性信息"""
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

class ElectricCar(Car):
    """电动汽车的独特之处"""

    def __init__(self, make, model, year):
        """初始化父类的属性,再初始化电动汽车特有的属性"""
        super().__init__(make, model, year)
        self.battery = Battery()
# my_tesla = ElectricCar('tesla', 'model s', 2016)
# my_tesla.battery.describe_battery()
# my_tesla.battery.upgrade_battery()
# my_tesla.battery.describe_battery()
# 9-13
from _collections import OrderedDict
vocabularys = OrderedDict()
vocabularys['if'] = '判断语句'
vocabularys['pop'] = '删除列表中的指定元素(指定索引)'
vocabularys['sort'] = '用来对列表进行排序(按照字母)的方法'
vocabularys['lower'] = '把字符串转换为小写'
vocabularys['strip'] = '去掉字符串两端空格'
# for key, value in vocabularys.items():
#     print(key + ": " + value)
# 9-14
from random import randint
class Die:
    def __init__(self, x, y):
        self.sides = 6
        self.x = x
        self.y = y

    def roll_die(self):
        print(randint(self.x, self.y))
die1 = Die(1, 20)
for i in range(1, 11):
    die1.roll_die()

# 9-15

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值