python开发入门_python编程从入门到实践同步练习:给子类定义属性和方法

这篇博客介绍了如何使用Python实现面向对象编程,创建一个Admin类,该类继承自User类,并添加了一个privileges属性来存储管理员权限。Admin类包含show_privileges()方法,用于显示管理员的权限列表。示例中创建了一个Admin实例,并调用了show_privileges()方法,展示了权限。
摘要由CSDN通过智能技术生成

1.管理员:管理员是一种特殊的用户。编写一个名为Admin的类,让它继承你为编写的User类。添加一个名为privileges的属性,用于存储一个由字符串(如"can add post"等)组成的列表。编写一个名为show_privileges()的方法,它显示管理员的权限。创建一个Admin实例,并调用这个方法。

class User():

def __init__(self,login_attempts):

self.login_attempts=login_attempts

def increment_login_attempts(self,login_attempts):

self.login_attempts+=1

if login_attempts>=self.login_attempts:

self.login_attempts=login_attempts

print(login_attempts)

else:

print("不能打印该值。")

def reset_login_attempts(self):

login_attempts=0

class Admin(User):

def __init__(self,login_attempts):

self.login_attempts=login_attempts

super().__init__(login_attempts)

privileges=[]

privileges.append(login_attempts)

def show_privileges(self):

print("管理员的权限为:"+str(self.login_attempts))

my_user=User(1)

my_user.increment_login_attempts(2)

my_user.increment_login_attempts(3)

my_user.reset_login_attempts()

Admin=Admin("can not post")

Admin.show_privileges()

9922720e0cf3d7ca2ba9136bc4081c0e6b63a96a.png?token=38715e5a1f04b1869b745570f26cc618

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值