python学习:静态方法、学生信息管理、魔术方法

dic={ ‘001’:{‘name’:‘张三’,‘age’: 18 ,‘num’:‘001’},
‘002’:{‘name’:‘张二’,‘age’: 28 ,‘num’:‘002’}}
#class FileAction():
@staticmethod
def writeDate(dic):
f=open(“data.txt”,‘w’,encoding=“utf-8”)
for k,v in dic.items():
for k1,v1 in v.items():
f.write( (k1+":"+str(v1)))
if k1==‘num’:
f.write(’\n’)
else:
f.write(’,’)
f.close()
@staticmethod
def readData( ):
f=open(‘data.txt’,‘r’,encoding=“utf-8”)
line=f.readline()
dzd={}
while len(line)>0:
items=line.strip().split(’,’)
zd={}
for item in items:
s=item.split(’:’)
if s[0]‘age’:
zd[s[0]]=int(s[1])
else:
zd[s[0]] = s[1]
if s[0]
‘num’:
dzd[s[1]]=zd
line=f.readline()
print(dzd)
f.close()
FileAction.writeDate(dic)
FileAction.readData()

class People():
tax=0
def init(self,name,age,work,salary,energy=100):
self.name=name
self.age=age
self.work=work
self.salary=salary
self.__energy=energy
def working(self):
People.tax+=self.salary*0.2
self.__energy=self.__energy-30 if self.__energy>30 else 0
def eat(self,money):
self.__energy=self.__energy+10 if self.__energy<90 else 100
def sleep(self):
self.__energy=self.__energy+60 if self.__energy<40 else 100
def say(self):
print(self.name,self.age,self.work,self.salary)
def get_energy(self):
return self.__energy
class Woman(People):
def shopping(self,money):
self.salary-=money
return self.salary# class Man(People):
def say(self):
super().say()
print(“哈哈哈哈哈哈哈哈哈哈刀哈哈哈哈哈哈哈哈哈哈哈”)
m=Man(“DD”,23,“运营总监”,1000000)
m.say()
w=Woman(“LL”,21,“开发”,100000)
w.shopping(20000)
w.say()

a={ ‘001’:{‘name’:‘张三’,‘age’:18,‘address’:‘北京’},
‘002’:{‘name’:‘张二’,‘age’:28,‘address’:‘上海’},
‘003’:{‘name’:‘张大’,‘age’:38,‘address’:‘北京’},
‘004’:{‘name’:‘张四’,‘age’:28,‘address’:‘上海’},
‘005’:{‘name’:‘张六’,‘age’:38,‘address’:‘北京’}
}
class Student():
@staticmethod
def write():
f=open(“d.txt”,“w”,encoding=“utf-8”)
f.write(str(a))
f.close()
@staticmethod
def read():
f=open(“d.txt”,“r”,encoding=“utf-8”)
c=f.read()
f.close()
return eval©
Student.write()
a=Student().read()
while True:
num=int(input(“请输入要进行的操作的编号”))
if num==1:
xh=input(“请输入学号”)
if xh in a.keys():
print(“学号重复”)

     else:
         zd={}
         zd["name"]=input("请输入姓名")
         zd["age"]=int(input("请输入年龄"))
         zd["address"]=input("请输入住址")
         a[xh]=zd
         print(a)
 elif num==2:
     xh = input("请输入学号")
     if xh not in a.keys():
         print("查无此人")
         continue
     else:
         print("要删除的人是",a[xh])
         del a[xh]
         print(a)

 elif num==3:
    xh = input("请输入学号")
     if xh not in a.keys():
         print("查无此人")
         continue
     else:
         a[xh]["name"]=input("请输入姓名")
         a[xh]["age"]=int(input("请输入年龄"))
         a[xh]["address"]=input("请输入地址")
     print(a)
 elif num==4:
     xh = input("请输入学号")
     if xh not in a.keys():
         print("查无此人")
         continue
     else:
         print(a[xh])
 else:
     a=Student.write()
     print("您的输入有误,程序已退出")
     break#exit(0)

class A():
count=0
def init(self,name):
self.name=name
A.count+=1
print(“创建了对象%s,共有%d个对象”%(self.name,A.count))

 def __del__(self):             #         析构函数
     A.count-=1
     print("删除了对象",self.name,"还剩%d个"%A.count)

a=A(“牧尘”)
b=A(“林动”)
del a
del b

class A():
obj=""
def new(cls, *args, **kwargs):
print(“初始化之前先创建了对象”)
return object.new(A)
def init(self,n,name,cid):
self.n=n
self.name=name
self.cid=cid
def add(self, other):
print(“进行了加法运算”)
return self.n+other.n
def sub(self, other):
print(“进行了减法运算”)
return self.n-other.n
def call(self, args, **kwargs):
result=self.n
for a in args:
result=result
a
for k,v in kwargs.items():# result+=v
return result
a=A(5)
b=A(3)
c=a+b
d=a-b
print©
print(d)
e=a(1,2,3,name=3,age=4)
a=A(2,“666”,66666)
print(hasattr(A,“obj”))#判断类是否有指定的属性
print(hasattr(A,“aoe”))

class Car():
def new(cls, *args, **kwargs):
if not hasattr(Car,“inst”):
Car.inst=object.new(Car)
return Car.inst
def init(self,name,cid):
print(“666”)
self.name=name
self.cid=cid
a=Car(“BMW”,“666”)
b=Car(“BC”,“777”)
print(a is b)
print(a.name,a.cid)
print(b.name,b.cid)

你可能感兴趣的:(python学习:静态方法、学生信息管理、魔术方法)