内容简介:031单例+作业
class Person:
__v = None
@classmethod
def get_instance(cls):
if cls.__v:
return cls.__v
else:
cls.__v = Person()
return cls.__v
#不使用类()创建对象
obj = Person.get_instance()
print(obj)
python2.*类型改,但是会报错,这个错误我就不分析了,复杂
class Singleton(object):
def __new__(cls,*args,**kwargs):
if not hasattr(cls,'_inst'):
cls._inst = super(Singleton,cls).__new__(cls,*args,**kwargs)
###### TypeError: object() takes no parameters
return cls._inst
if __name__ == '__main__':
class A(Singleton):
def __init__(self,s):
self.s = s
a = A('apple')
b = A('banana')
print(id(a), a.s)
print(id(b), b.s)
可能用最上面的就好了,我找时间去看一下其他的教程是怎么样的。然后再补上
作业: 选课系统 (我后面补/捂脸)
角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级,班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图,可以注册,交学费,选择班级,
6.2 讲师视图,讲师可管理自己的班级,上课时选择班级,查看班级学员列表,修改所管理的学员的成绩
6.3 管理视图,创建讲师,创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
源文档 <http://www.cnblogs.com/alex3714/articles/5188179.html> 源文档有一些例子,可以看一下
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Elements of Programming
Alexander A. Stepanov、Paul McJones / Addison-Wesley Professional / 2009-6-19 / USD 39.99
Elements of Programming provides a different understanding of programming than is presented elsewhere. Its major premise is that practical programming, like other areas of science and engineering, mus......一起来看看 《Elements of Programming》 这本书的介绍吧!