DAY_2

2024-08-27 23:08:41 浏览数 (2)

将(某类的)实例用作(某其他类的)属性

class IceCreamStand(Restaurant):

def __init__(self,restaurant_name, cuisine_type):

super().__init__(restaurant_name, cuisine_type)

self.flavors = ['草莓','巧克力','芒果']

def get_flavors(self):

print('现在提供%s这些口味'%self.flavors)

第一个作业, 练习子类的写法,初始化父类的属性和子类的属性

class User:

def __init__(self,first_name, last_name, sex, age):

self.first_name = first_name

# self.last_name = last_name

# self.sex = sex

# self.age = age

self.login_attempts = 0

def describe_user(self):

print('姓名: %s%sn性别: %sn年龄: %s'%(self.last_name,self.first_name,self.sex,self.age))

#print('姓名: %s %sn性别:%sn年龄:'%(self.last_name,self.first_name,self.sex))

def greet(self):

print('欢迎%s%s加入社区'%(self.last_name,self.first_name))

def increment_login_attempts(self):

self.login_attempts = 1

def reset_login_attempts(self):

self.login_attempts = 0

class Privileges:

privileges = ["can add post","can delete post","can ban user"]

def show_privileges(self):

print(self.privileges)

class Admin(User):

def __init__(self,first_name, last_name, sex, age):

super().__init__(first_name, last_name, sex, age)

self.privileges = Privileges()

创建Privileges类 用其实例当做 类Admin的属性

0 人点赞