zoukankan      html  css  js  c++  java
  • 面向对象11号作业·

    '''
    0、课堂代码理解,并敲两遍以上 (技术的牛逼是靠量的积累)
    
    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方法用来从文件中反序列化出对象
    
    2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放
    		参考答案(http://www.cnblogs.com/linhaifeng/articles/7340801.html#_label4)
    	
    3、使用abc模块定义一个phone抽象类 并编写一个具体的实现类
    
    4、着手编写选课系统作业:http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label15
    
    
    #1.
    import settings
    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('对象已存在')
            file_path=r'%s%s%s' %(settings.DB_PATH,os.sep,self.id)
            pickle.dump(self,open(file_path,'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)
    
    #2.
    import math
    class Circle:
        def __init__(self,radius): #半径
            self.__radius = radius
        def save(self):
            return self.__radius
        @property
        def area(self):
            return math.pi * self.__radius **2 #圆的面积
        @property
        def perimeter(self):
            return 2 * math.pi * self.__radius #算周长
    
    d = Circle(8)
    print(d.save())
    print(d.area)
    print(d.perimeter)
    
    #3.
    import abc
    
    class Phone(metaclass=abc.ABCMeta):
        @abc.abstractmethod
        def call(self):
            pass
    
    
    class Android(Phone):
        def call(self):
            print('安卓用户')
    
    class Apple_AMC(Phone):
        def call(self):
            print('苹果用户')
    
    apple = Apple_AMC()
    apple.call()
    
    
    #https://github.com/nickchen121/course_selected
    
  • 相关阅读:
    UVA10302 【Summation of Polynomials】
    小Z 系列 解题报告
    Dsu on tree
    轻重链剖分
    二分图匹配
    题解 P2455 【[SDOI2006]线性方程组】
    闫氏DP分析法
    扩展域并查集
    bindColumn、bindParam与bindValue的区别
    如何获取二维数组的列数
  • 原文地址:https://www.cnblogs.com/shaozheng/p/11656355.html
Copyright © 2011-2022 走看看