1:property(get_name,set_name)的使用
class User(object):
def __init__(self,name,age,gender):
self.__name = name
self.__age = age
self.__gender = gender
# 私有属性获取方法
def get_name(self):
return self.__name
def set_name(self,name):
self.__name = name
def speak(self):
print("我的名字叫:"+ self.__name +",我的年龄是:"+ str(self.__age) +"岁,我的性别是:"+ self.__gender)
name = property(get_name,set_name) # 将get_name,set_name 封装起来可以直接用name来使用
# main
if __name__ == '__main__':
u1 = User("郑洋洋",56,"女")
# u1.age = 65
# u1.name = "楼彭辉"
# u1.set_name("楼彭辉")
u1.name = "刘建宏"
u1.name = "liu"
print(u1.name)
u1.speak()
2:另一个变形
class User(object):
def __init__(self,name,age,gender):
self.__name = name
self.__age = age
self.__gender = gender
@property
def name(self):
return self.__name
@name.setter
def name(self,name):
self.__name = name
def speak(self):
# name = property(get_name,set_name)
# import copy
# main
if __name__ == '__main__':
pass
# u1 = User("郑洋洋",56,"女")
# u1.age = 65
# u1.name = "楼彭辉"
# u1.set_name("楼彭辉")
# u1.name = "刘建宏"
# print(u1.name)
# u1.speak()
# [x for x in range(10)]
# [x for x in range(10) if x%2==0]
# ls = []
# for x in range(10):
# if x % 2 == 0:
# ls.append(x)
# print(ls)
# [x+y for x in range(10) for y in range(10)]
# for x in range(10):
# for y in range(10):
# print(x + y)
网友评论