python面向对象的特征_python面向对象的三大特性

总结

如果写代码时,函数比较多比较乱

可以将函数归类并放到同一个类中

函数如果有一个反复使用的公共值,则可以放到对象中

# 示例:循环让用户输入:用户名/密码/邮箱。 输入完成后再进行数据打印。

# 以前的写法:

USER_LIST = []

while True:

user = input('请输入用户名:')

pwd = input('请输入密码:')

email = input('请输入邮箱:')

temp = {'username':user,'password':pwd,'email':email}

USER_LIST.append(temp)

for item in USER_LIST:

temp = "我的名字:%s,密码:%s,邮箱%s" %(item['username'],item['password'],item['email'],)

print(temp)

# 面向对象写法一:

class Person:

def __init__(self,user,pwd,email):

self.username = user

self.password = pwd

self.email = email

USER_LIST = [对象(用户/密码/邮箱),对象(用户/密码/邮箱),对象(用户/密码/邮箱)]

while True:

user = input('请输入用户名:')

pwd = input('请输入密码:')

email = input('请输入邮箱:')

p = Person(user,pwd,email)

USER_LIST.append(p)

for item in USER_LIST:

temp = "我的名字:%s,密码:%s,邮箱%s" %(item.username,item.password,item.email,)

print(temp)

# 面向对象写法二:

class Person:

def __init__(self,user,pwd,email):

self.username = user

self.password = pwd

self.email = email

def info(self):

return "我的名字:%s,密码:%s,邮箱%s" %(item.username,item.password,item.email,)

USER_LIST = [对象(用户/密码/邮箱),对象(用户/密码/邮箱),对象(用户/密码/邮箱)]

while True:

user = input('请输入用户名:')

pwd = input('请输入密码:')

email = input('请输入邮箱:')

p = Person(user,pwd,email)

USER_LIST.append(p)

for item in USER_LIST:

msg = item.info()

print(msg)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值