zoukankan      html  css  js  c++  java
  • day21作业

    1、定义MySQL类(参考答案:http://www.cnblogs.com/linhaifeng/articles/7341177.html#_label5)
      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 pickle
    import os
    class Mysql:
        def __init__(self,host,port):
            self.id = self.create_id()
            self.host = host
            self.port = port
        def save(self):
            if not self.is_exists:
                raise PermissionError('对象已经存在')
            filepath = r'%s%s%s'%(settings.DB_PATH,os.sep,self.id)
            pickle.dump(self,open(filepath,'wb'))
    
        @property
        def is_exists(self):
            tag = True
            files = os.listdir(settings.DB_PATH)
            for file in files:
                file_abspath = r'%s%s%s'%(settings.DB_PATH,os.sep,file)
                obj = pickle.load(open(file_abspath,'rb'))
                if self.host == obj.host and self.port == obj.port:
                    tag = False
                    break
            return tag
        @staticmethod
        def get_obj_by_id(id):
            file_abspath = r'%s%s%s'%(settings.DB_PATH,os.sep,id)
            return pickle.load(open(file_abspath,'rb'))
        @staticmethod
        def create_id():
            return str(uuid.uuid1())
        @classmethod
        def from_conf(cls):
            print(cls)
            return cls(settings.HOST,settings.PORT)
    
    conn = Mysql.from_conf()
    conn.save()
    
    conn1=Mysql('127.0.0.1',3306)
    conn1.save()
    
    obj=Mysql.get_obj_by_id('7e6c5ec0-7e9f-11e7-9acc-408d5c2f84ca')
    print(obj.host)
    
    
    
    2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放
    		参考答案(http://www.cnblogs.com/linhaifeng/articles/7340801.html#_label4)
    
    import cmath
    class circle1:
        def __init__(self,radius):
            self.__radius = radius
            self.round = self.round()
            self.area = self.area()
    
        def round(self):
            return cmath.pi * 2 * self.__radius
    
        def area(self):
            return cmath.pi * self.__radius * self.__radius
    
    aa = circle1(11)
    print(aa.area)
    
    
    3、使用abc模块定义一个phone抽象类 并编写一个具体的实现类
    import abc
    class Phone(metaclass=abc.ABCMeta):
        @abc.abstractmethod
        def call(self):
            print('call.....')
        @abc.abstractmethod
        def watch_TV(self):
            print('Watch_TV')
    class Iphone(Phone):
        def call(self):
            print('call...')
        def watch_TV(self):
            print('watch_TV')
        def powerin(self):
            print('powerin')
    iphone = Iphone()
    iphone.call()
    iphone.watch_TV()
    iphone.powerin()
    print(iphone)
    
    
  • 相关阅读:
    一句话搞定qt4 开发环境
    一句话随笔.....
    es6学习笔记(二)
    Vue学习笔记(一)
    es6学习笔记(一)
    Java中的一些基础概念
    你了解Java中String的substring函数吗?
    Ajax请求与浏览器缓存
    QCon杭州2012技术开发大会感受
    为什么选择使用Sass而不是Less?
  • 原文地址:https://www.cnblogs.com/kaizi111/p/11657216.html
Copyright © 2011-2022 走看看