- 帖子
- 34
- 精华
- 0
- 积分
- 126
- 阅读权限
- 20
- 注册时间
- 2017-12-14
- 最后登录
- 2018-2-8
|
如下代码:
class User(object):
"""用户的类"""
def __init__(self, first_name,last_name,password):
self.first_name = first_name
self.last_name = last_name
self.password = password
def describe_user(self):
user_name = self.last_name.title() + " " + self.first_name.title()
print ("User: " + user_name + "\npassword: " + str(self.password))
def greet_user(self):
user_name = self.last_name.title() + self.first_name.title()
print ("Hello " + user_name)
user_1 = User("zhao","derek",123456)
user_1.describe_user()
user_1.greet_user()
问题:
在两个方法中都有一句整理姓名的语句:user_name = self.last_name.title() + " " + self.first_name.title(),
怎样改写才可以不用重复用这一句,并且同时定义对象的时候依然只输入first_name 和 last_name |
|