wyh551202 发表于 2022-10-14 16:46:13

2022-10-14 学习Python-Day18

又学了两节{:10_275:}P65-P66
###########################################P65 对象与类
class C:
    def __init__(self,x):
      self.__x=x
    def set_x(self,x):
      self.__x=x
    def get_x(self):
      print(self.__x)
c=C(250)
c.get_x()
c.set_x(520)
print(c.__dict__)
print(c._C__x)
class D:
    def __func(self):
      print('Hello Fishc.')
d =D()
d._D__func()
class C:
    def __init__(self,x):
      self.x=x
c=C(250)
print(c.x)
c.y=520
print(c.__dict__)
c.__dict__['z']=666
print(c.z)
class C:
    __slots__ = ['x','y','z']
    def __init__(self,x):
      self.x=x
c=C(250)
print(c.x)
c.y=520
print(c.y)
# print(c.z)
###########################################P66 对象与类
class CapStr(str):
    def __new__(cls, string):
      string=string.upper()
      return super().__new__(cls,string)
cs=CapStr('FishC')
print(cs)
print(cs.lower())

class C:
    def __init__(self):
      print('我来了')
    def __del__(self):
      print('我走了')
c=C()
del c
c=C()
d=c
del c
del d

class D:
    def __init__(self,name):
      self.name=name
    def __del__(self):
      global x
      x=self
d=D('小甲鱼')
print(d.name)
del d
print(x.name)
class E:
    def __init__(self,name,func):
      self.name=name
      self.func=func
    def __del__(self):
      self.func(self)
def outter():
    x=0
    def inner(y=None):
      nonlocal x
      if y:
            x=y
      else:
            return x
    return inner
f =outter()
e=E('小甲鱼1',f)
print(e.name)
del e
g=f()
print(g.name)
页: [1]
查看完整版本: 2022-10-14 学习Python-Day18