sailan
# 一:定义一个People类,每个人的对象都有名字\年龄\性别三个属性
# 分别完成对这个三个属性的:隐藏\开放接口\property伪装操作
# ps:在开放的接口里严格控制赋值操作的数据类型问题
class People:
def __init__(self, name, age, gender):
self.__name = name
self.__age = age
self.__gender = gender
@property
def name(self):
print('查看')
return self.__name
@name.setter
def name(self, x):
print('重命名')
self.__name = x
@name.deleter
def name(self):
print('删除')
del self.__name
@property
def age(self):
print('查看')
return self.__age
@age.setter
def age(self, x):
print('重命名')
if type(x) is not int:
print('必须是数字整形')
return
self.__age = x
@age.deleter
def age(self):
print('删除')
del self.__age
p1 = People('sai_lan', 20, 'male')
p2 = People('egon', 84, 'unknown')
print(p2.name)
print(p2.age)
p2.age = 101.1
print(p2.age)
# 二:定义MySQL类
#
# 1.对象有id、host、port三个属性
#
# 2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一
#
# 3.提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化
#
# 4.为对象定制方法,save和get_obj_by_id,save能自动将对象序列化到文件中,文件路径为配置文件中DB_PATH,文件名为id号,保存之前验证对象是否已经存在,若存在则抛出异常,;get_obj_by_id方法用来从文件中反序列化出对象
import uuid
import settings
class MySOL:
def __init__(self, ip, port):
self.mid = self.__create_id()
self.ip = ip
self.port = port
def info(self):
print('%s,%s,%s' % (self.mid, self.ip, self.port))
@staticmethod
def __create_id():
return uuid.uuid4()
@classmethod
def from_conf(cls):
return cls(settings.IP, settings.PORT)
a1 = MySOL('10.10.10.101', 2020)
a1.info()
a1 = MySOL.from_conf()
a1.info()
# 三:其他练习:
# class Date:
# def __init__(self,year,month,day):
# self.year=year
# self.month=month
# self.day=day
# @staticmethod
# def now(): #用Date.now()的形式去产生实例,该实例用的是当前时间
# t=time.localtime() #获取结构化的时间格式
# return Date(t.tm_year,t.tm_mon,t.tm_mday) #新建实例并且返回
# @staticmethod
# def tomorrow():#用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
# t=time.localtime(time.time()+86400)
# return Date(t.tm_year,t.tm_mon,t.tm_mday)
#
# a=Date('1987',11,27) #自己定义时间
# b=Date.now() #采用当前时间
# c=Date.tomorrow() #采用明天的时间
#
# print(a.year,a.month,a.day)
# print(b.year,b.month,b.day)
# print(c.year,c.month,c.day)
#
#
# #分割线==============================
# import time
# class Date:
# def __init__(self,year,month,day):
# self.year=year
# self.month=month
# self.day=day
# @staticmethod
# def now():
# t=time.localtime()
# return Date(t.tm_year,t.tm_mon,t.tm_mday)
#
# class EuroDate(Date):
# def __str__(self):
# return 'year:%s month:%s day:%s' %(self.year,self.month,self.day)
#
# e=EuroDate.now()
# print(e) #我们的意图是想触发EuroDate.__str__,但是结果为
# '''
# 输出结果:
# <__main__.Date object at 0x1013f9d68>
# '''
# 因为e就是用Date类产生的,所以根本不会触发EuroDate.__str__,解决方法就是用classmethod
#
# import time
# class Date:
# def __init__(self,year,month,day):
# self.year=year
# self.month=month
# self.day=day
# # @staticmethod
# # def now():
# # t=time.localtime()
# # return Date(t.tm_year,t.tm_mon,t.tm_mday)
#
# @classmethod #改成类方法
# def now(cls):
# t=time.localtime()
# return cls(t.tm_year,t.tm_mon,t.tm_mday) #哪个类来调用,即用哪个类cls来实例化
#
# class EuroDate(Date):
# def __str__(self):
# return 'year:%s month:%s day:%s' %(self.year,self.month,self.day)
#
# e=EuroDate.now()
# print(e) #我们的意图是想触发EuroDate.__str__,此时e就是由EuroDate产生的,所以会如我们所愿
# '''
# 输出结果:
# year:2017 month:3 day:3
# '''