组合封装作业day21

1、定义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方法用来从文件中反序列化出对象

# settings.py
import os

IP='127.0.0.1'
PORT='3306'
DB_PATH = os.path.join(os.path.dirname(__file__),'db')
if not os.path.exists(DB_PATH):
    os.mkdir(DB_PATH)
    
# MYSQL1.py
import settings
import uuid
import os
import pickle
class MYSQL:
    def __init__(self,host,port):
        self.id = self.create_id()
        self.port = port
        self.host = host
    @staticmethod
    def create_id():
        return uuid.uuid4()

    @classmethod
    def conf_flie(cls):
       return cls(settings.IP,settings.PORT)

    def save(self):
        if not self.select_file:
            raise PermissionError('对象已存在!')
        file_path=os.path.join(settings.DB_PATH,str(self.id))
        with open(file_path,'wb') as f:
            pickle.dump(self,f)

    @property
    def select_file(self):
        tag=True
        files = os.listdir(settings.DB_PATH)
        for file in files:
            file_path=os.path.join(settings.DB_PATH,file)
            with open(file_path,'rb') as f:
                obj=pickle.load(f)
                if obj.host == self.host and obj.port == self.port:
                    tag = False
                    break
        return tag

    @staticmethod
    def get_obj_by_id(id):
        file_path=os.path.join(settings.DB_PATH,id)
        if not os.path.exists(file_path):
            print('该路径不存在!')
            return
        with open(file_path,'rb') as f:
            return pickle.load(f)

# obj=MYSQL.conf_flie()
# obj.save()
# obj1=MYSQL('127.0.0.1','3306')
# obj1.save()


# print(obj)
# print(obj.port)
# print(obj.create_id())

obj2=MYSQL.get_obj_by_id('3ca4b877-e5af-4451-9554-90bf307f2f6c')
print(obj2)
print(f'''
        IP:{obj2.host}
        PORT:{obj2.port}
''')

2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放

import cmath
class Circle:

    def __init__(self,radius):
        self.__radius=radius

    @property
    def parameter(self):
         return cmath.pi*self.__radius*2

    @property
    def area(self):
        return cmath.pi*(self.__radius)**2

int_radius=int(input('请输入半径:').strip())
c=Circle(int_radius)
print(c.parameter)
print(c.area)
print(c.radius)

3、使用abc模块定义一个phone抽象类 并编写一个具体的实现类

import abc

class Phone(metaclass=abc.ABCMeta):

    # 供电
    @abc.abstractmethod
    def supply_power(self):
        pass

    # 检测程序
    @abc.abstractmethod
    def test_program(self):
        pass

    # 输出复位信号
    @abc.abstractmethod
    def output_REF_ON(self):
        pass

    # 调用开机程序
    @abc.abstractmethod
    def transfer_start_program(self):
        pass

    # 建立通信链接
    @abc.abstractmethod
    def establish_communication_link(self):
        pass

class Xiaomi_mate3(Phone):

    # 供电
    def supply_power(self):
        print('手机供电中。。。')

    # 检测程序
    def test_program(self):
        print('正在检测开机程序。。。')

    # 输出复位信号
    def output_REF_ON(self):
        print('输出各路复位信号。。。。')

    # 调用开机程序
    def transfer_start_program(self):
        print('调用开机程序。。。。')

    # 建立通信链接
    def establish_communication_link(self):
        print('通信链接中。。。')

    # 派生
    def load_xiaomi_desk(self):
            pass

xiaomi = Xiaomi_mate3()
xiaomi.transfer_start_program()
原文地址:https://www.cnblogs.com/shin09/p/11657321.html